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.
My directory structure is as follows:
> 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
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)
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}
)
The CMakeLists.txt for tests is:
add_executable( DownloadTickers DownloadTickers.cpp )
add_executable( GetTickersForLetter GetTickersForLetter.cpp )
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.
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.
As pointed out in the comments, CMake 3.13 now allows the requested behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With