However when i type in g++ sample.cpp -o sample -lpthread it works normally. I would do that however my professor is very strict on the way i compile.
Asked
Active
Viewed 34 times
0
-
I think this solves your problem. Note that it is not always necessarily true to have linker before/after your source file as it varies from projects to projects. You should clarify with your prof. What happens is your sample.cpp depends on -pthread and you should link it after your cpp file, not before. https://stackoverflow.com/a/409402/8532468 – Iberico Nov 25 '21 at 10:24
-
so i was correct on using g++ sample.cpp -o sample -lpthread? My professor is very strict so what should i do? Thanks! – Jack Nguyen Nov 25 '21 at 10:33
-
Think you might have misunderstood your professor, or he is wrong. You should clarify with him. The standard for GNU compilers is to link libraries after the source file that depends on those libraries. This is a very good and comprehensive guide on linking, applies to gcc, g++ compilers https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking – Iberico Nov 25 '21 at 11:04
-
Maybe, it would be better to compile and link in two separate steps. – Daniel Langr Nov 25 '21 at 11:04