Because tomorrow we want to learnLinuxMulti-threading.
Compile, erase, and report the thread functions after reading references.Undefined reference to 'pthread _ create'Error
I also introduced the required header files.
# Include <stdio. h>
# Include <pthread. h>
# Include <stdlib. h>
Void printfids (const char * s)
{
Pid_t PID;
Pthread_t tid;
PID = getpid ();
Tid = pthread_self ();
Printf ("% s PID % u TID % u (0x % x) \ n", S, (unsigned INT) PID,
(Unsigned INT) tid, (unsigned INT) PID );
}
Void * thfun (void * Arg)
{
Printfids ("New thread :");
Return (void *) 0 );
}
Int main (INT argc, char * argv [])
{
Int ret;
Pthread_t pthid;
Ret = pthread_create (& pthid, null, thfun, null );
// If ATTR is null, the default attributes shall be used.
//Second ParameterIfYesNullTo create a process with the default attribute.
If (Ret! = 0)
Perror ("can't creat thread ");
Printfids ("main thread :");
Sleep (1 );
Exit (0 );
}
WorkGccCompilation result reportUndefined reference to 'pthread _ create'Error
Look left and right. There's no error. After half an hour, it's just a hundred.
Cause:
PthreadLibrary is notLinux
The default library of the system. The static library is required for connection.Libpthread..Pthread_create ()To create a thread, You need to link the library.
Add-LpthreadParameters
GCC thread. C-o thread-lpthread
Thread. cDo not forget to add the header file for the source file you wrote.# Include <pthread. h>