链接器错误编译使用Boost.Thread使用Boost.Thread链接到静态库的可执行文件
我有一个名为MyAwesomeLib
的静态库。它是建立与下面的CMakeLists.txt链接器错误编译使用Boost.Thread使用Boost.Thread链接到静态库的可执行文件
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})
现在我想建立MyAwesomeExecutable
从MyAwesomeLib
需要的符号。可执行文件和库都使用Boost.Thread(线程组和线程类)。
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})
当我建立MyAwesomeExecutable
,Visual Studio 2010中建立的依赖MyAwesomeLib
自动。 MyAwesomeLib
建立得很好。但是,建设MyAwesomeExecutable
给出以下链接错误:
2>MyAwesomeExecutable.obj : error LNK2019: unresolved external symbol "public: void __cdecl boost::thread::join(void)" ([email protected]@[email protected]@QEAAXXZ) referenced in function "public: void __cdecl boost::thread_group::join_all(void)" ([email protected][email protected]@@QEAAXXZ)
2>MyAwesomeLib.lib(Face.obj) : error LNK2001: unresolved external symbol "public: void __cdecl boost::thread::join(void)" ([email protected]@[email protected]@QEAAXXZ)
2>MyAwesomeExecutable.obj : error LNK2019: unresolved external symbol "public: __cdecl boost::thread::~thread(void)" ([email protected]@@[email protected]) referenced in function "public: void * __cdecl boost::thread::`scalar deleting destructor'(unsigned int)" ([email protected]@@[email protected])
2>MyAwesomeLib.lib(Face.obj) : error LNK2001: unresolved external symbol "public: __cdecl boost::thread::~thread(void)" ([email protected]@@[email protected])
2>MyAwesomeExecutable.obj : error LNK2019: unresolved external symbol "private: void __cdecl boost::thread::start_thread(void)" ([email protected]@[email protected]@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 *)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@@@[email protected]@@@[email protected]@@@[email protected]@@[email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@@@[email protected]@@@[email protected]@[email protected]@@Z)
2>MyAwesomeLib.lib(Face.obj) : error LNK2001: unresolved external symbol "private: void __cdecl boost::thread::start_thread(void)" ([email protected]@[email protected]@AEAAXXZ)
静态库只是一个目标文件的集合。它可以很好地编译,因为它不使用未定义的符号。可以进行多项检查以确保增强库与您的库真正连接。请做,提供一些更多的信息。
1.使用cmake输出Boost_LIBRARY_DIRS
和Boost_INCLUDE_DIRS
的值来检查链接的增强库。
2.查看cmake生成的link.txt
文件。这个文件是为像库或可执行文件这样的每个目标生成的,并且在Linux上被放置在buildir/path/to/target/folder/CMakeFiles/targetName.dir/link.txt
之下。 link.txt
包含编译命令来构建和链接可执行文件。有了它,你可以检查,增强线程库是否实际链接到您的可执行文件。在VS
link.txt
没有生成,正如在这个答案的评论中所说。然后你可以检查使用VS本身的链接器命令行。
使用Visual Studio 10构建时,不会生成link.txt。 MinGW会发生这种情况。也许编辑你的答案并让他检查Visual Studio中的链接器命令行? – 2011-05-02 07:36:46
@Andre:从来没有在VS下使用过CMake,所以不知道link.txt的缺失。感谢您的注意。 – beduin 2011-05-02 07:54:05
您是否使用Boost的自动链接?禁用它时我有更多的运气:add_definitions(-DBOOST_ALL_NO_LIB)并显式指定动态链接:add_definitions(-DBOOST_ALL_DYN_LINK) – 2011-05-02 07:39:55