创建封装其他 2 个项目的可执行文件
Creating an executable encapsulating 2 other projects
我正在使用 Eclipse C++ 开发 Windows。我的项目中有这个结构:
subProject A
-> creates a static library (works fine)
subProject B encapsulating subproject A
-> creates a static library (works fine)
subProject TestA
-> creates an executable (works fine)
subProject TestB
|dev
|src
| CmakeList.txt
|CmakeList.txt
-> creates an executable (doesn't work)
这是 TestB 可执行文件的开发目录中的CmakeList.txt:
set(${PROJECT_NAME}_INCLUDE_DIRS
$(CMAKE_CURRENT_SOURCE_DIR}/src
CACHE STRING "" FORCE)
include_directories(${${PROJECT_NAME}_INCLUDE_DIRS})
file(GLOB_RECURSE ${PROJECT_NAME}_HEADER_FILES FOLLOW_SYMLINKS
./*.hpp
./*.h)
file(GLOB_RECURSE ${PROJECT_NAME}_SORUCE_FILES FOLLOW_SYMLINKS
./*.cpp
./*.c)
set(${PROJECT_NAME}_FILES ${${PROJECT_NAME}_HEADER_FILES}
${${PROJECT_NAME}_SOURCE_FILES})
# Additional includes and link directories and dependencies
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/B/include)
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
endif()
# Add a library to the project
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/lib)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/B/lib)
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_FILES})
target_link_libraries(${PROJECT_NAME} ${${PROJECT_NAME}_EXTRA_LIBRARIES})
install(FILES ${${PROJECT_NAME}_HEADER_FILES}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/
PERSMISSIONS OWNER_READ GROUP_READ WORLD_READ)
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
在我的 B 子项目中,在一个文件中,我有这个:
#include "A.hpp"
...
A algo;
A.method(parameters)
当我构建我的 TestB 时,我在 B 项目 上面的两行中出现了这个奇怪的错误,这导致我的构建失败:
undefined reference to A::A()
undefined reference to A::method(parameter)
当我评论这些行并重建 B 项目 和 TestB 项目 时它工作正常,我在 B 中有一个库项目和 TestB 中的 .exe。
我不明白我的问题在哪里。
B
依赖于A
,所以将B
的CMakeLists.txt中的依赖添加到A和target_link_libraries
中。即使它是一个静态库,并且当你构建 B
时不会有任何实际的 linking,这无论如何都会给 CMake 一个提示,即每次你 link B
到可执行文件,您还必须 link 之后 到 A
。 A
必须在 link 命令行中的 B
之后,以便在到达 link 到 [=13] 时知道 B
所需的符号=].有关详细信息,请参阅 here。
如果您正在寻找快速修复,只需更改:
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
至此
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
B
A
pthread)
我正在使用 Eclipse C++ 开发 Windows。我的项目中有这个结构:
subProject A
-> creates a static library (works fine)
subProject B encapsulating subproject A
-> creates a static library (works fine)
subProject TestA
-> creates an executable (works fine)
subProject TestB
|dev
|src
| CmakeList.txt
|CmakeList.txt
-> creates an executable (doesn't work)
这是 TestB 可执行文件的开发目录中的CmakeList.txt:
set(${PROJECT_NAME}_INCLUDE_DIRS
$(CMAKE_CURRENT_SOURCE_DIR}/src
CACHE STRING "" FORCE)
include_directories(${${PROJECT_NAME}_INCLUDE_DIRS})
file(GLOB_RECURSE ${PROJECT_NAME}_HEADER_FILES FOLLOW_SYMLINKS
./*.hpp
./*.h)
file(GLOB_RECURSE ${PROJECT_NAME}_SORUCE_FILES FOLLOW_SYMLINKS
./*.cpp
./*.c)
set(${PROJECT_NAME}_FILES ${${PROJECT_NAME}_HEADER_FILES}
${${PROJECT_NAME}_SOURCE_FILES})
# Additional includes and link directories and dependencies
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/B/include)
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
endif()
# Add a library to the project
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/lib)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/B/lib)
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_FILES})
target_link_libraries(${PROJECT_NAME} ${${PROJECT_NAME}_EXTRA_LIBRARIES})
install(FILES ${${PROJECT_NAME}_HEADER_FILES}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/
PERSMISSIONS OWNER_READ GROUP_READ WORLD_READ)
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
在我的 B 子项目中,在一个文件中,我有这个:
#include "A.hpp"
...
A algo;
A.method(parameters)
当我构建我的 TestB 时,我在 B 项目 上面的两行中出现了这个奇怪的错误,这导致我的构建失败:
undefined reference to A::A()
undefined reference to A::method(parameter)
当我评论这些行并重建 B 项目 和 TestB 项目 时它工作正常,我在 B 中有一个库项目和 TestB 中的 .exe。
我不明白我的问题在哪里。
B
依赖于A
,所以将B
的CMakeLists.txt中的依赖添加到A和target_link_libraries
中。即使它是一个静态库,并且当你构建 B
时不会有任何实际的 linking,这无论如何都会给 CMake 一个提示,即每次你 link B
到可执行文件,您还必须 link 之后 到 A
。 A
必须在 link 命令行中的 B
之后,以便在到达 link 到 [=13] 时知道 B
所需的符号=].有关详细信息,请参阅 here。
如果您正在寻找快速修复,只需更改:
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
至此
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
B
A
pthread)