Possible Makefile error on Linux (and fix)
Flix01 opened this issue · comments
Hi. I've cloned the repository and tried compiling it with:
.../.../px/examples$ make
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example1 -lpthread -lpthread px_sched_example1.cpp
/tmp/cce6YjPP.o: In function `std::thread::thread<void (&)(px_sched::Scheduler*, px_sched::Scheduler::Worker*), px_sched::Scheduler*, px_sched::Scheduler::Worker*>(void (&)(px_sched::Scheduler*, px_sched::Scheduler::Worker*), px_sched::Scheduler*&&, px_sched::Scheduler::Worker*&&)':
/usr/include/c++/5/thread:137: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
Makefile:42: recipe for target 'px_sched_example1' failed
make: *** [px_sched_example1] Error 1
Then, based on these links link1 link2 (that seem to contradict each other 😃 ), I thought that -lpthread
must be put after the source files, whereas -pthread
only can be used before the source files.
I've tried the latter and simply replaced -lpthread
with -pthread
in the Makefile.
I don't know if this is the right solution, but it worked:
.../.../px/examples$ make clean
rm -f px_sched_example1 px_sched_example2 px_sched_example3 px_sched_example4 px_sched_example5 px_sched_example6 px_sched_example7
.../...//px/examples$ make
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example1 -pthread px_sched_example1.cpp
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example2 -pthread px_sched_example2.cpp
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example3 -pthread px_sched_example3.cpp
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example4 -pthread px_sched_example4.cpp
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example5 -pthread px_sched_example5.cpp
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example6 -pthread px_sched_example6.cpp
g++ -std=c++11 -pedantic -g -O2 -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wdouble-promotion -Werror -Wextra -Wformat=2 -Winit-self -Wmissing-include-dirs -Wno-unused -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -o px_sched_example7 -pthread px_sched_example7.cpp
Hope it helps other Linux users having the same problems 😄 .
Thanks for reporting it! It worked fine on my latest Debian, which distro are you working on?
Ubuntu 16.04 LTS, but I guess it probably depends on the gcc version:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.10' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
Probably newer versions are more robust.
Hello,
Same error (Linux Intel x86_64 there). In fact, the g++ command line does not work.
Below my little fix :
diff --git a/examples/Makefile b/examples/Makefile
index 7625868..3d97262 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -37,7 +37,7 @@ px_sched_examples = px_sched_example1 px_sched_example2 px_sched_example3 px_sch
all: $(px_sched_examples)
$(px_sched_examples): %: %.cpp ../px_sched.h
- $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) $<
+ $(CXX) $(CXXFLAGS) -o $@ $< $(LDFLAGS)
.PHONY: clean tests
clean:
Hope this helps ;-)
@ebachard Yes, your fix is probably more correct than mine: all the libraries should be placed after the source files in the command line (-pthread
does work before the source files just because it's a compiler option and not a library, but -lpthread
and all the other libraries should be placed after the source files, as far as I can understand).
Thank you, that worked for me too, so fixed!