我有以下代码
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
int main(int argc,char *argv[]) {
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;
}
但我有以下错误列表
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/Boost'
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/boost
make[2]: Entering directory `/home/david/NetBeansProjects/Boost'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/boost build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()'
/usr/include/boost/system/error_code.hpp:206: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost/system/error_code.hpp:211: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost/system/error_code.hpp:212: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost/system/error_code.hpp:213: undefined reference to `boost::system::get_system_category()'
build/Debug/GNU-Linux-x86/main.o: In function `error_code':
/usr/include/boost/system/error_code.hpp:312: undefined reference to `boost::system::get_system_category()'
build/Debug/GNU-Linux-x86/main.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:218: undefined reference to `boost::system::get_system_category()'
build/Debug/GNU-Linux-x86/main.o: In function `~posix_thread':
/usr/include/boost/asio/detail/posix_thread.hpp:69: undefined reference to `pthread_detach'
build/Debug/GNU-Linux-x86/main.o: In function `boost::asio::detail::posix_thread::join()':
/usr/include/boost/asio/detail/posix_thread.hpp:77: undefined reference to `pthread_join'
build/Debug/GNU-Linux-x86/main.o: In function `~posix_tss_ptr':
/usr/include/boost/asio/detail/posix_tss_ptr.hpp:61: undefined reference to `pthread_key_delete'
build/Debug/GNU-Linux-x86/main.o: In function `posix_tss_ptr':
/usr/include/boost/asio/detail/posix_tss_ptr.hpp:47: undefined reference to `pthread_key_create'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/boost] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/Boost'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/Boost'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 5s)
请求帮助什么是幸福的?
答案 0 :(得分:6)
问题不在您的代码中,问题出在编译器(实际上是链接器)的设置中。您需要链接到Boost.System和pthread库。通常通过将-lboost_system
,-lpthread
传递给Makefile中的链接器选项或最喜欢的IDE来完成。
答案 1 :(得分:2)
您需要链接Boost.System库:-lboost_system 您还需要链接到pthreads lib:-lpthread
答案 2 :(得分:0)
您需要右键单击项目根文件夹并选择属性。
从那里,您需要在左侧窗格中选择“链接器”,并且“库”选项字段就在那里。您需要浏览系统,直到找到libboost_system.a二进制文件。通常在/ usr / local / lib。
如果您无法找到所关注的文件,则可能尚未编译这些库的源代码。查看http://ubuntuforums.org/showthread.php?t=1180792或boost tar包中包含的入门文档。
答案 3 :(得分:0)
以后参考任何有相同问题的人: 在netbeans中右键单击项目 - &gt;属性 - &gt; C ++编译器,在底部有一个“Aditional arguments”选项传入-lboost_system选项。注意:必须定义-lboost_system,否则您将收到错误