2011-05-02 53 views
0

我有一個名爲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}) 

現在我想建立MyAwesomeExecutableMyAwesomeLib需要的符號。可執行文件和庫都使用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) 
+0

您是否使用Boost的自動鏈接?禁用它時我有更多的運氣:add_definitions(-DBOOST_ALL_NO_LIB)並顯式指定動態鏈接:add_definitions(-DBOOST_ALL_DYN_LINK) – 2011-05-02 07:39:55

回答

0

靜態庫只是一個目標文件的集合。它可以很好地編譯,因爲它不使用未定義的符號。可以進行多項檢查以確保增強庫與您的庫真正連接。請做,提供一些更多的信息。

1.使用cmake輸出Boost_LIBRARY_DIRSBoost_INCLUDE_DIRS的值來檢查鏈接的增強庫。

2.查看cmake生成的link.txt文件。這個文件是爲像庫或可執行文件這樣的每個目標生成的,並且在Linux上被放置在buildir/path/to/target/folder/CMakeFiles/targetName.dir/link.txt之下。 link.txt包含編譯命令來構建和鏈接可執行文件。有了它,你可以檢查,增強線程庫是否實際鏈接到您的可執行文件。在VSlink.txt沒有生成,正如在這個答案的評論中所說。然後你可以檢查使用VS本身的鏈接器命令行。

+0

使用Visual Studio 10構建時,不會生成link.txt。 MinGW會發生這種情況。也許編輯你的答案並讓他檢查Visual Studio中的鏈接器命令行? – 2011-05-02 07:36:46

+0

@Andre:從來沒有在VS下使用過CMake,所以不知道link.txt的缺失。感謝您的注意。 – beduin 2011-05-02 07:54:05

相關問題