2

cmakeを使用してプロジェクトをビルドし、conanを使用してGoogle Testを依存関係としてインストールします。

コナンファイル.txt

[requires]
gtest/1.7.0@lasote/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

構成の何が問題になっていますか?

4

1 に答える 1

6

問題は、デフォルト設定 (ビルド タイプRelease )を使用して conan 依存関係をインストールしていることです。

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

デフォルト設定はconan.confファイルで確認できます

次に、デフォルトのビルドタイプがDebugである 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
于 2016-12-31T14:55:49.787 に答える