2011-03-26 45 views
3

讓我快速設置問題。我有一個依賴於boost的庫「序列」,它是在cmake中設置的,我有一個Findserial.cmake被安裝來幫助我的第二個庫「xbow_400」找到它。這是所有罰款,直到我試圖編譯「test_xbow_400」鏈接到「xbow_400」,在這一點上,我得到這個連接錯誤:現在未定義的符號:「boost :: system :: generic_category()」Cmake和boost setup

Undefined symbols: 
    "boost::system::generic_category()", referenced from: 
     __static_initialization_and_destruction_0(int, int)in test_xbow_400.o 
     __static_initialization_and_destruction_0(int, int)in test_xbow_400.o 
     __static_initialization_and_destruction_0(int, int)in libxbow_400.a(xbow_400.o) 
     __static_initialization_and_destruction_0(int, int)in libxbow_400.a(xbow_400.o) 
     __static_initialization_and_destruction_0(int, int)in libserial.a(serial.o) 
     __static_initialization_and_destruction_0(int, int)in libserial.a(serial.o) 
    "boost::system::system_category()", referenced from: 
     boost::asio::error::get_system_category() in test_xbow_400.o 
     __static_initialization_and_destruction_0(int, int)in test_xbow_400.o 
     boost::asio::error::get_system_category() in libxbow_400.a(xbow_400.o) 
     __static_initialization_and_destruction_0(int, int)in libxbow_400.a(xbow_400.o) 
     boost::asio::error::get_system_category() in libserial.a(serial.o) 
     boost::system::error_code::error_code()in libserial.a(serial.o) 
     __static_initialization_and_destruction_0(int, int)in libserial.a(serial.o) 
ld: symbol(s) not found 

,我可以通過添加這些行到我的CMakeLists解決這個問題。對於「xbow_400」 txt文件:

+ # Find Boost 
+ find_package(Boost COMPONENTS system filesystem thread REQUIRED) 
+ 
+ link_directories(${Boost_LIBRARY_DIRS}) 
+ include_directories(${Boost_INCLUDE_DIRS}) 

    # Compile the xbow_400 Library 
    add_library(xbow_400 src/xbow_400.cpp include/xbow_400.h) 
- target_link_libraries(xbow_400 ${serial_LIBRARIES}) 
+ target_link_libraries(xbow_400 ${serial_LIBRARIES} ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY}) 

,但我想有xbow_400使用串口,​​而不必專門爲尋找刺激,並鏈接到它。 (xbow_400代碼不包含或直接使用boost,只能通過串行庫)。

這可能嗎?如果是這樣,我應該添加的東西FindSerial.cmake或者我應該改變我建立序列的方式?

串行庫:https://github.com/wjwwood/serial xbow_400:https://github.com/wjwwood/Crossbow-IMU400CC-100

我在OS X 10.6.6。我還沒有在Linux或Windows上嘗試過。

回答

2

假設我正確理解你的問題然後不,這是不可能的。如果你的程序使用庫A並且庫使用庫B,那麼你必須將你的程序鏈接到庫B.如果庫B使用庫C,那麼你也必須鏈接庫C。這可以永遠持續下去。

有些時候鏈接器可以告訴你如果你不使用符號,有時它不能。例如:如果庫A包含全局符號a和b,並且在程序中使用符號a但不包含符號b或c,則包含GNU鏈接程序的靜態符號c。鏈接器將插入符號b,但不插入符號c,即使這兩個符號都未使用。這裏的規則很複雜,特定於平臺。爲了讓鏈接器不連接boost :: system,你必須弄清楚什麼規則導致鏈接器想要這個符號並且改變你的代碼來移除依賴關係。在大多數情況下,除非鏈接器發瘋,並將20 MB不需要的符號帶入您的程序,否則可能不值得付出努力。

相關問題