Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake Linking Error pthread: Enable multithreading to use std::thread: Operation not permitted

I have a similar error like another had before C++ Threads, std::system_error - operation not permitted?

I am using exactly the same source code and compiling with

g++ ../src/main.cpp -pthread -std=c++11

works without any problem.

Since I want to use threads in a larger project I have to use threads with CMake. After searching for solutions I found several codes for example:

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})

But for me it's not working I get always:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

What is my mistake?

The CMake output looks promising:

-- 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
like image 936
Julian Avatar asked Jun 09 '15 13:06

Julian


1 Answers

EDIT: I am now using gcc 5.4, the CMake snippet in question just work fine.

I just faced the same problem.My final CMakeLists.txt, which works, is as follows (note - it will work for Windows as well, i.e.- using 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})

EDIT 2: As of CMake 3.2, there is an IMPORTED target you link to instead:

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)

(it might be 3.1.3, but I cannot find the change in the release notes)

like image 88
prehistoricpenguin Avatar answered Sep 17 '22 16:09

prehistoricpenguin