如何使用boost / asio.hpp文件g ++一个C ++程序?

时间:2014-01-25 03:27:23

标签: c++ boost

我有以下示例程序

http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/sync_client.cpp

当我执行时,我收到此错误..

$ g++ sync_client.cpp -I usr/include/boost

/tmp/cc3eDbiW.o: In function `__static_initialization_and_destruction_0(int, int)':
sync_client.cpp:(.text+0xd61): undefined reference to `boost::system::generic_category()'
sync_client.cpp:(.text+0xd6d): undefined reference to `boost::system::generic_category()'
sync_client.cpp:(.text+0xd79): undefined reference to `boost::system::system_category()'
/tmp/cc3eDbiW.o: In function `boost::system::error_code::error_code()':
sync_client.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/cc3eDbiW.o: In function `boost::asio::error::get_system_category()':
sync_client.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::system_category()'
/tmp/cc3eDbiW.o: In function `boost::asio::detail::posix_tss_ptr_create(unsigned int&)':
sync_client.cpp:(.text._ZN5boost4asio6detail20posix_tss_ptr_createERj[boost::asio::detail::posix_tss_ptr_create(unsigned int&)]+0x19): undefined reference to `pthread_key_create'
/tmp/cc3eDbiW.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
sync_client.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/cc3eDbiW.o: In function `boost::asio::detail::posix_thread::join()':
sync_client.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x2b): undefined reference to `pthread_join'
/tmp/cc3eDbiW.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service>::context>::~posix_tss_ptr()':
sync_client.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceEE7contextEED5Ev]+0x15): undefined reference to `pthread_key_delete'
/tmp/cc3eDbiW.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl>::context>::~posix_tss_ptr()':
sync_client.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEE7contextEED5Ev]+0x15): undefined reference to `pthread_key_delete'
collect2: ld returned 1 exit status

什么是正确的编译器命令?

1 个答案:

答案 0 :(得分:0)

g++ sync_client.cpp -lboost_iostreams -lboost_system -o sync_client

然后:

./sync_client

您可能需要在编译命令中添加-lpthread,似乎您的LD_LIBRARY_PATHld.so.conf配置不正确。

注意:无需在命令行中添加-I /usr/include/boost

<强>更新

命令ls -l /usr/lib/libboost_iostreams*应输出如下内容:

... /usr/lib/libboost_iostreams.so -> libboost_iostreams.so.1.55.0*
... /usr/lib/libboost_iostreams.so.1.55.0*

如果是这种情况,请尝试运行sudo ldconfig