2016-12-31 236 views
1

我用cmake建立我的項目,conan安裝Google Test的依賴性:GTEST安裝了柯南:未定義的引用`測試::內部:: GetBoolAssertionFailureMessage`

conanfile.txt

[requires] 
gtest/[email protected]/stable 

[generators] 
cmake 

[imports] 
bin, *.dll -> ./build/bin 
lib, *.dylib* -> ./build/bin 

的CMakeLists.txt

PROJECT(MyTestingExample) 
CMAKE_MINIMUM_REQUIRED(VERSION 2.8) 

INCLUDE(conanbuildinfo.cmake) 
CONAN_BASIC_SETUP() 

ADD_EXECUTABLE(my_test test/my_test.cpp) 
TARGET_LINK_LIBRARIES(my_test ${CONAN_LIBS}) 

測試/ my_test.cpp

#include <gtest/gtest.h> 
#include <string> 

TEST(MyTest, foobar) { 
    std::string foo("foobar"); 
    std::string bar("foobar"); 
    ASSERT_STREQ(foo.c_str(), bar.c_str()); // working 
    EXPECT_FALSE(false); // error 
} 

構建

$ conan install --build=missing 
$ mkdir build && cd build 
$ cmake .. && cmake --build . 

我可以使用ASSERT_STREQ,但如果我用EXPECT_FALSE我得到一個意外的錯誤:

my_test.cpp:(.text+0x1e1): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)' 
collect2: error: ld returned 1 exit status 

什麼問題與我的配置?

+0

在其操作系統和編譯器(帶版本)你在運行嗎? ''conaninfo.txt''和''CONAN_LIBS''內容可能也很有用。 – drodri

+1

它可能是一個''libcxx''不匹配,從錯誤輸出看來你應該使用''libcxx = libstdC++ 11''。如果在linux中(通常不是多配置的envs),你應該使用''cmake .. -DCMAKE_BUILD_TYPE = Release'',假設conan安裝使用默認設置''-s build_type = Release'' – drodri

+0

謝謝非常多,'-DCMAKE_BUILD_TYPE = Release'修復了這個問題。 – maiermic

回答

3

的問題是,要安裝使用默認設置(這是構建型發佈)柯南的依賴關係:

$ conan install --build=missing 
# equivalent to 
$ conan install -s build_type=Release ... --build=missing 

的默認設置可以在conan.conf文件中可以看出

然後,您在nix系統中使用cmake,默認構建類型爲調試,這是一個單一conf配置環境(與多配置調試/發佈環境相反,與Visual Studio相反),因此當您執行此操作時:

$ cmake .. && cmake --build . 
# equivalent to 
$ cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . 

調試/發佈版本的不兼容導致這個未解決的問題。因此,解決辦法是使用已安裝的依賴關係相匹配的相同的生成類型:

$ cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . 

如果使用像Visual Studio多配置環境中,正確的方法是:

$ cmake .. && cmake --build . --config Release 
+0

謝謝你的回答,它也適用於@maiermic的特定示例,但使用不同的斷言仍然顯示對我的未定義引用(例如'ASSERT_TRUE(「hi」==「hallo」)) ;)'。你是否也嘗試過不同的斷言類型? – erikzenker

+1

這很奇怪,一個斷言正在工作,但不是另一個。也許值得自己提問詳細的輸出和文件,或者將問題提交給軟件包作者回購:https:// github。com/lasote/conan-gtest(如果它是你正在使用的包) – drodri

+0

我開始了一個後續問題http://stackoverflow.com/questions/42162014/gtest-installed-with-conan-undefined-reference並且還打開了關於lasote https://github.com/lasote/conan-gtest/issues/20的回購問題 – erikzenker