0

CMake と gtest を使用する簡単なプロジェクトを進めています。基本的な CMakeLists.txt ファイルが機能していますが、複数の CMakeLists.txt を使用してそれらを接続する方法をよりよく理解したいと考えています。プロジェクトのこれまでのコードは次のようになります。

https://github.com/dmonopoly/writeart/tree/10b62048e6eb6a6ddd0658123d85ce4f5f601178

より簡単に参照できるように、私が利用する唯一の CMakeLists.txt ファイル (プロジェクト ルート内) には、これが含まれています。

cmake_minimum_required(VERSION 2.8)

# Options
option(TEST "Build all tests." OFF) # makes boolean 'TEST' available

# Make PROJECT_SOURCE_DIR, PROJECT_BINARY_DIR, and PROJECT_NAME available
set(PROJECT_NAME MyProject)
project(${PROJECT_NAME})

set(CMAKE_CXX_FLAGS "-g") # -Wall")

#set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/include) if you want your own include/ directory
# then you can do include_directories(${COMMON_INCLUDES}) in other cmakelists.txt files

################################
# Normal Libraries & Executables
################################
add_library(standard_lib Standard.cpp Standard.h)
add_library(converter_lib Converter.cpp Converter.h)
add_executable(main Main.cpp)

target_link_libraries(main standard_lib converter_lib)

################################
# Testing
################################
if (TEST)
    # This adds another subdirectory, which has project(gtest)
    add_subdirectory(lib/gtest-1.6.0)

    enable_testing()

    # Include the gtest library
    # gtest_SOURCE_DIR is available due to project(gtest) above
    include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

    ##############
    # Unit Tests
    ##############
    # Naming
    set(UNIT_TESTS runUnitTests)

    add_executable(${UNIT_TESTS} ConverterTest.cpp)

    # standard linking to gtest stuff
    target_link_libraries(${UNIT_TESTS} gtest gtest_main)

    # extra linking for the project
    target_link_libraries(${UNIT_TESTS} standard_lib converter_lib)

    # This is so you can do 'make test' to see all your tests run, instead of manually running the executable runUnitTests to see those specific tests.
    add_test(NAME myUnitTests COMMAND runUnitTests)
endif()

私の目標は、Standard.cpp と Standard.h を lib/ に移動することです。ただし、これを行った瞬間に、CMakeLists.txt で行っていることの順序が複雑であることに気付きます。gtest セットアップ用のライブラリが必要ですが、ライブラリは lib/CMakeLists.txt で作成する必要がありますよね? すべての CMakeLists.txt を調べなければならないので、すべてのライブラリと実行可能ファイルがどこにあるかを見つけるのは簡単に複雑になりませんか?

概念的に何かが欠けている場合、またはこれを簡単に解決するために使用できる良い例があれば、それは素晴らしいことです.

よろしくお願いします。よろしくお願いします。

4

1 に答える 1

1

CMakeLists.txt複数のファイルを使用したくない場合は、使用しないでください。

################################
# Normal Libraries & Executables
################################

add_library(standard_lib lib/Standard.cpp lib/Standard.h)
add_library(converter_lib lib/Converter.cpp lib/Converter.h)

# Main.cpp needs to know where "Standard.h" is for the #include, 
#   so we tell it to search this directory too. 
include_directories(lib)

複数が必要な場合CMakeLists.txtは、移動します。

# Main CMakeLists.txt:
add_subdirectory(lib)

include_directories (${standard_lib_SOURCE_DIR}/standard_lib) 

link_directories (${standard_lib_BINARY_DIR}/standard_lib) 

とで/lib/CMakeLists.txt

add_library (standard_lib Standard.cpp)

ここに例があります。

于 2013-01-08T04:18:07.610 に答える