2

我有一个非常简单的示例程序,我正在尝试编译,但我遇到了链接错误。

程序

int main()
{
  boost::asio::io_service io;

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
  t.wait();

  std::cout << "Hello, world!\n";

  return 0;
}

汇编

我正在使用以下编译选项:

g++ -L/usr/lib -lboost_system -lboost_filesystem -lboost_thread -pthread -I/usr/include timer.cpp -o timer

输出

我检查了动态库是否存在并且位于 -L 路径中。以下是我得到的错误:

/tmp/cc3hJrVk.o: In function `__static_initialization_and_destruction_0(int, int)':
timer.cpp:(.text+0xf0): undefined reference to `boost::system::generic_category()'
timer.cpp:(.text+0xfc): undefined reference to `boost::system::generic_category()'
timer.cpp:(.text+0x108): undefined reference to `boost::system::system_category()'
/tmp/cc3hJrVk.o: In function `boost::system::error_code::error_code()':
timer.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/cc3hJrVk.o: In function `boost::asio::error::get_system_category()':
timer.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status

正如您所看到的,如果您不正确地链接到 libboost_system,似乎会出现这些错误。我已经测试过两次单独安装的 boost,但我已经束手无策了。

提前感谢您的帮助!

4

1 回答 1

4

您需要在命令末尾的文件名之后移动所有库。

另外:Boost::System 必须与普通的 boost 库分开构建。因此,如果您没有单独构建它们,它们将不存在。

请参阅此处的第 3 节:

http://www.boost.org/doc/libs/1_49_0/more/getting_started/unix-variants.html

于 2012-04-13T06:19:09.097 回答