[英]CMake undefined reference to `pthread_create` in Github Action Ubuntu image
我在使用Github Action CI的時候發現不管用什么方法鏈接,都沒有辦法鏈接pthread_create
但是這個錯誤只出現在Ubuntu環境下,Windows、macOS都沒有問題
我試過了:
不工作
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)
不工作
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
您可以在此處查看編譯的日志:
https://github.com/YuzukiTsuru/lessampler/runs/6640320037?check_suite_focus=true
如果你仔細閱讀構建日志
/usr/bin/ld: CMakeFiles/GenerateAudioModelTest.dir/__/src/GenerateAudioModel.cpp.o: in function `GenerateAudioModel::GenerateModelFromFile()':
GenerateAudioModel.cpp:(.text+0x27aa): undefined reference to `pthread_create'
您注意到在鏈接位於目錄test
和 CMakeLists.txt 中的目標GenerateAudioModelTest
時發生了錯誤,那里沒有您顯示的編譯器標志。 只需在test/CMakeLists.txt
添加-pthread
即可。
這是一個壞主意。
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
利用
target_compile_options(GenerateAudioModelTest PRIVATE -pthread)
- 不工作
您沒有鏈接到 Thread::Thread 也沒有鏈接到目標鏈接到的任何庫。
- 不工作
標志必須在add_executable
之前設置。 這意味着在所有add_subdirectories
之前。 並且標志具有目錄范圍。 使用targe_compile_options
。
https://github.com/YuzukiTsuru/lessampler/blob/master/src/CMakeLists.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.