CMake binding error pthread: enable multithreading to use std :: thread: operation not allowed

I have a similar error as before. C ++ Threads, std :: system_error - operation not allowed?

I use the exact same source code and compile with

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

works without problems.

Since I want to use streams in a larger project, I have to use streams 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 this does not work, I always get:

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

What's my mistake?

CMake's 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 
+6
source share
2 answers

EDIT : I'm using gcc 5.4 , the CMake snippet in question just works fine.

I just ran into the same problem. My final CMakeLists.txt , which works, looks like this (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}) 
+15
source

In my case (Linux desktop), just set the flag:

 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) 

but explicitly looking for a library and linking to it also works and may be necessary for some situations with cross-situations with embedded platforms.

0
source

All Articles