Я получаю ошибку "неопределенная ссылка на 'pthread_create'" при запуске make
в программе, которая использует библиотеку pthread.Он работает, когда я создаю его напрямую с помощью g ++:
g++ -std=c++11 -pthread pthread_Mutex.c stopwatch.o -o pthread_Mutex
Но не с CMake.
Я рассмотрел несколько хороших примеров, в том числе здесь: cmake и libpthread
Я пробовал примеры thehouse для 3.1+ и 2.8+ версий CMake.Я использую 3.5.1 в Ubuntu.Вот мой CMakeList.txt.
cmake_minimum_required(VERSION 3.5)
project(pthread_Mutex)
set (CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(stopwatch ${SOURCES})
add_executable(pthread_Mutex ${SOURCES})
# I've tried this one by itself
#target_link_libraries(pthread_Mutex Threads::Threads)
if(THREADS_HAVE_PTHREAD_ARG)
target_compile_options(pthread_Mutex PUBLIC "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(pthread_Mutex "${CMAKE_THREAD_LIBS_INIT}")
endif()
target_link_libraries(pthread_Mutex pthread)
Вот моя структура каталогов:
├── build
├── CMakeLists.txt
├── include
│ └── stopwatch.h
├── src
├── pthread_Mutex.cpp
└── stopwatch.cpp
pthread_Mutex.cpp - моя основная программа.stopwatch.cpp - это библиотека, которая не использует pthreads.
Но я получаю следующие ошибки при запуске cmake VERBOSE = 1:
/usr/bin/cmake -H/home/ben/workspaces/c++/thread_experiments -B/home/ben/workspaces/c++/thread_experiments/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/pthread_Mutex.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make[2]: Nothing to be done for 'CMakeFiles/pthread_Mutex.dir/build'.
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 50%] Built target pthread_Mutex
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/stopwatch.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 66%] Linking CXX executable stopwatch
/usr/bin/cmake -E cmake_link_script CMakeFiles/stopwatch.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o CMakeFiles/stopwatch.dir/src/stopwatch.cpp.o -o stopwatch
CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o: In function `main':
pthread_Mutex.cpp:(.text+0x15a): undefined reference to `pthread_attr_getstacksize'
pthread_Mutex.cpp:(.text+0x19f): undefined reference to `pthread_attr_setstacksize'
pthread_Mutex.cpp:(.text+0x260): undefined reference to `pthread_create'
pthread_Mutex.cpp:(.text+0x29d): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
CMakeFiles/stopwatch.dir/build.make:120: recipe for target 'stopwatch' failed
make[2]: *** [stopwatch] Error 1
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/stopwatch.dir/all' failed
make[1]: *** [CMakeFiles/stopwatch.dir/all] Error 2
make[1]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Поэтому у меня должно быть что-то не так в файле CMakeList.txt, но у меня заканчиваются идеипытаться.
Спасибо