《Unix环境高级编程》学习笔记

来源:互联网 发布:opera连不上网络 编辑:程序博客网 时间:2024/04/30 05:00
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


这个错误跟以前遇到的那个使用<math.h>头文件时,很多数学函数的找不到的提示类似,那时候是在编译的时候添加“-lm”选项。
而此处产生这个问题原因是:
    pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,和其他一些与线程操作相关的函数时,需要链接该库。

解决方法:
    在编译中要加 -lpthread选项

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

 linux下《UNIX环境高级编程》(apue2)源码编译出错的处理方法

编辑源码解压生成的apue.2e文件夹下的Make.defines.linux

修改WKDIR=/home/var/apue.2e为你的apue.2e目录,比如我的apue源码解压在/usr/local,那我就改为:

WKDIR=/usr/local/apue.2e

然后进入apue.2e/std 目录,编辑linux.mk。修改里面所有的nawk为awk。

最后返回apue.2e目录,执行make命令。

以下是编译源码时的错误提示跟解决方法(假定你的工作目录跟我的一样,为/usr/local/apue.2e)

错误提示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.)

解决办法:

拷贝apue.h到系统默认头文件目录中

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

错误提示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

解决办法:

err_quit跟err_sys是作者自己定义的错误处理函数,需要单独定义头文件

在/usr/include 下新建一个名为myerr.h的文件

拷贝下边的内容到myerr.h(其实此头文件在原书的附录B中)

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 */
}

然后在你需要使用这几种错误处理函数的程序源代码里加入

#include 

就好了。

原创粉丝点击