knatten / cpp-brain-teasers

This is the source code for my book C++ Brain Teasers
https://pragprog.com/titles/akbrain/c-brain-teasers/
2 stars 0 forks source link

Build error #1

Open paulfloyd opened 4 hours ago

paulfloyd commented 4 hours ago

[ 38%] Linking CXX executable back-from-the-future /home/pafloyd/tools/bin/ld: CMakeFiles/back-from-the-future.dir/back-from-the-future.cpp.o: in function std::thread::_M_thread_deps_never_run()': back-from-the-future.cpp:(.text._ZNSt6thread24_M_thread_deps_never_runEv[_ZNSt6thread24_M_thread_deps_never_runEv]+0x5): undefined reference topthread_create' /home/pafloyd/tools/bin/ld: back-from-the-future.cpp:(.text._ZNSt6thread24_M_thread_deps_never_runEv[_ZNSt6thread24_M_thread_deps_never_runEv]+0xa): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status make[2]: [CMakeFiles/back-from-the-future.dir/build.make:97: back-from-the-future] Error 1 make[1]: [CMakeFiles/Makefile2:711: CMakeFiles/back-from-the-future.dir/all] Error 2 make: *** [Makefile:101: all] Error 2

I'm using a nonstandard location for my GCC so I had to run cmake with

CC=gcc CXX=g++ LDFLAGS="-Wl,-rpath,${HOME}/tools/gcc/lib64 -pthread" cmake ..

In any case "-pthread" seems to be missing for a couple of the puzzles.

knatten commented 3 hours ago

Are you sure that -phtread is needed here? This repo is passing CI out-of-the-box on Linux/gcc, Linux/clang, and Windows/MSVC. It's also building on Mac with AppleClang (not in CI).

paulfloyd commented 2 hours ago

Yes I'm sure. Also on FreeBSD 14.1

[ 38%] Linking CXX executable back-from-the-future
ld: error: undefined symbol: pthread_create
>>> referenced by back-from-the-future.cpp
>>>               CMakeFiles/back-from-the-future.dir/back-from-the-future.cpp.o:(std::__1::__libcpp_thread_create[abi:se180100](pthread**, void* (*)(void*), void*))
c++: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1

With make VERBOSE-1

[ 37%] Linking CXX executable back-from-the-future
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/back-from-the-future.dir/link.txt --verbose=1
/usr/bin/c++ "CMakeFiles/back-from-the-future.dir/back-from-the-future.cpp.o" -o back-from-the-future
ld: error: undefined symbol: pthread_create
>>> referenced by back-from-the-future.cpp
>>>               CMakeFiles/back-from-the-future.dir/back-from-the-future.cpp.o:(std::__1::__libcpp_thread_create[abi:se180100](pthread**, void* (*)(void*), void*))
c++: error: linker command failed with exit code 1 (use -v to see invocation)

Does ninja detect that -pthread is needed? You CI scripts use ninja but the book instructions on page xiv uses the default (Makefiles on Linux and FreeBSD).

knatten commented 2 hours ago

I just added building with make to CI, and that works too. This problem might be specific to FreeBSD? Could you please try this branch and see if it fixes it? It doesn't make a difference on Linux, but hopefully fixes it for you. https://github.com/knatten/cpp-brain-teasers/tree/pthread