[英]Linker error compiling an executable which uses Boost.Thread linking to a static library using Boost.Thread
I have a static library called MyAwesomeLib
.我有一个名为
MyAwesomeLib
的 static 库。 It is built with the CMakeLists.txt below它是用下面的 CMakeLists.txt 构建的
PROJECT(MyAwesomeLib)
find_package(OpenCV)
find_package(VTK REQUIRED)
find_package(OpenGL)
find_package(GLUT)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost COMPONENTS thread)
if(NOT Boost_FOUND)
message(SEND_ERROR "Cannot find Boost Thread")
endif(NOT Boost_FOUND)
link_directories (${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIR} ${GLUT_INCLUDE_DIR})
INCLUDE(${VTK_USE_FILE})
file(GLOB SRCS "*.cpp" "*.c")
file(GLOB HDRS "*.h")
add_library(MyAwesomeLib STATIC ${SRCS} ${HDRS})
target_link_libraries(MyAwesomeLib ${OpenCV_LIBS} ${Boost_LIBRARIES} ${GLUT_LIBRARY} ${OPENGL_LIBRARY})
Now I want to build MyAwesomeExecutable
which needs symbols from MyAwesomeLib
.现在我想构建
MyAwesomeExecutable
,它需要来自MyAwesomeLib
的符号。 Both the executable and the library uses Boost.Thread (thread_group and thread class).可执行文件和库都使用 Boost.Thread(thread_group 和线程类)。
PROJECT(MyAwesomeExecutable)
FIND_PACKAGE(OpenCV REQUIRED)
FIND_PACKAGE(VTK REQUIRED)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost COMPONENTS thread)
if(NOT Boost_FOUND)
message(SEND_ERROR "Cannot find Boost Thread")
endif(NOT Boost_FOUND)
link_directories (${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIR} ${GLUT_INCLUDE_DIR})
INCLUDE(${VTK_USE_FILE})
FILE(GLOB SRCS "*.cpp" "*.c")
FILE(GLOB HDRS "*.h")
ADD_EXECUTABLE(MyAwesomeExecutable ${SRCS} ${HDRS})
TARGET_LINK_LIBRARIES(MyAwesomeExecutable MyAwesomeLib ${Boost_LIBRARIES} ${GLUT_LIBRARY} ${OPENGL_LIBRARY} ${OpenCV_LIBS})
When I build MyAwesomeExecutable
, Visual Studio 2010 build its dependency MyAwesomeLib
automatically.当我构建
MyAwesomeExecutable
时,Visual Studio 2010 会自动构建其依赖MyAwesomeLib
。 MyAwesomeLib
builds just fine. MyAwesomeLib
构建得很好。 But building MyAwesomeExecutable
gives the following linker errors:但是构建
MyAwesomeExecutable
会出现以下 linker 错误:
2>MyAwesomeExecutable.obj : error LNK2019: unresolved external symbol "public: void __cdecl boost::thread::join(void)" (?join@thread@boost@@QEAAXXZ) referenced in function "public: void __cdecl boost::thread_group::join_all(void)" (?join_all@thread_group@boost@@QEAAXXZ)
2>MyAwesomeLib.lib(Face.obj) : error LNK2001: unresolved external symbol "public: void __cdecl boost::thread::join(void)" (?join@thread@boost@@QEAAXXZ)
2>MyAwesomeExecutable.obj : error LNK2019: unresolved external symbol "public: __cdecl boost::thread::~thread(void)" (??1thread@boost@@QEAA@XZ) referenced in function "public: void * __cdecl boost::thread::`scalar deleting destructor'(unsigned int)" (??_Gthread@boost@@QEAAPEAXI@Z)
2>MyAwesomeLib.lib(Face.obj) : error LNK2001: unresolved external symbol "public: __cdecl boost::thread::~thread(void)" (??1thread@boost@@QEAA@XZ)
2>MyAwesomeExecutable.obj : error LNK2019: unresolved external symbol "private: void __cdecl boost::thread::start_thread(void)" (?start_thread@thread@boost@@AEAAXXZ) referenced in function "public: __cdecl boost::thread::thread<class boost::_bi::bind_t<void,void (__cdecl*)(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >),class boost::_bi::list1<class boost::_bi::value<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > >(class boost::_bi::bind_t<void,void (__cdecl*)(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >),class boost::_bi::list1<class boost::_bi::value<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >,struct boost::thread::dummy *)" (??$?0V?$bind_t@XP6AXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@ZV?$list1@V?$value@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@_bi@boost@@@_bi@boost@@@_bi@boost@@@thread@boost@@QEAA@V?$bind_t@XP6AXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@ZV?$list1@V?$value@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@_bi@boost@@@_bi@boost@@@_bi@1@PEAUdummy@01@@Z)
2>MyAwesomeLib.lib(Face.obj) : error LNK2001: unresolved external symbol "private: void __cdecl boost::thread::start_thread(void)" (?start_thread@thread@boost@@AEAAXXZ)
Static library is just a collection of object files. Static 库只是 object 文件的集合。 It can compile quite well, because it doesn't use undefined symbols.
它可以很好地编译,因为它不使用未定义的符号。 Several checks can be done to ensure that boost libraries is really linked with your library.
可以进行多项检查以确保 boost 库确实与您的库链接。 Please, do it to provide some more information.
请这样做以提供更多信息。
1.Using cmake print out values from Boost_LIBRARY_DIRS
and Boost_INCLUDE_DIRS
to check linked boost libraries. 1.使用 cmake 打印
Boost_LIBRARY_DIRS
和Boost_INCLUDE_DIRS
的值来检查链接的 boost 库。
2.Check link.txt
file generated by cmake. 2.查看cmake生成的
link.txt
文件。 This file is generated for each target like library or executable and on Linux it is placed under buildir/path/to/target/folder/CMakeFiles/targetName.dir/link.txt
.该文件是为每个目标(如库或可执行文件)生成的,在 Linux 上,它位于
buildir/path/to/target/folder/CMakeFiles/targetName.dir/link.txt
。 link.txt
contains compiler command to build and link executable. link.txt
包含用于构建和链接可执行文件的编译器命令。 With it you can check, whether boost thread libraries are actually linked to your executable.有了它,您可以检查 boost 线程库是否实际链接到您的可执行文件。 In
VS
link.txt
isn't generated, as said in the comment to this answer.如对此答案的评论所述,在
VS
中未生成link.txt
。 Then you can check for linker command-line using VS itself.然后您可以使用 VS 本身检查 linker 命令行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.