Cannot link pthread

Hello, I have been trying to follow a online tutorial for C++ pthread and I cannot seem to get pthread to link with cmake and clang. I have been tried four different solutions that I found on other forums but none seem to work.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>
#include <pthread.h>


#define NUM_THREADS     5

void *PrintHello(void *threadid);

int main ()
{
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
   for( i=0; i < NUM_THREADS; i++ )
   {
      std::cout << "main() : creating thread, " << i << std::endl;
      rc = pthread_create(&threads[i], NULL, 
                          PrintHello, NULL);
      if (rc)
	  {
         std::cout << "Error:unable to create thread," << rc << std::endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   std::cout << "Hello World! Thread ID, " << tid << std::endl;
   pthread_exit(NULL);
}

And my cmake:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cmake_minimum_required(VERSION 2.6)

SET (CMAKE_C_COMPILER             "/usr/bin/clang")
SET (CMAKE_C_FLAGS                "-Wall -std=c++11 -Werror")
SET (CMAKE_C_FLAGS_DEBUG          "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL     "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE        "-O4 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")

SET (CMAKE_CXX_COMPILER             "/usr/bin/clang++")
SET (CMAKE_CXX_FLAGS                "-Wall -std=c++11 -Werror")
SET (CMAKE_CXX_FLAGS_DEBUG          "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL     "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE        "-O4 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")

SET (CMAKE_AR      "/usr/bin/llvm-ar")
SET (CMAKE_LINKER  "/usr/bin/llvm-ld")
SET (CMAKE_NM      "/usr/bin/llvm-nm")
SET (CMAKE_OBJDUMP "/usr/bin/llvm-objdump")
SET (CMAKE_RANLIB  "/usr/bin/llvm-ranlib")

SET (LLVM_ENABLE_THREADS "yes")

project(threading)

add_executable(threading main.cpp)
TARGET_LINK_LIBRARIES(pthread)

Thanks

Sorry here is the error:
1
2
3
4
5
6
7
8
9
CMakeFiles/threading.dir/main.cpp.o: In function `main':
/home/arortell/Development/Projects/C++_Projects/Threading/main.cpp:18: undefined reference to `pthread_create'
x86_64-pc-linux-gnu-clang-3.5.0: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/threading.dir/build.make:88: recipe for target 'threading' failed
make[2]: *** [threading] Error 1
CMakeFiles/Makefile2:63: recipe for target 'CMakeFiles/threading.dir/all' failed
make[1]: *** [CMakeFiles/threading.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2
Last edited on
-pthread (at compilation and linking time)
Oky I got pthread to work with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cmake_minimum_required(VERSION 2.6)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(TARGET_NAME "a.out")

project(${TARGET_NAME} C CXX)
file(GLOB ALL_SRC_FILES "src/*.cpp")

add_definitions("-std=c++11 -Wall -g -pthread")
find_package(Boost 1.55 COMPONENTS filesystem regex REQUIRED)
message(status ": Boost Include: ${Boost_INCLUDE_DIR}")
message(status ": Boost Libraries Dir: ${Boost_LIBRARY_DIRS}")
message(status ": Boost Libraries: ${Boost_LIBRARIES}")

include_directories(${Boost_INCLUDE_DIR})
include_directories(/usr/include/c++/4.9.1)
include_directories(/usr/lib/clang/3.5.0/include)
include_directories(src)
list(APPEND CMAKE_CXX_FLAGS "-pthread")
add_executable(threading main.cpp)
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries(threading ${Boost_LIBRARIES})
project(threading)

But I still cannot get boost to work, here is the new error:
1
2
3
4
5
6
7
8
9
10
Linking CXX executable threading
/usr/bin/x86_64-pc-linux-gnu-ld: CMakeFiles/threading.dir/main.cpp.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib64/libboost_system.so.1.55.0: error adding symbols: DSO missing from command line
x86_64-pc-linux-gnu-clang-3.5.0: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/threading.dir/build.make:90: recipe for target 'threading' failed
make[2]: *** [threading] Error 1
CMakeFiles/Makefile2:63: recipe for target 'CMakeFiles/threading.dir/all' failed
make[1]: *** [CMakeFiles/threading.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2

The code is the same but with:

#include <boost/thread.hpp>


Last edited on
I got it fixed it by changing:
 
find_package(Boost 1.56 COMPONENTS filesystem regex REQUIRED)


to:
[code]
find_package(Boost 1.56 COMPONENTS system filesystem regex REQUIRED)
[\code]
Topic archived. No new replies allowed.