【问题标题】:CMake Linking Error pthread: Enable multithreading to use std::thread: Operation not permittedCMake链接错误pthread:启用多线程以使用std :: thread:不允许操作
【发布时间】: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

【问题讨论】:

标签: c++ multithreading cmake pthreads


【解决方案1】:

编辑:我现在使用gcc 5.4,有问题的CMake sn-p 工作正常。

我刚刚遇到了同样的问题。我的最终CMakeLists.txt 如下(注意 - 它也适用于 Windows,即使用 Visual Studio):

cmake_minimum_required (VERSION 2.6)
project (Test)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")

find_package (Threads)
add_executable (main src/main.cpp)
target_link_libraries (main ${CMAKE_THREAD_LIBS_INIT})

编辑 2:从 CMake 3.2 开始,您可以链接到 IMPORTED 目标:

cmake_minimum_required (VERSION 3.2)
project (Test)
SET(CMAKE_CXX_STANDARD 11)

find_package (Threads)
add_executable (main src/main.cpp)
target_link_libraries (main Threads::Threads)

(可能是 3.1.3,但我在发行说明中找不到更改)

【讨论】:

  • +1 有同样的问题,在我的树莓派交叉编译时,在 gnu c++ 中启用多线程的所有“标准”方法都不起作用。然而,这个 sn-p 解决了这些问题。谢谢
  • @BirgerSkogengPedersen 很高兴听到这个消息
  • 请注意,这个 sn-p 以两种不同的方式链接同一个库。 -pthread 单独工作,或 find_package + target_link_libraries 调用。只需要这两种方法中的一种(我会说后者是首选)。
  • @ChuckClaunch 也许对于较低的 gcc 版本(例如 4.8),我们应该以某种方式使用 cmake,就像我的 sn-p 显示的那样。几秒钟前我在 gcc 5.4 上试过,-pthreadfind_package + target_link_libraries 运行顺利。
  • @ChuckClaunch 我会编辑答案添加一些补充,谢谢!
【解决方案2】:

在我的情况下(Linux 桌面),设置标志就足够了:

cmake_minimum_required (VERSION 2.6)
PROJECT (threads_tests)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")
ADD_EXECUTABLE(threads_1 threads_1.cpp)

但显式查找库并与之链接也可以,并且可能对于某些嵌入式平台的交叉编译情况是必要的。

【讨论】:

    猜你喜欢
    • 2013-11-06
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 2022-12-18
    相关资源
    最近更新 更多