【发布时间】:2015-06-09 13:54:27
【问题描述】:
我遇到了与以前类似的错误 C++ Threads, std::system_error - operation not permitted?
我使用完全相同的源代码并使用
进行编译g++ ../src/main.cpp -pthread -std=c++11
没有任何问题。
因为我想在一个更大的项目中使用线程,所以我必须在 CMake 中使用线程。搜索解决方案后,我发现了几个代码,例如:
cmake_minimum_required (VERSION 2.6)
project (Test)
add_definitions("-std=c++11")
find_package (Threads)
add_executable (main src/main.cpp)
target_link_libraries (main ${CMAKE_THREAD_LIBS_INIT})
但对我来说,它总是不起作用:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
我的错误是什么?
CMake 输出看起来很有希望:
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
【问题讨论】:
-
您需要在 cmake 的链接器设置中添加
pthread -
检查
find_package (Threads)做了什么:找到了哪些库? -
直接使用
-pthread选项就可以了。
标签: c++ multithreading cmake pthreads