使用Linux上的CMake将提升链接到共享库

时间:2022-09-18 12:26:25

i have one executable and one shared library in my project. The shared library uses boost library. the executable uses olny the shared library.

我的项目中有一个可执行文件和一个共享库。共享库使用boost库。可执行文件使用olny共享库。

tilenet/             <-- Project
   ttest/            <-- Test (executable)
      CMakeLists.txt
   tilenet/          <-- The shared library
      CMakeLists.txt
   CMakeLists.txt    <-- Root CMake-file

Root Cmake-file:

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)

ttest/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}
            )

(I've cut some unimportant stuff)

(我削减了一些不重要的东西)

On windows it works fine (but there i use VisuelStudio without CMake), but on linux i get following linking errors:

在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

I've tried a lot of combinations with given options but i was not able to link it. Do you know where i made mistakes ? This is the first time i've used CMake seriously :)

我已经尝试了很多给定选项的组合,但我无法链接它。你知道我犯了哪些错误吗?这是我第一次认真使用CMake :)

1 个解决方案

#1


7  

CMake variables are case-sensitive, and the FindBoost module sets the boost libraries to a variable named Boost_LIBRARIES, not BOOST_LIBRARIES.

CMake变量区分大小写,FindBoost模块将boost库设置为名为Boost_LIBRARIES的变量,而不是BOOST_LIBRARIES。

If you replace ${BOOST_LIBRARIES} with ${Boost_LIBRARIES} in your two target_link_libraries calls and it should work correctly.

如果在两个target_link_libraries调用中用$ {Boost_LIBRARIES}替换$ {BOOST_LIBRARIES},它应该可以正常工作。

For full info on the FindBoost module, run:

有关FindBoost模块的完整信息,请运行:

cmake --help-module FindBoost

#1


7  

CMake variables are case-sensitive, and the FindBoost module sets the boost libraries to a variable named Boost_LIBRARIES, not BOOST_LIBRARIES.

CMake变量区分大小写,FindBoost模块将boost库设置为名为Boost_LIBRARIES的变量,而不是BOOST_LIBRARIES。

If you replace ${BOOST_LIBRARIES} with ${Boost_LIBRARIES} in your two target_link_libraries calls and it should work correctly.

如果在两个target_link_libraries调用中用$ {Boost_LIBRARIES}替换$ {BOOST_LIBRARIES},它应该可以正常工作。

For full info on the FindBoost module, run:

有关FindBoost模块的完整信息,请运行:

cmake --help-module FindBoost