-lpthread fails in makefile solutions

Question : When writing the project today, I used the C++ multithreading library <thread>, and then added -lpthread to compile as required to compile successfully, but after writing the compiled content into a makefile, there is always an error "right" pthread_create'undefined reference'", I feel very puzzled.
Solution : The reason is that the position of -lpthread in the makefile is incorrect. This is the makefile when I reported the error:
Insert picture description here
I put -lpthread on the line of compiling control.cpp (because only this file uses thread), but in fact it should be This command is moved up to the second line because the link library is used when linking executable files, not when compiling.
Insert picture description here
That's it.

Guess you like

Origin blog.csdn.net/weixin_43867940/article/details/105477928