Undefined reference to 'pthread _ create'

Source: Internet
Author: User

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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.