undefined reference to `pthread_create'
Under Ubuntu 18.04.6 and gcc 7.5/11/13, I got an error undefined reference to pthread_create which is caused by target_compile_options(PTHASH INTERFACE -pthread) which does not seem to work. Re-adding the original set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") works.
Why is that?
CC. @ByteHamster any ideas?
Did you also notice this @hmusta under gcc-12?
My environment is gcc-12 with Ubuntu 20.04 and this doesn't seem to be an issue, target_compile_options(PTHASH INTERFACE -pthread) works fine
Thanks for confirming @hmusta! Perhaps it is an issue of cmake 3.25.
Did you try a new, fresh build directory and used cmake from the command line? Maybe the old build directory or the IDE caches something. Using CMAKE_CXX_FLAGS is the old way to use cmake and causes problems because it applies the flag even to compilation units that don't use PTHash. (it even breaks the build when some target in the project uses a different compiler that doesn't understand the flag)
Actually yes, I did. I created some new build directories and executed cmake from there. But I could also try to re-clone the entire project from scratch...maybe maybe!
No, still getting the same errors: undefined pthread_create and pthread_join.
Just for some of the targets (build, example, etc) or for all?
For all of them :/
Closing this as it seems a problem related to that specific server machine.
Looks like adding this SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") before the add_executable commands solves the problem...
That's old cmake again, though, which sets stuff on all targets at once. Even completely unrelated targets that do not need pthread. Maybe even targets that are compiled with a compiler that doesn't know what to do with that parameter
As of e1be88af75fc6939723b6ff2030d447e593e0e93, the issue is now solved by using modern CMake tricks as suggested by @ByteHamster . Thank you!