Nebulous Ryan Nebulous Ryan - 12 days ago 6
C Question

How do I fix this cmake file? c file is ignoring h file it is dependent on

the cmake file

cmake_minimum_required(VERSION 3.6)
project(producer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -pthread")

add_executable(server wrappers.h wrappers.c server.c request.h request.c)


the error message

`CMakeFiles/server.dir/wrappers.c.o: In function Pthread_create':
/home/user/ClionProjects/producer/wrappers.c:570: undefined reference to `pthread_create'
CMakeFiles/server.dir/wrappers.c.o: In function `Pthread_join':
/home/user/ClionProjects/producer/wrappers.c:645: undefined reference to `pthread_join'
CMakeFiles/server.dir/wrappers.c.o: In function `Pthread_cancel':
/home/user/ClionProjects/producer/wrappers.c:653: undefined reference to `pthread_cancel'
collect2: error: ld returned 1 exit status
make[3]: *** [server] Error 1
make[2]: *** [CMakeFiles/server.dir/all] Error 2
make[1]: *** [CMakeFiles/server.dir/rule] Error 2
make: *** [server] Error 2


wrapper.c includes wrapper.h which includes pthread.h

I'm overlooking something obvious, but I don't know what.
I tried set, add_library, and install commands but never got output with fewer errors than this.

Answer

thats a linker error, you need to use -pthread in the linker aswell, either directly:

target_link_libraries(server -lpthread)

or more cross-platform:

set(CMAKE_THREAD_PREFER_PTHREAD ON)
find_package (Threads)
target_link_libraries (server ${CMAKE_THREAD_LIBS_INIT})