[英]CMake undefined reference to `pthread_create` in Github Action Ubuntu image
When I was using Github Action CI, I found that no matter what method I used to link, there was no way to link pthread_create我在使用Github Action CI的时候发现不管用什么方法链接,都没有办法链接pthread_create
But this error only appears in the Ubuntu environment, Windows, macOS are no problem但是这个错误只出现在Ubuntu环境下,Windows、macOS都没有问题
I tried:我试过了:
Not Working不工作
set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) add_executable(xxx xxx.c) target_link_libraries(xxx PRIVATE Threads::Threads)
Not Working不工作
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
You can view the compiled log here:您可以在此处查看编译的日志:
https://github.com/YuzukiTsuru/lessampler/runs/6640320037?check_suite_focus=true https://github.com/YuzukiTsuru/lessampler/runs/6640320037?check_suite_focus=true
If you read the build log carefully如果你仔细阅读构建日志
/usr/bin/ld: CMakeFiles/GenerateAudioModelTest.dir/__/src/GenerateAudioModel.cpp.o: in function `GenerateAudioModel::GenerateModelFromFile()':
GenerateAudioModel.cpp:(.text+0x27aa): undefined reference to `pthread_create'
You notice the error has happened while linking the target GenerateAudioModelTest
that is located in the directory test
and CMakeLists.txt there does not have the compiler flags you shown.您注意到在链接位于目录test
和 CMakeLists.txt 中的目标GenerateAudioModelTest
时发生了错误,那里没有您显示的编译器标志。 Just add -pthread
in test/CMakeLists.txt
.只需在test/CMakeLists.txt
添加-pthread
即可。
This is a bad idea.这是一个坏主意。
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
Use利用
target_compile_options(GenerateAudioModelTest PRIVATE -pthread)
See What is the modern method for setting general compile flags in CMake?请参阅在 CMake 中设置通用编译标志的现代方法是什么?
- Not Working不工作
You did not link with Thread::Thread nor any library that the target links to.您没有链接到 Thread::Thread 也没有链接到目标链接到的任何库。
https://github.com/YuzukiTsuru/lessampler/blob/a6bb7e7d7ac30b6b4043d4f717a2d4deb7fb7638/test/CMakeLists.txt#L22 https://github.com/YuzukiTsuru/lessampler/blob/a6bb7e7d7ac30b6b4043d4f717a2d4deb7fb7638/test/CMakeLists.txt#L22
- Not Working不工作
Flags have to be set before add_executable
.标志必须在add_executable
之前设置。 Which means before all the add_subdirectories
.这意味着在所有add_subdirectories
之前。 And flags have directory scope.并且标志具有目录范围。 Use targe_compile_options
.使用targe_compile_options
。
https://github.com/YuzukiTsuru/lessampler/blob/master/src/CMakeLists.txt https://github.com/YuzukiTsuru/lessampler/blob/master/src/CMakeLists.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.