cmake文件
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)
错误消息
`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包含wrapper.h,其中包含pthread.h
我忽略了一些明显的东西,但我不知道是什么。 我尝试了set,add_library和install命令,但从来没有输出比这更少的错误。
编辑:这是固定文件。
cmake_minimum_required(VERSION 3.6)
project(producer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -lpthread")
add_library(wrapper wrapper.h wrapper.c)
add_library(cs537 cs537.h cs537.c)
add_library(request request.h request.c)
add_executable(server server.c)
target_link_libraries(server request wrapper -lpthread)
答案 0 :(得分:1)
这是一个链接器错误,你需要在链接器中使用-pthread,直接:
target_link_libraries(server -lpthread)
或更多跨平台:
set(CMAKE_THREAD_PREFER_PTHREAD ON)
find_package (Threads)
target_link_libraries (server ${CMAKE_THREAD_LIBS_INIT})