c++中的boost库链接问题



实现基于时间的事件"validate"的问题,如等待方法获取当前时间和1小时后的时间,并休眠1小时,然后调用此验证函数。
但是当我运行这段代码时,它显示错误
boost库版本是:1.71

在main.cpp中,我试图使两个触摸文件和时钟同步,然后实现get_latest_clock_entry,打印时钟文件的最后修改时间。然后wait函数等待1小时,然后运行validate函数。Wait函数应该每小时调用一次validates方法。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;
}

错误:

/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

更新:
编译后使用g++ -o main main.cpp -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_threadpthread

$ g++ boost.cpp -o boost -lboost_thread -lpthread
$ ldd ./boost
linux-vdso.so.1 (0x00007ffc1bd79000)
libboost_thread.so.1.71.0 => /lib/x86_64-linux-gnu/libboost_thread.so.1.71.0 (0x00007fc4b7a51000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc4b7a2e000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc4b784c000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc4b7831000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc4b763f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc4b7aee000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc4b74f0000)
$ ./boost 
Failed to open file
Failed to open file
Can't retrieve last modified time /home/saman/Desktop/clock
Now Time2023-Jan-19 03:43:47
update Time2023-Jan-19 03:43:47
hi

供参考,首先你必须找到丢失的东西,在这种情况下是boost::detail::get_current_thread_data

使用

下面的行
$ find /usr/lib -name 'libboost*' | xargs -i /bin/bash -x -c "nm -C {}" | grep boost::detail::get_current_thread_data

然后查找状态为"T"演讲结束后。上面的库就是你需要包含的。

+ nm -C /usr/lib/x86_64-linux-gnu/libboost_thread.a
0000000000000210 T boost::detail::get_current_thread_data()

您需要与Boost链接。线程库。例如,如果您正在使用clang或gcc,请在编译器命令行中添加-lboost_thread

最新更新