Undefined reference to pthread CLion
Asked Answered
W

5

9

I'm trying to run this simple thread c++ program in CLion

#include <iostream>
#include <thread>

using namespace std;


//Start of the thread t1
 void hello() {
     cout << "Hello,concurrent world!" << endl; }



 int main() {
     thread t1(hello);     // spawn new thread that calls hello()

     cout << "Concurrency has started!" << endl;
     t1.join();            

     cout << "Concurrency completed!";

     return 0;
   }

My problem is that there's an error of undefined reference to pthread, and I don't undestand what I'm doing wrong... please notice that I'm doing this on CLion.

Wreckfish answered 9/8, 2017 at 22:50 Comment(3)
You need to link with -lpthread.Baby
@Sam ... or even better with -pthread.Hamlett
It's much better to use cross-platform style, listed here: https://mcmap.net/q/108997/-cmake-and-libpthreadPetry
T
25

In CLion, to compile with flag -pthread you should add the following line to CMakeLists.txt (I've tested and it works):

SET(CMAKE_CXX_FLAGS -pthread)
Tinctorial answered 26/12, 2017 at 17:9 Comment(3)
It worked for me but I also had to add find_package(Threads).Cirsoid
@PaulLaffitte It worked for me without having to add find_package(Threads)Beep
I downvoted not because this is a bad answer, it was ok at the time of writing. However, best practice is now to use find_package(…) and link against Threads::Threads as in https://mcmap.net/q/1121533/-undefined-reference-to-pthread-clionUteutensil
D
5

In CMake first find the package:

find_package(Threads REQUIRED)

Then link against it:

target_link_libraries(${PROJECT_NAME} Threads::Threads)

Now the build will succeed the linking step.

Dispatch answered 22/9, 2019 at 19:14 Comment(1)
Best "modern" cmake answer, thx!Pheon
P
0

You have to compile with flag -lpthread or -pthread (its usually advised to usepthread). If you're using CLion then you will need to edit CMakeLists.txt to ensure that your code is compiled with this flag by setting compiler flags with a command like:

SET( CMAKE_CXX_FLAGS  "<other compiler flags> -pthread")

You can learn more about these options on this post.

Pancreatin answered 9/8, 2017 at 22:52 Comment(3)
with -pthread flag? First I hear about that. Where you found that information?Guddle
I learned about it here #23251363Pancreatin
As the answer in the linked question states, you should use -pthread, not -lpthread.Supranational
B
0

Make sure you are linking to pthread in the end of your CMakeLists.txt

target_link_libraries(${PROJECT_NAME} pthread)
Bowling answered 9/8, 2017 at 23:29 Comment(1)
That is not enough. You also have to set the compiler flag -pthreadSupranational
C
0

As per CLion 2021.3.3, no need to specify any additional settings:

Simply be sure to start from C and not C++ and select C 99.

--

cmake_minimum_required(VERSION 3.21)
project(threads01 C)

set(CMAKE_C_STANDARD 99)

add_executable(threads01 main.c)
Cathycathyleen answered 13/3, 2022 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.