我的项目中有一个可执行文件和一个共享库。 共享库使用boost库。可执行文件使用 olny 共享库。
tilenet/ <-- Project
ttest/ <-- Test (executable)
CMakeLists.txt
tilenet/ <-- The shared library
CMakeLists.txt
CMakeLists.txt <-- Root CMake-file
根 Cmake 文件:
cmake_minimum_required(VERSION 2.6)
project(tilenet)
set(Boost_USE_STATIC_LIBS OFF) # I've already tried ON
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.49 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_subdirectory(test)
add_subdirectory(tilenet)
测试/CMakeLists.txt
add_executable(ttest test.cpp)
target_link_libraries(ttest tilenet ${BOOST_LIBRARIES})
tilenet/CMakeLists.txt
include_directories("include")
set(tilenet_src "src/tilenet.cpp" ...)
add_library(tilenet SHARED ${tilenet_src})
target_link_libraries(tilenet
${SFML_LIBRARIES}
${BOOST_LIBRARIES}
)
(我删掉了一些不重要的东西)
在 Windows 上它工作正常(但我使用没有 CMake 的 VisuelStudio),但在 Linux 上 我收到以下链接错误:
../../lib/libtilenet.so: undefined reference to `boost::filesystem3::path_traits::convert(wchar_t const*, wchar_t const*, std::string&, std::codecvt<wchar_t, char, __mbstate_t> const&)'
../../lib/libtilenet.so: undefined reference to `boost::filesystem3::path::operator/=(boost::filesystem3::path const&)'
../../lib/libtilenet.so: undefined reference to `boost::system::system_category()'
../../lib/libtilenet.so: undefined reference to `boost::filesystem3::path::wchar_t_codecvt_facet()'
../../lib/libtilenet.so: undefined reference to `boost::system::generic_category()'
../../lib/libtilenet.so: undefined reference to `boost::filesystem3::path_traits::convert(char const*, char const*, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, __mbstate_t> const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [../bin/ttest] Error 1
make[1]: *** [test/CMakeFiles/ttest.dir/all] Error 2
make: *** [all] Error 2
我尝试了很多给定选项的组合,但我无法将其链接起来。 你知道我哪里出错了吗?这是我第一次认真使用 CMake :)
最佳答案
CMake 变量区分大小写,FindBoost 模块将 boost 库设置为名为 Boost_LIBRARIES
的变量,而不是 BOOST_LIBRARIES
。
如果您在两个 target_link_libraries
调用中将 ${BOOST_LIBRARIES}
替换为 ${Boost_LIBRARIES}
,它应该可以正常工作。
有关 FindBoost 模块的完整信息,请运行:
cmake --help-module FindBoost
关于c++ - 在 Linux 上使用 CMake 将 boost 链接到共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585726/