【发布时间】:2019-11-20 08:03:51
【问题描述】:
第一次运行命令时出现以下错误
cmake .
但是如果我再次运行命令,编译成功。也就是说,我必须运行cmake 两次 才能编译项目。为什么会这样?我该如何解决?
错误信息:
CMakeFiles/exec.dir/Timer.cpp.o: In function `std::thread::thread<Timer::Timer()::{lambda()#1}>(Timer::Timer()::{lambda()#1}&&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/exe.dir/build.make:146: recipe for target 'exe' failed
make[2]: *** [exe] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/exe.dir/all' failed
make[1]: *** [CMakeFiles/exe.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -g -std=c++14 -std=c++11 -std=c++17 ")
project(myproject)
set(SOURCE_FILES main.cpp Timer.cpp TimerEntity.cpp)
add_executable(exe ${SOURCE_FILES})
【问题讨论】:
-
你确定 -pthread 应该在编译标志中吗?缺少引用是链接时错误,而外部库是编译成目标文件时的点。
-
相关:stackoverflow.com/questions/1620918/cmake-and-libpthread。该问题描述了在 CMake 中使用 pthreads。
标签: c++ multithreading makefile cmake pthreads