簡體   English   中英

CMake 未定義引用 Github Action Ubuntu 映像中的“pthread_create”

[英]CMake undefined reference to `pthread_create` in Github Action Ubuntu image

我在使用Github Action CI的時候發現不管用什么方法鏈接,都沒有辦法鏈接pthread_create

但是這個錯誤只出現在Ubuntu環境下,Windows、macOS都沒有問題

我試過了:

  1. 不工作

    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)
  2. 不工作

    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)

請參閱在 CMake 中設置通用編譯標志的現代方法是什么?

  1. 不工作

沒有鏈接到 Thread::Thread 也沒有鏈接到目標鏈接到的任何庫。

https://github.com/YuzukiTsuru/lessampler/blob/a6bb7e7d7ac30b6b4043d4f717a2d4deb7fb7638/test/CMakeLists.txt#L22

  1. 不工作

標志必須在add_executable之前設置。 這意味着所有add_subdirectories之前。 並且標志具有目錄范圍。 使用targe_compile_options

https://github.com/YuzukiTsuru/lessampler/blob/master/src/CMakeLists.txt

  1. 考慮把它變成一個庫,為什么每個目錄中有這么多和這么多的 CMakeLists.txt。 如果這些工具不是那么獨立並且您永遠不會單獨使用它們,只需將其設為一個庫,其中包含一個與所有庫鏈接的 CMakeLists.txt。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM