我在cmake项目中编译一些单元测试时遇到了问题。我们的想法是将这些boost单元测试添加为可执行文件,以便我可以运行它们。无论如何我得到这个错误,我没有放弃理解,这是说我无法链接我的cpp单元测试必须链接到测试我建立的库。我试图链接并添加为可执行文件的两个单元测试是DownloadTickers.cpp和GetTickersForLetter.cpp。
我的目录结构如下:
> Algo
> build (this is where I do: cmake .. which gives me errors)
-CMakeLists.txt (top level cmake)
-algo.h.ini
-run.cpp
> NetworkModule
> CrawlTickers
-CMakeLists.txt
-CrawlTickers.cpp
-CrawlTickers.hpp
> tests
-CMakeLists.txt
-DownloadTickers.cpp
-GetTickersForLetter.cpp
Algo的CMakeLists.txt是:
cmake_minimum_required (VERSION 2.8)
project (Algo)
set (Algo_VERSION_MAJOR 0)
set (Algo_VERSION_MINOR 1)
set (CMAKE_CXX_COMPILER g++-4.8)
set (CMAKE_BUILD_TYPE Release)
add_definitions(
-std=c++11
)
configure_file(
"${PROJECT_SOURCE_DIR}/algo.h.in"
"${PROJECT_BINARY_DIR}/algo.h"
)
include_directories("${PROJECT_BINARY_DIR}")
add_subdirectory(NetworkModule/CrawlTickers)
add_executable(Run run.cpp)
CrawlTickers的CMakeLists.txt是:
find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
add_library(CrawlTickers SHARED CrawlTickers.cpp)
add_subdirectory(tests)
target_link_libraries(
CrawlTickers
cpprest
)
target_link_libraries(
DownloadTickers
CrawlTickers
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
)
target_link_libraries(
GetTickersForLetter
CrawlTickers
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
)
测试的CMakeLists.txt是:
add_executable( DownloadTickers DownloadTickers.cpp )
add_executable( GetTickersForLetter GetTickersForLetter.cpp )
我的想法是cmake会自然地将DownloadTickers和GetTickersForLetter注册为CrawlTickers CMakeLists.txt中的可执行文件,然后知道如何将其链接到目标,但我不知道为什么我会收到此错误。任何帮助是极大的赞赏。感谢。
答案 0 :(得分:6)
target_link_libraries
指令需要与CMakeLists.txt
指令属于同一add_executable
。这也适用于图书馆。将每个目录和关联的CMakeLists.txt
视为子项目。