CMake:尝试将链接库添加到该目录中未构建的目标 [英] CMake: Attempted to add link library to target which is not built in this directory
问题描述
在我的cmake项目中编译某些单元测试时遇到问题.想法是将这些增强单元测试添加为可执行文件,以便随后可以运行它们.无论如何,我都得到了我不停止理解的错误,这就是说我无法链接我的cpp单元测试,必须对其进行链接才能测试我构建的库.我试图链接并添加为可执行文件的两个单元测试分别是DownloadTickers.cpp和GetTickersForLetter.cpp.
I'm having a an issue compiling some unit tests in my cmake project. The idea is to add these boost unit tests as executables, so that I can then run them. Anyway I'm getting this error that I don't quit understand, which is saying that I cannot link my cpp unit tests which must be linked to test the library I built. The two unit tests I'm trying to link and add as executables are, DownloadTickers.cpp and 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是:
The CMakeLists.txt for Algo is:
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是:
The CMakeLists.txt for CrawlTickers is:
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是:
The CMakeLists.txt for tests is:
add_executable( DownloadTickers DownloadTickers.cpp )
add_executable( GetTickersForLetter GetTickersForLetter.cpp )
我的想法是cmake会自然地将DownloadTickers和GetTickersForLetter注册为CrawlTickers CMakeLists.txt中的可执行文件,然后知道如何将其链接到目标,但是我不知道为什么会出现此错误.任何帮助是极大的赞赏.谢谢.
My thinking is that cmake would naturally register DownloadTickers and GetTickersForLetter as executables in the CrawlTickers CMakeLists.txt and then know how to link it to the target, but I have no idea why I'm getting this error. Any help is greatly appreciated. Thanks.
推荐答案
target_link_libraries
指令必须与add_executable
指令属于同一CMakeLists.txt
.这也适用于库.将每个目录和关联的CMakeLists.txt
视为一个子项目.
The target_link_libraries
directive needs to be part of the same CMakeLists.txt
as the add_executable
directive. This also applies to libraries. Think of each directory and associated CMakeLists.txt
as a subproject.
这篇关于CMake:尝试将链接库添加到该目录中未构建的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!