报错是这样的:
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_impl<std::thread::_Invoker<std::tuple<int (*)()> >, int>::_Async_state_impl(std::thread::_Invoker<std::tuple<int (*)()> >&&)::{lambda()#1}, , void>(std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<int (*)()> >, int>::_Async_state_impl(std::thread::_Invoker<std::tuple<int (*)()> >&&)::{lambda()#1}&&)':
/usr/include/c++/9/thread:126: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
解决方法:g++编译器添加-pthread参数
在CMakeLists.txt中加上 set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -pthread”)
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)
如果是直接运行编译好的可执行文件,可以这么编译:
g++ -std=c++2a -g -o gdb_main gdb_main.cpp -pthread
编译好之后,直接运行gdb_main文件:
./gdb_main