When using future under linux, an error is reported undefined reference to `pthread_create‘

Xeon_CC 2022-11-24 20:57:50 阅读数:594

usingfuturelinuxerrorreported

The error is like this:

FAILED: test_gdb: && /usr/bin/g++ -g CMakeFiles/test_gdb.dir/gdb_main.cpp.o -o test_gdb && :/usr/bin/ld: CMakeFiles/test_gdb.dir/gdb_main.cpp.o: in function `std::thread::thread<std::__future_base::_Async_state_implstd::thread::_Invokerstd::tupleint (*)() , int::_Async_state_impl(std::thread::_Invoker<std::tupleint (*)() >&&)::{lambda()#1}, , void>(std::__future_base::_Async_state_impl >, int>::_Async_state_impl(std::thread::_Invoker >&&)::{lambda()#1}&&)': /usr/include/c++/9/thread:126: undefined reference to `pthread_create'collect2: error: ld returned 1 exit statusninja: build stopped: subcommand failed.

Solution: add -pthread parameter to g++ compiler
Add set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") in CMakeLists.txt

cmake_minimum_required(VERSION 3.23)project(test_gdb)set(CMAKE_CXX_STANDARD 20)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")add_executable(test_gdb gdb_main.cpp)

If you run the compiled executable file directly, you can compile it like this:

g++ -std=c++2a -g -o gdb_main gdb_main.cpp -pthread

After compiling, run the gdb_main file directly:

./gdb_main
copyright:author[Xeon_CC],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/328/202211242057077395.html