开发者

Undefined symbols: "boost::system::generic_category()" Cmake and boost setup

Let me setup the problem real quick. I have a library "serial" that depends on boost and it is setup in cmake and I have a Findserial.cmake that gets installed to help my second library "xbow_400" find it. This is all fine until I try to compile "test_xbow_400" that links to "xbow_400", at which point I get this linking error:

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

Now, I can fix this problem by adding these lines to my CMakeLists.txt file for "xbow_400":

+ # 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})

BUT, I would like to have xbow_400 use serial WITHOUT having to specifically having to find boost and link to it. (The xbow_400 code does not include or use boost directly, only via the serial library).

Is this possible? And if so s开发者_JAVA百科hould I add things to Findserial.cmake or should I change the way I build serial?

Serial library: https://github.com/wjwwood/serial xbow_400: https://github.com/wjwwood/Crossbow-IMU400CC-100

I am on OS X 10.6.6. I have not tried this on Linux or Windows yet.


Assuming that I understand your problem correctly then no, this is not possible. If your program uses library A and library uses library B then you have to link your program against library B. If library B uses library C then you must link against library C as well. This can go on forever.

Some of the time the linker can tell if you are not using a symbol and sometimes it cannot. For example: if library A contains global symbols a and b and static symbol c with the GNU linker if you use symbol a in your program but not symbol b or c. The linker will insert symbol b but not symbol c even though both of these symbols are unused. The rules here are complicated and platform specific. To get the linker to not link boost::system you would have to figure out what rule was causing the linker to want that symbol and alter you code to remove the dependency. In most cases it is probably not worth the effort unless the linker is going crazy and bringing 20 MB of unneeded symbols into your program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜