link to pthread library using CMake (in CLion)

clion add library
clion cannot find any cmake profile
undefined reference to pthread_create
cmake link static library
cmake link two libraries
clion without cmake
c executable vs c library clion
cmake include library

I've looked all over and I can't figure out how to get CLion to link the lpthread library. I know that w/ gcc you can just type -lpthread, but I need to do some debugging in CLion.

Here's my current CMakeLists file:

cmake_minimum_required(VERSION 3.3)


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")


set(SOURCE_FILES lab4_v2.c)
add_executable(lab4 ${SOURCE_FILES})

Before CMake 2.8.12:

find_package(Threads REQUIRED)
  set_property(TARGET my_app PROPERTY COMPILE_OPTIONS "-pthread")
  set_property(TARGET my_app PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
  target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}")

If you have CMAKE 2.8.12+:

find_package(Threads REQUIRED)
  target_compile_options(my_app PUBLIC "-pthread")
  target_link_libraries(my_app "${CMAKE_THREAD_LIBES_INIT}")

If you have CMake 3.1.0+

find_package(Threads REQUIRED)
target_link_libraries(my_app Threads::Threads)

If you want to use one of the first two methods with CMake 3.1+, you will need:


Info taken from video by Anastasia Kazakova

CLION add compiler flag(-pthread) · GitHub, Obviously, not added on pthread library, though his library include the header file​, but also Because clion using cmake, simply checked cmake add translation  CLion 140.2310.6 (a cross-plattform C/C++-IDE by JetBrains using CMake as build system) installed boost via brew install boost into /usr/local/Cellar/boost/ Now, my goal is to setup a simple project and include the boost library. I defined just one test.cpp file that looks like this:

you should use target_link_libraries:

target_link_libraries(lab4 pthread)

clion several ways to add pthread, Since CLion relies on CMake build system, you can do this with CMake commands. To add This thread on SO has some useful information and links. 0​. Avatar. 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")

For C:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")

How do I add my libraries to the project? – IDEs Support (IntelliJ , When you create a new CMake project in CLion, a CMakeLists.txt file is automatically Target is an executable or a library to be built using a CMake script. We will use two commands to link our static library to the cmake_testapp target:  This happens when I try to link my application with myhtml when it was built using Cmake. After looking at your CMakeLists.txt, I came to the conclusion that you forgot to link myhtml targets with the pthreads library. Adding target_link_librairies directive after lines 189 and 195 should solve the problem.

Answer as of CLion 2018.2 and the bundled cmake version of 3.12.0

I used other answers in this thread to modify my CMakeLists.txt, and ultimately found I had to add a SECOND line with set() to make this work. My file looks like the following:

cmake_minimum_required(VERSION 3.12)
project(thread_test_project C)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")



Quick CMake Tutorial - Help, But you can try importing non-CMake project into CLion using Import Project functionality. [New Thread 5452.0xc8c] I could not find any way to add an external library to my project other than editing the CMakeList file. I would See instructions and links here: Just out of curiosity - if the compiler is gcc and the taget system is Solaris, it might me necessary to use the compiler option -lm to statically link the math library. The same might hold for other target systems if gcc is used. – Codor Oct 22 '16 at 20:00. yes, i know it.

For C++ use

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread" )

CLion answers frequently asked questions, WSL toolchain enables you to build projects using CMake and compilers from Linux and run/debug on WSL without leaving CLion running on your Windows  If you want to use one of the first two methods with CMake 3.1+, you will need set (THREADS_PREFER_PTHREAD_FLAG ON) there too. improve this answer. edited Jul 10 '17 at 9:18. 2 silver badges. 13 bronze badges. answered Apr 25 '15 at 23:29. 24 silver badges. 32 bronze badges. It seems like e.g. CMake 3.0.2 won't set THREADS_HAVE_PTHREAD_ARG if

Quick Tutorial: Configure CLion on Windows - Help, Hi,After some fiddling with SDL2 I have finally been able to compile an CMakeLists.txt don't work but cmake works fine. Looking for include file pthread​.h - not found Found SDL2: mingw32;D:/apps/SDL2/i686-w64-mingw32/lib/​libSDL2main.a Linking CXX executable simple-timeline-recorder.exe Oh, this was was a pain! I probably lost 2 hours on this. Here is the solution: CMake uses short 'C' applications to test/try things. If the CMakeLists.txt states that C++ is used for the project, without also listing C, then some of those shorts tests incorrectly fail, and cmake then thinks those things aren't found.

Manually add include path for header files. CMakeLists.txt don't work , I am trying to learn Qt5 with CMake in the new CLion C/C++ IDE, I am relatively Maybe you know some other way to add the library for the linking or maybe I've icuuc53.dll libgcc_s_dw2-1.dll libstdc++-6.dll libwinpthread-1.dll Qt5Cored.dll  find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY freeimage) Variable FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY is set to full path to the library FreeImage (if found). No hints to the commands are present, because of missing optional arguments PATHS or HINTS

CMake support in CLion, CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists.txt file is automatically generated under the project root.

  • This is about CMake, and not about makefile or CLion...
  • @somecbusnerd : if my answer helped you, consider accepting it.
  • Note: might also need to specify find_package( Threads REQUIRED ) before that (source: