Project scenario:
Use g++ to compile C++ code with thread library
g++ listing_1.1.cpp -o list_1.1
Problem Description
Error:
/usr/bin/ld: /tmp/ccYH9VAd.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
listing_1.1.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x33): undefined reference to `pthread_create'
Cause Analysis:
C++11 is not added, lpthread is not the default multithreading library under linux
solution:
The g++ command adds parameters:
g++ listing_1.1.cpp -std=c++11 -o list_1.1 -lpthread
compile successfully