【问题标题】:CMake failing to detect pthreads due to warnings由于警告,CMake 未能检测到 pthread
【发布时间】:2014-09-08 22:22:06
【问题描述】:

使用 CMake 制作项目时出现错误:

-- Could NOT find Threads (missing:  Threads_FOUND)

错误日志显示 CMake 被一些真正平庸的东西绊倒了:

/usr/bin/cc   -std=c11 -D_GNU_SOURCE   -Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Werror -Wno-error=extra -Wno-error=deprecated-declarations -Wno-error=sign-compare -Wno-error=strict-aliasing -Wno-error=type-limits -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized -Wlogical-op -Wno-error=maybe-uninitialized -Waggregate-return -Wnested-externs -Wold-style-definition -Wstrict-prototypes -march=native    -o CMakeFiles/cmTryCompileExec2533162744.dir/CheckIncludeFiles.c.o   -c /mnt/shared/fooproj/build/release/CMakeFiles/CMakeTmp/CheckIncludeFiles.c
/mnt/shared/fooproj/build/release/CMakeFiles/CMakeTmp/CheckIncludeFiles.c:5:5: 
     error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 int main(){return 0;}
 ^
/mnt/shared/fooproj/build/release/CMakeFiles/CMakeTmp/CheckIncludeFiles.c: 
     In function ‘main’:
/mnt/shared/fooproj/build/release/CMakeFiles/CMakeTmp/CheckIncludeFiles.c:5:5: 
     error: old-style function definition [-Werror=old-style-definition]
cc1: all warnings being treated as errors
[...]
Source:
/* */
#include <pthread.h>


int main(){return 0;}

这真的不应该是 CMake 认为线程不存在的理由。我该如何解决这个问题?

【问题讨论】:

    标签: gcc compilation include cmake pthreads


    【解决方案1】:

    我相信这是我刚刚报告的CMake bug 15058

    CMake 用于检查包含文件的测试使用旧式 C 函数定义。如果-Wold-style-definition -Werror 有效,gcc 将对此表示反对。

    我在上面链接的错误报告中包含了一个补丁,但为了快速修复,请在您的 CMake 安装中找到文件 Modules/CheckIncludeFiles.cmake(可能在 /usr/share/cmake 或类似文件中),找到该行

      "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(){return 0;}\n")
    

    并将int main() 更改为int main(void)

    【讨论】:

    • 这确实可以找到包含文件,但似乎这还不够。一旦我做出改变并重新创建它,我就会得到this output。但是,如果我在CMakeLists.txt 中有this,那么it works。看起来它本身没有添加-pthread?然而我认为这就是它应该做的?
    • @Claudiu:嗯。您是否再次检查了错误日志?也许还有其他问题。
    • 是的,另一个警告被视为错误失败。我认为“检查编译器是否接受 -pthread”应该是唯一有效的方法,但它失败了,@ 987654325@。我通过将违规行更改为 printf("%p CC: %d\n", args, cc); 来修复它
    • 我在全新安装 Ubuntu 16.04 和 cmake 3.5 时收到此错误。 CheckIncludeFiles.cmake 已经包含 main(void)。在我的 14.04 上使用具有 main() 的 cmake 2.8 构建相同的项目不会给出任何错误
    • 但是,我的项目中也有 find_library(PTHREAD_LIBRARY) 并且发现 lib 没有问题。我在第 223 行的 FindThreads.cmake 中注释掉了 FIND_PACKAGE_HANDLE_STANDARD_ARGS 以解决这个问题。
    【解决方案2】:

    问题似乎仍然存在。 我尝试编译lates nominatim。好像我遇到了同样的错误。

    Determining if the pthread_create exist failed with the following output:
    Change Dir: /home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp
    
    Run Build Command:"/usr/bin/make" "cmTC_b87d8/fast"
    /usr/bin/make -f CMakeFiles/cmTC_b87d8.dir/build.make CMakeFiles/cmTC_b87d8.dir/build
    make[1]: Entering directory '/home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_b87d8.dir/CheckSymbolExists.c.o
    /usr/bin/cc    -o CMakeFiles/cmTC_b87d8.dir/CheckSymbolExists.c.o   -c /home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
    Linking C executable cmTC_b87d8
    /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_b87d8.dir/link.txt --verbose=1
    /usr/bin/cc      -rdynamic CMakeFiles/cmTC_b87d8.dir/CheckSymbolExists.c.o  -o cmTC_b87d8 
    CMakeFiles/cmTC_b87d8.dir/CheckSymbolExists.c.o: In function `main':
    CheckSymbolExists.c:(.text+0x1b): undefined reference to `pthread_create'
    collect2: error: ld returned 1 exit status
    CMakeFiles/cmTC_b87d8.dir/build.make:97: recipe for target 'cmTC_b87d8' failed
    make[1]: *** [cmTC_b87d8] Error 1
    make[1]: Leaving directory '/home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp'
    Makefile:126: recipe for target 'cmTC_b87d8/fast' failed
    make: *** [cmTC_b87d8/fast] Error 2
    
    File /home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
    /* */
    #include <pthread.h>
    
    int main(int argc, char** argv)
    {
      (void)argv;
    #ifndef pthread_create
      return ((int*)(&pthread_create))[argc];
    #else
      (void)argc;
      return 0;
    #endif
    }
    
    Determining if the function pthread_create exists in the pthreads failed with the following output:
    Change Dir: /home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp
    
    Run Build Command:"/usr/bin/make" "cmTC_eb9e3/fast"
    /usr/bin/make -f CMakeFiles/cmTC_eb9e3.dir/build.make CMakeFiles/cmTC_eb9e3.dir/build
    make[1]: Entering directory '/home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_eb9e3.dir/CheckFunctionExists.c.o
    /usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_eb9e3.dir/CheckFunctionExists.c.o   -c /usr/share/cmake-3.9/Modules/CheckFunctionExists.c
    Linking C executable cmTC_eb9e3
    /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_eb9e3.dir/link.txt --verbose=1
    /usr/bin/cc  -DCHECK_FUNCTION_EXISTS=pthread_create    -rdynamic CMakeFiles/cmTC_eb9e3.dir/CheckFunctionExists.c.o  -o cmTC_eb9e3 -lpthreads 
    /usr/bin/ld: cannot find -lpthreads
    collect2: error: ld returned 1 exit status
    CMakeFiles/cmTC_eb9e3.dir/build.make:97: recipe for target 'cmTC_eb9e3' failed
    make[1]: *** [cmTC_eb9e3] Error 1
    make[1]: Leaving directory '/home/mikelaptop/Desktop/nominatim/290102/Nominatim/build/CMakeFiles/CMakeTmp'
    Makefile:126: recipe for target 'cmTC_eb9e3/fast' failed
    make: *** [cmTC_eb9e3/fast] Error 2
    

    编辑:cmake -lpthread .. 似乎解决了这个问题

    【讨论】:

    • 在 Ubuntu 18.04.5 LTS(2021 年 2 月)上也遇到了这个错误(我相信);所以,仍然没有修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2020-05-06
    • 1970-01-01
    相关资源
    最近更新 更多