problem with implementing the time based event "validate", like wait methode takes the current time and time after 1 hour and sleep for 1 hour then calling this validate function.
but when i run this code it shows error
boost library version is: 1.71
In main.cpp i am trying to make the two touch file and clock and synchronize, then implement the get_latest_clock_entry which print the last modified time of the clock file. and then wait function waits for 1 hour and run the validate function. wait function should invokes the validates methode every hour. 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;
}
Error:
/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
Update:
after compiling with 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
CodePudding user response:
You need to link against both boost_thread
and pthread
.
$ 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
FYI first you have to find what is missing, in this case boost::detail::get_current_thread_data
.
Use the line below
$ find /usr/lib -name 'libboost*' | xargs -i /bin/bash -x -c "nm -C {}" | grep boost::detail::get_current_thread_data
Then look for the line that has the status "T" after the address. The library above it is the one that you need to include.
nm -C /usr/lib/x86_64-linux-gnu/libboost_thread.a
0000000000000210 T boost::detail::get_current_thread_data()
CodePudding user response:
You need to link with Boost.Thread library. For example, if you're using clang or gcc, add -lboost_thread
to your compiler command line.