我有一个使用自己的代码和需要编译的外部代码的项目。我的文件树如下。
PROJECT_ROOT
|--CMakeLists.txt
|--src
|--CMakeLists.txt
|--B1.cpp
|--B1.h
|--B2.cpp
|--B2.h
|
|--ext
| |--CMakeLists.txt
| |--outside_module
| | |--CMakeLists.txt
| | |--A1.h
| | |--A1.cpp
|
|--exe
|--CMakeLists.txt
|--runnerA.cpp
|--runnerB.cpp
我在下面粘贴了我的CMakeList.txt文件的结构。我遇到的问题是我的runnerA
可执行文件使我对A1.h和A1.cpp中定义的符号产生了许多未定义的引用错误。但是,由于某些原因,A
当试图链接到可执行文件时,CMake无法找到我的库runnerA
。
我包括了其余的代码,因为它可以很好地编译。我能够runnerB
毫无问题地生成可执行文件。我推断出这种方法是runnerB
有效的,因为源文件位于我的src
文件夹中,该文件夹是我的文件夹的父exe
文件夹,因此B
库范围包括exe
。
我如何让CMake知道在哪里可以找到我的A
库,以便在将其链接到runnerA
in时可以找到它src/exe
?
PROJECT_ROOT
cmake_minimum_required(VERSION 2.8)
project(MyProject)
add_subdirectory(src)
PROJECT_ROOT / src
find_package(Module REQUIRED)
# CREATE LIBRARIES
add_library(
B
B1.cpp B1.h
B2.cpp B2.h
)
# GROUP LIBRARIES TOGETHER
set(
B_LIBS
B
${Module_LIBS}
)
# INCLUDE DIRECTORIES
set(
B_INCLUDES
.
${Module_INCLUDE_DIRS}
)
# ASSIGN INCLUDES TO LIBRARY
target_include_directories(
B PUBLIC
${B_INCLUDES}
)
add_subdirectory(ext)
add_subdirectory(exe)
PROJECT_ROOT / src / ext
add_subdirectory(outside_module)
PROJECT_ROOT / src / ext / outside_module
find_package(Boost COMPONENTS
filesystem
system
log
thread
REQUIRED)
# ADD DEFINITIONS
add_definitions(-DBOOST_LOG_DYN_LINK) # Necessary to compile A with log shared libraries (instead of static)
# DEFINE LIBRARY
add_library(
A
A1.cpp A2.h
)
# LINK LIBRARIES
set(
A_LIBS
A
${Boost_FILESYSTEM_LIBRARY}
${Boost_LOG_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
)
# INCLUDE DIRECTORIES
set(
A_INCLUDES
${Boost_INCLUDE_DIRS}
)
PROJECT_ROOT / src / exe
# DEFINE EXECUTABLES
add_executable(runnerB runnerB.cpp)
add_executable(runnerA runnerA.cpp)
##################
# LINK LIBRARIES
##################
target_link_libraries(
runnerB
${B_LIBS}
)
target_link_libraries(
runnerA
${A_LIBS}
)
target_include_directories(
runnerA PUBLIC
${A_INCLUDES}
)
# INSTALLATION
install(TARGETS runnerB DESTINATION bin/)
install(TARGETS runnerA DESTINATION bin/)
正确,您${A_LIBS}
在嵌套的CMakeLists.txt中设置了该变量。解决方案是将set()命令与PARENT_SCOPE
或CACHE
关键字一起使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句