macos - undefined symbol : "boost::system::generic_category()" Cmake and boost setup

标签 macos boost cmake

让我快速解决问题。我有一个依赖于 boost 的库“serial”,它是在 cmake 中设置的,我安装了一个 Findserial.cmake 来帮助我的第二个库“xbow_400”找到它。这一切都很好,直到我尝试编译链接到“xbow_400”的“test_xbow_400”,此时我收到此链接错误:

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

现在,我可以通过将这些行添加到“xbow_400”的 CMakeLists.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 使用 serial 而不必专门寻找 boost 并链接到它。 (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 上尝试过。

最佳答案

假设我正确理解您的问题,那么不,这是不可能的。如果您的程序使用库 A 而库使用库 B,那么您必须将您的程序链接到库 B。如果库 B 使用库 C,那么您也必须链接到库 C。这可以永远持续下去。

有时链接器可以判断您是否没有使用符号,有时则不能。例如:如果库 A 包含全局符号 a 和 b 以及带有 GNU 链接器的静态符号 c 如果您在程序中使用符号 a 而不是符号 b 或 c。链接器将插入符号 b 而不是符号 c,即使这两个符号都未使用。这里的规则很复杂并且特定于平台。要使链接器不链接 boost::system,您必须弄清楚是什么规则导致链接器需要该符号并更改您的代码以删除依赖项。在大多数情况下,除非链接器发疯并将 20 MB 不需要的符号带入您的程序,否则可能不值得付出努力。

关于macos - undefined symbol : "boost::system::generic_category()" Cmake and boost setup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5444633/

相关文章:

c++ - 在 Mysql ODBC 中禁用 GUI

linux - Bash - 错误信息 'Syntax error: "("unexpected'

c++ - 编译但得到 'uncaught exception of type std::bad_cast'

c++ - 具有自动生成源的 Cmake 可执行文件

C++项目与多个版本的boost的兼容性

c++ - 拥有以下计时器的最简单方法

使用 contribute_world 编译 opencv_world 时 opencv3.2 build on vs2017 报错

php - curl 错误 : OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 54

xcode - 减小 cocoa 应用程序中图像的大小

java - 在 mac os 上配置 hadoop namenode 时出现 yarn-site.xml 错误