简体   繁体   English

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

[英]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:我试过了:

  1. 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)
  2. 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 中设置通用编译标志的现代方法是什么?

  1. 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

  1. 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

  1. Consider just making it one library, why so many, and so many CMakeLists.txt in every directory.考虑把它变成一个库,为什么每个目录中有这么多和这么多的 CMakeLists.txt。 If the tools are not so separate and you are never going to use them separately, just make it one library with one CMakeLists.txt that links with all the libraries.如果这些工具不是那么独立并且您永远不会单独使用它们,只需将其设为一个库,其中包含一个与所有库链接的 CMakeLists.txt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM