15

I get an error when making a project with CMake:

-- Could NOT find Threads (missing:  Threads_FOUND)

The error log shows that CMake tripped up over something truly banal:

/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;}

This really should be no reason for CMake to think Threads doesn't exist. How do I go about fixing this?

Claudiu
  • 224,032
  • 165
  • 485
  • 680

2 Answers2

27

I believe this is CMake bug 15058 which I just reported.

The test that CMake is using to check the include file uses an old-style C function definition. If -Wold-style-definition -Werror is in effect, gcc will barf on this.

I included a patch in the bug report linked above, but for a quick fix, find the file Modules/CheckIncludeFiles.cmake in your CMake installation (possibly in /usr/share/cmake or similar), find the line

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

and change int main() to int main(void).

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 1
    This does get it to find the include file, but it doesn't seem like this is enough. Once I made the change and re-created it, I get [this output](http://pastebin.com/46MvQRaT). If however I have [this](http://pastebin.com/uqGGj8LM) in the `CMakeLists.txt`, then [it works](http://pastebin.com/YE6Ny1yj). It looks like it doesn't add `-pthread` itself? Yet I thought that's what it was supposed to do? – Claudiu Aug 05 '14 at 18:06
  • 1
    @Claudiu: Huh. Have you checked the error log again? Maybe something else is wrong. – Nate Eldredge Aug 05 '14 at 18:15
  • 1
    Yep, another warning treated as an error failure. "Check if compiler accepts -pthread" should be the only one that worked, I think, but it failed, [log here](http://pastebin.com/tcnV5LjW). I fixed it by changing the offending line to `printf("%p CC: %d\n", args, cc);` – Claudiu Aug 05 '14 at 18:55
  • 2
    I am getting this error an a fresh install of Ubuntu 16.04 and cmake 3.5. The CheckIncludeFiles.cmake already contains main(void). Building the same project on my 14.04 with cmake 2.8 that has main() does not give any error – Jana Jun 24 '16 at 23:04
  • @Jana: I bet you it's not this **exact** error... is yours also an error in CheckIncludeFiles.c? – Nate Eldredge Jun 24 '16 at 23:58
  • 1
    CMake Error at /usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:148 (message): Could NOT find Threads (missing: Threads_FOUND) Call Stack (most recent call first): /usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE) /usr/share/cmake-3.5/Modules/FindThreads.cmake:223 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) /usr/share/cmake-3.5/Modules/CMakeFindDependencyMacro.cmake:65 (find_package) /usr/share/cmake-3.5/Modules/FindBoost.cmake:1338 (find_dependency) CMakeLists.txt:461 (find_package) – Jana Jun 27 '16 at 22:44
  • 2
    However, I also have find_library(PTHREAD_LIBRARY) in my project and that finds the lib no problem. I commented out FIND_PACKAGE_HANDLE_STANDARD_ARGS in FindThreads.cmake at line 223 to silent this problem. – Jana Jun 27 '16 at 22:45
  • @Jana: Since it's a different error than addressed by this question, you should probably post a new question. I don't know the solution, and nobody else is going to see your post buried in the comments here. – Nate Eldredge Jun 28 '16 at 13:24
  • Heh, they still haven't fixed that bug - at least on FreeBSD it's still there. So much for "resolved". – Daniel Ziltener Dec 30 '17 at 13:11
  • In Ubuntu 18.04; /usr/share/cmake-3.19/Modules/CheckIncludeFiles.cmake – Andor Kiss Feb 22 '21 at 22:19
2

Issue seems to be still there. I try to compile the lates nominatim. seems i got the same error.

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

Edit: cmake -lpthread .. seems to fix this

YetAnotherDuck
  • 294
  • 4
  • 13