I am super new to C++ and just started dabbling into Boost C++ library
I am currently on a MacOS Catalina and using CLion as my c++ IDE.
I installed boost using Homebrew brew install boost
I was following some tutorials from: http://antonym.org/2009/05/threading-with-boost-part-i-creating-threads.html learning about boost::thread
Here is the code:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
here is my CMakeLists.txt file
cmake_minimum_required(VERSION 3.17)
project(cpp_boost)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost 1.73.0 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
And finally, the build error that I get when I run the code.
====================[ Build | cpp_boost | Debug ]===============================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/parvpatel/Desktop/development/dev-projects/lang-tutorials/cpp-tutorials/cpp-boost/cmake-build-debug --target cpp_boost -- -j 12
[ 50%] Building CXX object CMakeFiles/cpp_boost.dir/main.cpp.o
[100%] Linking CXX executable cpp_boost
Undefined symbols for architecture x86_64:
"boost::this_thread::interruption_point()", referenced from:
boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.cpp.o
boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::real_platform_timepoint const&) in main.cpp.o
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<void (*)()>::~thread_data() in main.cpp.o
"boost::detail::get_current_thread_data()", referenced from:
boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*, _opaque_pthread_cond_t*) in main.cpp.o
"boost::thread::join_noexcept()", referenced from:
boost::thread::join() in main.cpp.o
"boost::thread::native_handle()", referenced from:
boost::thread::get_id() const in main.cpp.o
"boost::thread::start_thread_noexcept()", referenced from:
boost::thread::start_thread() in main.cpp.o
"boost::thread::detach()", referenced from:
boost::thread::~thread() in main.cpp.o
"typeinfo for boost::detail::thread_data_base", referenced from:
typeinfo for boost::detail::thread_data<void (*)()> in main.cpp.o
"vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base() in main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [cpp_boost] Error 1
make[2]: *** [CMakeFiles/cpp_boost.dir/all] Error 2
make[1]: *** [CMakeFiles/cpp_boost.dir/rule] Error 2
make: *** [cpp_boost] Error 2
I tried to look through the internet, but I could not find anything specific. I found its useful to target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES}), but after adding that to the CMakeLists.txt file, the error still persisted.
Any help will be appreciated.
Thank you :)
Thank you to Some programmer dude
It was just adding
threadto find_package that made it workHere is the updated
CMakeLists.txt