C++中Boost库链接错误:实现定时validate事件遇未定义引用
问题描述
我要实现一个基于时间的validate事件,需求如下:
wait函数获取当前时间与1小时后的时间,等待1小时后调用validate函数- 创建
clock和synchronized文件,实现get_latest_clock_entry函数获取clock文件的最后修改时间
使用Boost 1.71版本编译代码时出现链接错误,添加-lpthread编译选项后仍存在未定义引用问题。
代码文件 main.cpp
#include <iostream> #include <fstream> #include <time.h> #include <sys/stat.h> #include <boost/date_time.hpp> #include <boost/thread/thread.hpp> using namespace std; constexpr char clock_file[] = "/home/saman/Desktop/clock"; constexpr char sync_file[] = "/home/saman/Desktop/synchronized"; class TimeEvent { public: void receive_time_event(timespec& time) { // Create clock file ofstream clockFile(clock_file); if (!clockFile.is_open()) { cout << "Failed to open file" << endl; } clockFile.close(); // Create synchronized file ofstream synchronizedFile(sync_file); if (!synchronizedFile.is_open()) { cout << "Failed to open file" << endl; } synchronizedFile.close(); } timespec get_latest_clock_entry(){ string clockFilePath = clock_file; struct stat fileStat; if(stat(clockFilePath.c_str(), &fileStat) == 0){ time_t mtime = fileStat.st_mtime; timespec time; time.tv_sec = mtime; time.tv_nsec = 0; char timeString[50]; strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", gmtime(&time.tv_sec)); cout << "Last modified time "<< clockFilePath << " : " << timeString << endl; return time; } else{ cout << "Can't retrieve last modified time " << clockFilePath << endl; return {0,0}; } } void wait(){ boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time(); cout << "Now Time" << timeLocal << endl; boost::posix_time::ptime counter = timeLocal + boost::posix_time::hours(1); cout << "update Time" << timeLocal << endl; boost::this_thread::sleep(counter); } void validate(){ cout << "hi" << endl; } }; int main() { TimeEvent timeEvent; timespec time; timespec_get(&time, TIME_UTC); timeEvent.receive_time_event(time); auto lastModifiedTime = timeEvent.get_latest_clock_entry(); while(1){ timeEvent.wait(); timeEvent.validate(); } return 0; }
编译错误信息
初始编译错误(未加-lpthread)
/usr/bin/ld: /tmp/ccJEJpvk.o: in function `boost::condition_variable::condition_variable()': 2.cpp:(.text._ZN5boost18condition_variableC2Ev[_ZN5boost18condition_variableC5Ev]+0xc5): undefined reference to `pthread_condattr_setclock' /usr/bin/ld: /tmp/ccJEJpvk.o: in function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)': 2.cpp:(.text._ZN5boost6detail20interruption_checkerC2EP15pthread_mutex_tP14pthread_cond_t[_ZN5boost6detail20interruption_checkerC5EP15pthread_mutex_tP14pthread_cond_t]+0x29): undefined reference to `boost::detail::get_current_thread_data()' /usr/bin/ld: /tmp/ccJEJpvk.o: in function `boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::mono_platform_timepoint const&)': 2.cpp:(.text._ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE[_ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE]+0x135): undefined reference to `boost::this_thread::interruption_point()' collect2: error: ld returned 1 exit status
添加-lpthread后仍存在的错误
/usr/bin/ld: /tmp/cccOLyt1.o: in function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)': 2.cpp:(.text._ZN5boost6detail20interruption_checkerC2EP15pthread_mutex_tP14pthread_cond_t[_ZN5boost6detail20interruption_checkerC5EP15pthread_mutex_tP14pthread_cond_t]+0x29): undefined reference to `boost::detail::get_current_thread_data()' /usr/bin/ld: /tmp/cccOLyt1.o: in function `boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::mono_platform_timepoint const&)': 2.cpp:(.text._ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE[_ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE]+0x135): undefined reference to `boost::this_thread::interruption_point()' collect2: error: ld returned 1 exit status
解决方法
这些未定义引用问题是因为编译时未正确链接Boost线程库,Boost 1.71的线程模块需要显式链接对应库文件,具体步骤如下:
确认Boost线程库已安装:
- 若使用包管理器,确保
libboost-thread-dev(Debian/Ubuntu系)或对应系统的Boost线程开发包已安装; - 若为源码编译的Boost,确认线程库已编译完成且位于系统可识别的链接路径中。
- 若使用包管理器,确保
修改编译命令:
编译时需添加-lboost_thread选项,同时因为代码用到了Boost日期时间模块,还要链接-lboost_date_time,完整编译命令示例:g++ main.cpp -o time_event -lboost_thread -lpthread -lboost_date_time注:库的顺序需注意,Boost线程库需放在系统线程库
-lpthread前后均可,推荐放在前面。自定义Boost安装路径的额外处理:
若Boost未安装在系统默认路径,需用-L参数指定库所在目录,示例:g++ main.cpp -o time_event -L/path/to/boost/lib -lboost_thread -lpthread -lboost_date_time
另外,代码中wait函数存在一处小问题:输出的等待目标时间错误,可修正为:
cout << "Wait until: " << counter << endl;
内容的提问来源于stack exchange,提问作者Rabia Hussain
相关产品推荐
相关产品推荐

