子目录中的 CMake 共享库
CMake shared library in subdirectory
我的问题解释起来比较简单。我有一个 CMake 项目(使用 CLion)运行 主要在 Windows.
一个主CMakeLists.txt
项目,链接2个子目录
src/Library
共享库
src/Executable
可执行链接 Library
请参阅下面的项目结构。
# 主要 CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(TestTest)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(src/Library)
add_subdirectory(src/Executable)
# src/Library/CMakeLists.txt
add_subdirectory(include)
add_subdirectory(src)
add_library(TestLib SHARED ${TESTLIB_H_FILES} ${TESTLIB_SRC_FILES} )
target_include_directories(TestLib PUBLIC include)
#src/Executable/CMakeLists.txt
add_subdirectory(src)
add_executable(Executable ${EXECUTABLE_SRC_FILES})
target_link_libraries(Executable PUBLIC TestLib)
我的问题是可执行文件在运行时找不到共享库。
我尝试添加 link_directories( ${CMAKE_CURRENT_BINARY_DIR}/src/Library)
但没有成功。
我错过了什么?
请注意:我不想通过 CMake 将共享库复制到可执行文件 manually/automatically 旁边,因为我相信我会丢失共享库的调试 "capability"。 GDB 将收到以下错误消息:No source file named C:/Users/flatron/CLionProjects/TestTest/src/Library/src/library.cpp.
非常欢迎和赞赏所有建议,
感谢您的帮助
我建议使用以下cmake命令:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
这会将所有可执行文件和共享库构建到 bin
文件夹中。
因此无需手动复制任何内容。
我的问题解释起来比较简单。我有一个 CMake 项目(使用 CLion)运行 主要在 Windows.
一个主CMakeLists.txt
项目,链接2个子目录
src/Library
共享库src/Executable
可执行链接Library
请参阅下面的项目结构。
cmake_minimum_required(VERSION 3.8)
project(TestTest)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(src/Library)
add_subdirectory(src/Executable)
# src/Library/CMakeLists.txt
add_subdirectory(include)
add_subdirectory(src)
add_library(TestLib SHARED ${TESTLIB_H_FILES} ${TESTLIB_SRC_FILES} )
target_include_directories(TestLib PUBLIC include)
#src/Executable/CMakeLists.txt
add_subdirectory(src)
add_executable(Executable ${EXECUTABLE_SRC_FILES})
target_link_libraries(Executable PUBLIC TestLib)
我的问题是可执行文件在运行时找不到共享库。
我尝试添加 link_directories( ${CMAKE_CURRENT_BINARY_DIR}/src/Library)
但没有成功。
我错过了什么?
请注意:我不想通过 CMake 将共享库复制到可执行文件 manually/automatically 旁边,因为我相信我会丢失共享库的调试 "capability"。 GDB 将收到以下错误消息:No source file named C:/Users/flatron/CLionProjects/TestTest/src/Library/src/library.cpp.
非常欢迎和赞赏所有建议,
感谢您的帮助
我建议使用以下cmake命令:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
这会将所有可执行文件和共享库构建到 bin
文件夹中。
因此无需手动复制任何内容。