Notes on advanced programming in UNIX

Source: Internet
Author: User
Tags mathematical functions
loopos@webols:~/work/apue$ gcc -o fig11.2 fig11.2.c/tmp/ccXTzASv.o: In function `main':fig11.2.c:(.text+0x2fe): undefined reference to `pthread_create'collect2: ld returned 1 exit status

This error is similar to the prompt that many mathematical functions cannot find when using the <math. h> header file. At that time, the "-lm" option was added during compilation.

The cause of this problem is:

 The pthread library is not the default library in Linux. You must use the static library libpthread for connection. a, so when using pthread_create () to create a thread and other functions related to thread operations, you need to link the library.


Solution:

Add the-lpthread option during compilation.

loopos@webols:~/work/apue$ gcc -o fig11.2 fig11.2.c -lpthread
 

 How to handle errors in source code compilation of apue2 in UNIX environment in Linux

Edit make. Defines. Linux

Modify wkdir =/home/var/apue.2e to your apue.2e directory. For example, if the source code of my apue is decompressed to/usr/local, I will change it:

Wkdir =/usr/local/apue.2e

Then go to the apue.2e/STD directory and edit Linux. mk. Change all nawk to awk.

Finally, return to the directory apue.2e and execute the make command.

The following is the error message and solution when compiling the source code (assuming your working directory is the same as mine, it is/usr/local/apue.2e)

Error 1:

Myls. C: 1: 19: apue. h: no such file or directory
Myls. C: In function 'main ':
Myls. C: 13: Error: 'null' undeclared (first use in this function)
Myls. C: 13: Error: (each undeclared identifier is reported only once
Myls. C: 13: Error: for each function it appears in .)

Solution:

Copy apue. H to the default system header file directory.

$ CP/usr/local/apue.2e/include/apue. h/usr/include

Error 2:

/Tmp/ccbbopm0.o (. Text + 0x2b): In function 'main ':
: Undefined reference to 'err _ quit'
/Tmp/ccbbopm0.o (. Text + 0x5f): In function 'main ':
: Undefined reference to 'err _ sys'
Collect2: LD returned 1 exit status

Solution:

Err_quit and err_sys are error handling functions defined by the author. You need to define the header file separately.

Create a file named myerr. h under/usr/include.

Copy the following content to myerr. H (in fact, this header file is in appendix B of the original book)

Quote:
# Include "apue. H"
# Include/* for definition of errno */
# Include/* Iso c variable aruments */

Static void err_doit (INT, Int, const char *, va_list );

/*
* Nonfatal error related to a system call.
* Print a message and return.
*/
Void
Err_ret (const char * FMT ,...)
{
Va_list AP;

Va_start (AP, FMT );
Err_doit (1, errno, FMT, AP );
Va_end (AP );
}

/*
* Fatal error related to a system call.
* Print a message and terminate.
*/
Void
Err_sys (const char * FMT ,...)
{
Va_list AP;

Va_start (AP, FMT );
Err_doit (1, errno, FMT, AP );
Va_end (AP );
Exit (1 );
}

/*
* Fatal error unrelated to a system call.
* Error Code passed as explict parameter.
* Print a message and terminate.
*/
Void
Err_exit (INT error, const char * FMT ,...)
{
Va_list AP;

Va_start (AP, FMT );
Err_doit (1, error, FMT, AP );
Va_end (AP );
Exit (1 );
}

/*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
Void
Err_dump (const char * FMT ,...)
{
Va_list AP;

Va_start (AP, FMT );
Err_doit (1, errno, FMT, AP );
Va_end (AP );
Abort ();/* dump core and terminate */
Exit (1);/* shouldn't get here */
}

/*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
Void
Err_msg (const char * FMT ,...)
{
Va_list AP;

Va_start (AP, FMT );
Err_doit (0, 0, FMT, AP );
Va_end (AP );
}

/*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
Void
Err_quit (const char * FMT ,...)
{
Va_list AP;

Va_start (AP, FMT );
Err_doit (0, 0, FMT, AP );
Va_end (AP );
Exit (1 );
}

/*
* Print a message and return to caller.
* Caller specifies "errnoflag ".
*/
Static void
Err_doit (INT errnoflag, int error, const char * FMT, va_list AP)
{
Char Buf [maxline];
Vsnprintf (BUF, maxline, FMT, AP );
If (errnoflag)
Snprintf (BUF + strlen (BUF), maxline-strlen (BUF), ": % s ",
Strerror (error ));
Strcat (BUF ,"");
Fflush (stdout);/* In case stdout and stderr are the same */
Fputs (BUF, stderr );
Fflush (null);/* flushes all stdio output streams */
}



Then add the following in the source code of the program that you need to use these error handler functions.

# Include

That's all.

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.