C++/Boost:示例中的未定义符号?

C++ / Boost: Undefined Symbols in example?(C++/Boost:示例中的未定义符号?)

本文介绍了C++/Boost:示例中的未定义符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译的例子来自:http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html(聊天例子)

这是我用来制作它的:

<预><代码>>>>g++ chat_client.cpp chat_message.hpp>>>g++ chat_server.cpp chat_message.hpp

这是终端输出的内容:

% g++ chat_client.cpp chat_message.hpp在 chat_client.cpp:17 包含的文件中:chat_message.hpp:在成员函数‘void chat_message::encode_header()’中:chat_message.hpp:84: 警告:格式%4d"需要类型int",但参数 3 的类型为size_t"ld:警告:在 chat_message.hpp 中,文件是为不受支持的文件格式构建的,这不是所链接的体系结构 (x86_64)未定义符号:boost::system::generic_category()",引用自:__static_initialization_and_destruction_0(int, int)in cctJA2c1.o__static_initialization_and_destruction_0(int, int)in cctJA2c1.oboost::thread::start_thread()",引用自:boost::thread::thread, boost::_bi::list1>>>(boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >, boost::disable_if, boost::_bi::list1, boost::_bi::list1>>>在cctJA2c1.o中"vtable for boost::detail::thread_data_base",引用自:boost::detail::thread_data_base::thread_data_base() 在 cctJA2c1.oboost::thread::~thread()",引用自:_main 在 cctJA2c1.o_main 在 cctJA2c1.oboost::detail::thread_data_base::~thread_data_base()",引用自:boost::detail::thread_data, boost::_bi::list1>>>::~thread_data() 在 cctJA2c1.o 中boost::detail::thread_data, boost::_bi::list1>>>::~thread_data() 在 cctJA2c1.o 中boost::system::system_category()",引用自:cctJA2c1.o 中的 boost::system::get_system_category()cctJA2c1.o 中的 boost::system::error_code::error_code()__static_initialization_and_destruction_0(int, int)in cctJA2c1.old:未找到符号collect2: ld 返回 1 个退出状态

  • Boost 安装正确.我有另一个应用,它使用了一些 boosts 位功能.

更新:这是我正在使用的当前命令:

% g++ chat_client.cpp chat_message.hpp -lboost_asio -lboost_thread -o client -L/opt/local/lib/

说它找不到 -lboost_asio

我查看了 opt/local/lib(安装了 boost 的地方),但我没有看到任何 libboost_asio.so 或类似的东西 =

解决方案

您需要提供 g++ boost 库以与 usins -l 选项链接.我快速浏览了这个示例,并猜测您肯定需要 thread 库.您可以使用

链接它

g++ source.cpp -o 可执行文件 -lboost_thread

你也可能需要使用

的 boost_system 库

g++ source.cpp -o 可执行文件 -lboost_thread -lboost_system

The example I'm trying to compile is from: http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html (the chat example)

Here is what I'm using to make it:

>>>    g++ chat_client.cpp chat_message.hpp
>>>    g++ chat_server.cpp chat_message.hpp

This is what the terminal outputs:

% g++ chat_client.cpp chat_message.hpp
In file included from chat_client.cpp:17:
chat_message.hpp: In member function ‘void chat_message::encode_header()’:
chat_message.hpp:84: warning: format ‘%4d’ expects type ‘int’, but argument 3 has type ‘size_t’
ld: warning: in chat_message.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols:
  "boost::system::generic_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in cctJA2c1.o
      __static_initialization_and_destruction_0(int, int)in cctJA2c1.o
  "boost::thread::start_thread()", referenced from:
      boost::thread::thread<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >(boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >, boost::disable_if<boost::is_convertible<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >&, boost::detail::thread_move_t<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > > >, boost::thread::dummy*>::type)in cctJA2c1.o
  "boost::thread::join()", referenced from:
      _main in cctJA2c1.o
  "typeinfo for boost::detail::thread_data_base", referenced from:
      typeinfo for boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >in cctJA2c1.o
  "vtable for boost::detail::thread_data_base", referenced from:
      boost::detail::thread_data_base::thread_data_base()in cctJA2c1.o
  "boost::thread::~thread()", referenced from:
      _main in cctJA2c1.o
      _main in cctJA2c1.o
  "boost::detail::thread_data_base::~thread_data_base()", referenced from:
      boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data()in cctJA2c1.o
      boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data()in cctJA2c1.o
  "boost::system::system_category()", referenced from:
      boost::system::get_system_category()     in cctJA2c1.o
      boost::system::error_code::error_code()in cctJA2c1.o
      __static_initialization_and_destruction_0(int, int)in cctJA2c1.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

  • Boost is installed correctly. I have another app that uses a couple of boosts bit features.

UPDATE: this is the current command I'm using:

% g++ chat_client.cpp chat_message.hpp -lboost_asio -lboost_thread -o client -L/opt/local/lib/

says it can't find -lboost_asio

I looked in opt/local/lib (where boost is installed) and I don't see any libboost_asio.so or anything like that =

解决方案

You need to provide g++ boost libraries to link with usins -l option. I have a quick look on this examples and guess that you definitly need thread library. You can link with it using

g++ source.cpp -o executable -lboost_thread 

Also you may need with boost_system library using

g++ source.cpp -o executable -lboost_thread -lboost_system

这篇关于C++/Boost:示例中的未定义符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++/Boost:示例中的未定义符号?

基础教程推荐