pthash icon indicating copy to clipboard operation
pthash copied to clipboard

undefined reference to `pthread_create'

Open jermp opened this issue 1 year ago • 8 comments

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?

jermp avatar May 28 '24 08:05 jermp

Did you also notice this @hmusta under gcc-12?

jermp avatar May 28 '24 15:05 jermp

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

hmusta avatar May 28 '24 16:05 hmusta

Thanks for confirming @hmusta! Perhaps it is an issue of cmake 3.25.

jermp avatar May 28 '24 21:05 jermp

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)

ByteHamster avatar May 29 '24 15:05 ByteHamster

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!

jermp avatar May 29 '24 15:05 jermp

No, still getting the same errors: undefined pthread_create and pthread_join.

jermp avatar May 29 '24 15:05 jermp

Just for some of the targets (build, example, etc) or for all?

ByteHamster avatar May 29 '24 15:05 ByteHamster

For all of them :/

jermp avatar May 29 '24 16:05 jermp

Closing this as it seems a problem related to that specific server machine.

jermp avatar Jul 11 '24 07:07 jermp

Looks like adding this SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") before the add_executable commands solves the problem...

jermp avatar Nov 11 '24 12:11 jermp

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

ByteHamster avatar Nov 11 '24 12:11 ByteHamster

As of e1be88af75fc6939723b6ff2030d447e593e0e93, the issue is now solved by using modern CMake tricks as suggested by @ByteHamster . Thank you!

jermp avatar Dec 12 '24 11:12 jermp