2
GCC 4.6.0
Linux
cmake 2.8

I am using cmake to generate my make file. However, in my network_imp.c file I need to do some threading. So I have included the header file #include <pthread.h> and I am using the pthread_create() function

How can I tell cmake to use this header pthread.h and shared library -lpthread?

I thought about using the find_package, but I don't think I am using it correctly. This is my CMakeLists.txt file.

find_package(pthread)

add_library(network SHARED network_imp.c)

The error I get when I try and make is this:

undefined reference to pthread_create

Many thanks for any suggestions,

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • possible duplicate of [cmake and libpthread](http://stackoverflow.com/questions/1620918/cmake-and-libpthread) – mpromonet Jan 26 '15 at 22:01

1 Answers1

3

In general you should use target_link_libraries cmake command to link your executables with other libraries. find_package command is used to set special cmake variables, containing, for example, the actually libraries, to link with.

And for working with pthread you should use find_package(Threads).

And here is the answer to your particular question.

Community
  • 1
  • 1
beduin
  • 7,943
  • 3
  • 27
  • 24
  • 1
    Hello Beduin, I was just wondering how do I know the name I am looking for. I know I want to find pthreads. But how would I know that it is called Threads? The reason I asked as I am using the apache portable runtime API. And I would like to find the apr package? I tried Apr and APR, but cmake make couldn't find it. Many thanks for any further help. – ant2009 May 24 '11 at 15:00