《UNIX环境高级编程》单个源码编译方法和编译问题彻底解决方案
2012-06-08 09:21
483 查看
对于很多初学 《UNIX环境高级编程》(AdvancedProgramming in the UNIX Environment,简称APUE,以下使用简称)的朋友 ,第一个遇到的问题可能就是该书中的源代码编译 的问题。此书中差不多每个例程中,都会有这样一行源码 :
#include "ourhdr.h"
在第二版中改为:
#include "apue.h"
这个头文件 是作者把把每个例程中常用的标准头文件,一些常用的出错处理函数(err_**()之类的函数)和一些常用的宏定义给整理在一个头文件中。这个可以省去在每个例程中录入较多的重复代码,这样可以减少每个例程的长度。但是,这样就给读者带来了不少麻烦。因为我们还要去搞明白如和把这个头文件编译,然后做成库文件,添加到我们的系统 中。特别读于初学者,本来满怀信心的,结果在编译第一个程序 的时候就出现了问题。我也没有搞明白如何把"ourhdr.h"静态的编译到系统中。
不过,不明白如何使用"ourhdr.h"这个头文件,并不会影响我们学习APUE,也不会影响我们编译和运行每一个例程。其实,简单的想一下,如果一个C程序要能顺利的编译和运行,除了我们要语法正确等方面外,最根本的是要保证我们程序中所调用的函数以及宏等等都要有完整的来源,也就是必须包含所有调用函数和宏所在的头文件。对于一个具体的源程序,如果我们正确的包含了头文件,那么剩下的就是程序本生语法方面应该注意的事项。
如何确定系统调用函数包含在那个头文件中呢?这在Unix/Linux系统下并非一件难事。Unix/Linux下命令 man可以帮助我们找到。man命令不仅可以帮助我们查找一般命令的用法,同时提供不同层次的帮助诸如系统调用或者管理员级别的命令等等(譬如FreeBSD6.1中,man1是用户专用手册,man 2是系统调用,man 3是库函数查询等等)。
下面我们就以APUE书中程序1-1(实现ls命令部分功能 )为例,来说明如何将书中的程序改编成全部使用标准头文件的程序。其中,操作系统用的是FreeBSD6.1,经过相应的修改可以在书中所说的几个Unix 系统及Linux系统中运行,我也曾在Debian Linux下成功编译和运行该程序。书中1-1.c的原始代码如下:
复制代码
# include < sys/ types. h>
# include < dirent. h>
# include "ourhdr.h"
int
main( int argc, char * argv[ ] )
{
DIR * dp;
struct dirent * dirp;
if ( argc ! = 2)
err_quit( "usage: ls directory_name" ) ;
if ( ( dp = opendir( argv[ 1] ) ) = = NULL )
err_sys( "can't open %s" , argv[ 1] ) ;
while ( ( dirp = readdir( dp) ) ! = NULL )
printf ( "%s/n" , dirp- > d_name) ;
closedir( dp) ;
exit ( 0) ;
}
复制代码
从书后面的附录中可以看到"ourhdr.h"的内容比较多,包含了比较多的常用头文件,一些宏定义和一些常用函数和出错函数的定义。其实,对于每一个具体的程序,我们只需要找到该程序中用到的头文件即可。
该1-1.c中所用到的系统函数调用有:opnedir(),readdir(),printf(),closedir()和exit()。
其中,对于常用的函数prinft()和exit(),它们所在的头文件一般都知道,分别是<stdio.h>和<stdlib.h>。而对于 opnedir(),readdir()和closedir(),我们可以通过man opendir,man readdir,manclosedir得到这三个关于目录 操作的函数所在的头文件都是:<sys/types.h>和<dirent.h>。这两个头文件在源程序中也已经列出。
其次,1-1.c中还用到了作者自定义的两个函数:err_quit()和err_sys()。这两个函数主要使用来进行出错处理的。当然,使用这两个函数对错误信息的处理是比较完善的。但是,作为我们学习来讲,了解程序的核心功能是首要的,我们可以将出错处理简化一点,即当遇到错误的时候,我们只简单的使用printf()函数来提示一下有错误发生。当然,用printf()来进行出错处理并不是一种很合理的方法,而且往往我们看不到更关键的错误信息,但对于我们仅仅作为学习来用还是可以接受的。毕竟我们要理解的核心部分是程序的功能实现,出错处理在于其次。
通过以上的说明,我们可以将1-1.c修改为如下内容:
复制代码
# include < sys/ types. h>
# include < dirent. h>
# include < stdio. h>
# include < stdlib. h>
int main( int argc, char * argv[ ] )
{
DIR * dp;
struct dirent * dirp;
if ( argc ! = 2)
{
printf ( "You need input the directory name./n" ) ;
exit ( 1) ;
}
if ( ( dp = opendir( argv[ 1] ) ) = = NULL )
{
printf ( "cannot open %s/n" , argv[ 1] ) ;
exit ( 1) ;
}
while ( ( dirp = readdir( dp) ) ! = NULL )
printf ( "%s/n" , dirp- > d_name) ;
closedir( dp) ;
exit ( 0) ;
}
复制代码
这样修改后的程序已经与作者的头文件"ourhdr.h"没有关系,可以单独的进行编译。我使用的是root用户,执行命令:
# gcc 1-1.c //生成目标文件a.out
或者
# gcc -o 1-1 1-1.c //生成目标文件1-1
没有任何错误和警告,说明编译成功。这时我们执行生成的目标文件:
# ./a.out /home
或者
# ./1-1 /home
则会列出/home路径下的所有文件,包括目录(.)和(..)。
通过这样的方法,基本上我们可以将该书中所有的例程修改成不包含"ourhdr.h"的程序。这样,我们就可以单独的编译每一个例程,而不用顾及作者所给的杂凑的头文件。同时这种比较笨的方法,反而有利于帮助我们了解不同系统调用所对应的头文件,对于学习来说,这应该是一件好事。
《UNIX环境高级编程》编译问题彻底解决方案
复制代码
#include "apue.h"
#include <dirent.h>
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
{
err_quit("usage: ls directory_name");
}
if((dp = opendir(argv[1])) == NULL)
{
err_sys("can`t open %s", argv[1]);
}
while((dirp = readdir(dp)) != NULL)
{
printf("%s/n", dirp->d_name);
}
closedir(dp);
exit(0);
}
复制代码
直接这样用gcc编译会很悲剧的出现一大堆错误,错误这里就不罗列出来了。
其实细心的朋友会发现本程序第一行包含了一个apue.h的头文件,这个头文件在/usr/include可是没有的哦,也就是说这不是标准头文件,其实从它用双引号包含也可以看出来。那这个头文件在哪呢?
附录里。
找到了问题,解决就好办了,把附录里的apue.h和error.c敲进电脑里,我敲好了,懒得就直接复制吧。
复制代码
/* Our own header, to be included before all standard system headers */
#ifndef _APUE_H
#define _APUE_H
#if defined(SOLARIS)
#define _XOPEN_SOURCE 500 /* Single UNIX Specification, Version 2 for Solaris 9 */
#define CMSG_LEN(x) _CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))
#elif !defined(BSD)
#define _XOPEN_SOURCE 600 /* Single UNIX Specification, Version 3 */
#endif
#include <sys/types.h> /* some systems still require this */
#include <sys/stat.h>
#include <sys/termios.h> /* for winsize */
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif
#include <stdio.h> /* for convenience */
#include <stdlib.h> /* for convenience */
#include <stddef.h> /* for offsetof */
#include <string.h> /* for convenience */
#include <unistd.h> /* for convenience */
#include <signal.h> /* for SIG_ERR */
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
#include <error.h>
#define MAXLINE 4096 /* max line length */
/****/
/*
* Default file access permissions for new files.
*/
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/*
* Default permissions for new directories.
*/
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
typedef void Sigfunc(int); /* for signal handlers */
#if defined(SIG_IGN) && !defined(SIG_ERR)
#define SIG_ERR ((Sigfunc *)-1)
#endif
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
/*
* Prototypes for our own functions.
*/
char *path_alloc(int *); /* {Prog pathalloc} */
long open_max(void); /* {Prog openmax} */
void clr_fl(int, int); /* {Prog setfl} */
void set_fl(int, int); /* {Prog setfl} */
void pr_exit(int); /* {Prog prexit} */
void pr_mask(const char *); /* {Prog prmask} */
Sigfunc *signal_intr(int, Sigfunc *); /* {Prog signal_intr_function} */
int tty_cbreak(int); /* {Prog raw} */
int tty_raw(int); /* {Prog raw} */
int tty_reset(int); /* {Prog raw} */
void tty_atexit(void); /* {Prog raw} */
#ifdef ECHO /* only if <termios.h> has been included */
struct termios *tty_termios(void); /* {Prog raw} */
#endif
void sleep_us(unsigned int); /* {Ex sleepus} */
ssize_t readn(int, void *, size_t); /* {Prog readn_writen} */
ssize_t writen(int, const void *, size_t); /* {Prog readn_writen} */
void daemonize(const char *); /* {Prog daemoninit} */
int s_pipe(int *); /* {Progs streams_spipe sock_spipe} */
int recv_fd(int, ssize_t (*func)(int,
const void *, size_t));/* {Progs recvfd_streams recvfd_sockets} */
int send_fd(int, int); /* {Progs sendfd_streams sendfd_sockets} */
int send_err(int, int,
const char *); /* {Prog senderr} */
int serv_listen(const char *); /* {Progs servlisten_streams servlisten_sockets} */
int serv_accept(int, uid_t *); /* {Progs servaccept_streams servaccept_sockets} */
int cli_conn(const char *); /* {Progs cliconn_streams cliconn_sockets} */
int buf_args(char *, int (*func)(int,
char **)); /* {Prog bufargs} */
int ptym_open(char *, int); /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */
int ptys_open(char *); /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */
#ifdef TIOCGWINSZ
pid_t pty_fork(int *, char *, int, const struct termios *,
const struct winsize *); /* {Prog ptyfork} */
#endif
int lock_reg(int, int, int, off_t, int, off_t); /* {Prog lockreg} */
#define read_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
pid_t lock_test(int, int, off_t, int, off_t); /* {Prog locktest} */
#define is_read_lockable(fd, offset, whence, len) /
(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) /
(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)
void err_dump(const char *, ...); /* {App misc_source} */
void err_msg(const char *, ...);
void err_quit(const char *, ...);
void err_exit(int, const char *, ...);
void err_ret(const char *, ...);
void err_sys(const char *, ...);
void log_msg(const char *, ...); /* {App misc_source} */
void log_open(const char *, int, int);
void log_quit(const char *, ...);
void log_ret(const char *, ...);
void log_sys(const char *, ...);
void TELL_WAIT(void); /* parent/child from {Sec race_conditions} */
void TELL_PARENT(pid_t);
void TELL_CHILD(pid_t);
void WAIT_PARENT(void);
void WAIT_CHILD(void);
#endif /* _APUE_H */
复制代码
复制代码
#include "apue.h"
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* 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 */
}
复制代码
这样涉及到两个文件的同时编译问题,如果你更懒,那么还有办法解决。
给个批处理用用吧。
复制代码
#!/bin/sh
#gg
#以下是修改的gg脚本,添加了一个函数,indent每一个.c和.h的功能
#wraper the gcc
#set -x
#用indent所有的c文件和头文件(因为我向ssh中拷代码的时候缩进总是出问题,所以需要indent)
go2indent()
{
for tt in $*
do
char=`echo $tt|cut -d. -f2`
if [ $char = "c" -o $char = "h" ]; then
char=""
#此处indent格式自己修改。
indent -ts2 -bli0 $tt
rm $tt~ >/dev/null 2>&1
fi
done
}
#不需要indent的话,直接注释下面这条语句
go2indent $*
exec_obj=`echo $1|cut -d. -f1`
if gcc error.c $* -Wall -o $exec_obj; then
echo "gcc Done! Create executable file /"$exec_obj/"."
else
echo "gcc failed!"
exit 1
fi
#end
复制代码
把批处理命名为gg,是不是迫不及待的想试试了?
不过,还有等一下,应该此时你还没有执行权限,敲命令sudo chmod 755 gg提升权限。然后别忘了用ls -l看一下,如果是该文件权限是-rwxr-xr-x,即本用户和超级用户读写执行,本组用户读执行,其他用户执行。
以后只要把这3个文件拷贝到你编写的APUE例程文件夹中,以./gg 文件名,来进行编译就可以了
#include "ourhdr.h"
在第二版中改为:
#include "apue.h"
这个头文件 是作者把把每个例程中常用的标准头文件,一些常用的出错处理函数(err_**()之类的函数)和一些常用的宏定义给整理在一个头文件中。这个可以省去在每个例程中录入较多的重复代码,这样可以减少每个例程的长度。但是,这样就给读者带来了不少麻烦。因为我们还要去搞明白如和把这个头文件编译,然后做成库文件,添加到我们的系统 中。特别读于初学者,本来满怀信心的,结果在编译第一个程序 的时候就出现了问题。我也没有搞明白如何把"ourhdr.h"静态的编译到系统中。
不过,不明白如何使用"ourhdr.h"这个头文件,并不会影响我们学习APUE,也不会影响我们编译和运行每一个例程。其实,简单的想一下,如果一个C程序要能顺利的编译和运行,除了我们要语法正确等方面外,最根本的是要保证我们程序中所调用的函数以及宏等等都要有完整的来源,也就是必须包含所有调用函数和宏所在的头文件。对于一个具体的源程序,如果我们正确的包含了头文件,那么剩下的就是程序本生语法方面应该注意的事项。
如何确定系统调用函数包含在那个头文件中呢?这在Unix/Linux系统下并非一件难事。Unix/Linux下命令 man可以帮助我们找到。man命令不仅可以帮助我们查找一般命令的用法,同时提供不同层次的帮助诸如系统调用或者管理员级别的命令等等(譬如FreeBSD6.1中,man1是用户专用手册,man 2是系统调用,man 3是库函数查询等等)。
下面我们就以APUE书中程序1-1(实现ls命令部分功能 )为例,来说明如何将书中的程序改编成全部使用标准头文件的程序。其中,操作系统用的是FreeBSD6.1,经过相应的修改可以在书中所说的几个Unix 系统及Linux系统中运行,我也曾在Debian Linux下成功编译和运行该程序。书中1-1.c的原始代码如下:
复制代码
# include < sys/ types. h>
# include < dirent. h>
# include "ourhdr.h"
int
main( int argc, char * argv[ ] )
{
DIR * dp;
struct dirent * dirp;
if ( argc ! = 2)
err_quit( "usage: ls directory_name" ) ;
if ( ( dp = opendir( argv[ 1] ) ) = = NULL )
err_sys( "can't open %s" , argv[ 1] ) ;
while ( ( dirp = readdir( dp) ) ! = NULL )
printf ( "%s/n" , dirp- > d_name) ;
closedir( dp) ;
exit ( 0) ;
}
复制代码
从书后面的附录中可以看到"ourhdr.h"的内容比较多,包含了比较多的常用头文件,一些宏定义和一些常用函数和出错函数的定义。其实,对于每一个具体的程序,我们只需要找到该程序中用到的头文件即可。
该1-1.c中所用到的系统函数调用有:opnedir(),readdir(),printf(),closedir()和exit()。
其中,对于常用的函数prinft()和exit(),它们所在的头文件一般都知道,分别是<stdio.h>和<stdlib.h>。而对于 opnedir(),readdir()和closedir(),我们可以通过man opendir,man readdir,manclosedir得到这三个关于目录 操作的函数所在的头文件都是:<sys/types.h>和<dirent.h>。这两个头文件在源程序中也已经列出。
其次,1-1.c中还用到了作者自定义的两个函数:err_quit()和err_sys()。这两个函数主要使用来进行出错处理的。当然,使用这两个函数对错误信息的处理是比较完善的。但是,作为我们学习来讲,了解程序的核心功能是首要的,我们可以将出错处理简化一点,即当遇到错误的时候,我们只简单的使用printf()函数来提示一下有错误发生。当然,用printf()来进行出错处理并不是一种很合理的方法,而且往往我们看不到更关键的错误信息,但对于我们仅仅作为学习来用还是可以接受的。毕竟我们要理解的核心部分是程序的功能实现,出错处理在于其次。
通过以上的说明,我们可以将1-1.c修改为如下内容:
复制代码
# include < sys/ types. h>
# include < dirent. h>
# include < stdio. h>
# include < stdlib. h>
int main( int argc, char * argv[ ] )
{
DIR * dp;
struct dirent * dirp;
if ( argc ! = 2)
{
printf ( "You need input the directory name./n" ) ;
exit ( 1) ;
}
if ( ( dp = opendir( argv[ 1] ) ) = = NULL )
{
printf ( "cannot open %s/n" , argv[ 1] ) ;
exit ( 1) ;
}
while ( ( dirp = readdir( dp) ) ! = NULL )
printf ( "%s/n" , dirp- > d_name) ;
closedir( dp) ;
exit ( 0) ;
}
复制代码
这样修改后的程序已经与作者的头文件"ourhdr.h"没有关系,可以单独的进行编译。我使用的是root用户,执行命令:
# gcc 1-1.c //生成目标文件a.out
或者
# gcc -o 1-1 1-1.c //生成目标文件1-1
没有任何错误和警告,说明编译成功。这时我们执行生成的目标文件:
# ./a.out /home
或者
# ./1-1 /home
则会列出/home路径下的所有文件,包括目录(.)和(..)。
通过这样的方法,基本上我们可以将该书中所有的例程修改成不包含"ourhdr.h"的程序。这样,我们就可以单独的编译每一个例程,而不用顾及作者所给的杂凑的头文件。同时这种比较笨的方法,反而有利于帮助我们了解不同系统调用所对应的头文件,对于学习来说,这应该是一件好事。
《UNIX环境高级编程》编译问题彻底解决方案
复制代码
#include "apue.h"
#include <dirent.h>
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
{
err_quit("usage: ls directory_name");
}
if((dp = opendir(argv[1])) == NULL)
{
err_sys("can`t open %s", argv[1]);
}
while((dirp = readdir(dp)) != NULL)
{
printf("%s/n", dirp->d_name);
}
closedir(dp);
exit(0);
}
复制代码
直接这样用gcc编译会很悲剧的出现一大堆错误,错误这里就不罗列出来了。
其实细心的朋友会发现本程序第一行包含了一个apue.h的头文件,这个头文件在/usr/include可是没有的哦,也就是说这不是标准头文件,其实从它用双引号包含也可以看出来。那这个头文件在哪呢?
附录里。
找到了问题,解决就好办了,把附录里的apue.h和error.c敲进电脑里,我敲好了,懒得就直接复制吧。
复制代码
/* Our own header, to be included before all standard system headers */
#ifndef _APUE_H
#define _APUE_H
#if defined(SOLARIS)
#define _XOPEN_SOURCE 500 /* Single UNIX Specification, Version 2 for Solaris 9 */
#define CMSG_LEN(x) _CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))
#elif !defined(BSD)
#define _XOPEN_SOURCE 600 /* Single UNIX Specification, Version 3 */
#endif
#include <sys/types.h> /* some systems still require this */
#include <sys/stat.h>
#include <sys/termios.h> /* for winsize */
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif
#include <stdio.h> /* for convenience */
#include <stdlib.h> /* for convenience */
#include <stddef.h> /* for offsetof */
#include <string.h> /* for convenience */
#include <unistd.h> /* for convenience */
#include <signal.h> /* for SIG_ERR */
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
#include <error.h>
#define MAXLINE 4096 /* max line length */
/****/
/*
* Default file access permissions for new files.
*/
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/*
* Default permissions for new directories.
*/
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
typedef void Sigfunc(int); /* for signal handlers */
#if defined(SIG_IGN) && !defined(SIG_ERR)
#define SIG_ERR ((Sigfunc *)-1)
#endif
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
/*
* Prototypes for our own functions.
*/
char *path_alloc(int *); /* {Prog pathalloc} */
long open_max(void); /* {Prog openmax} */
void clr_fl(int, int); /* {Prog setfl} */
void set_fl(int, int); /* {Prog setfl} */
void pr_exit(int); /* {Prog prexit} */
void pr_mask(const char *); /* {Prog prmask} */
Sigfunc *signal_intr(int, Sigfunc *); /* {Prog signal_intr_function} */
int tty_cbreak(int); /* {Prog raw} */
int tty_raw(int); /* {Prog raw} */
int tty_reset(int); /* {Prog raw} */
void tty_atexit(void); /* {Prog raw} */
#ifdef ECHO /* only if <termios.h> has been included */
struct termios *tty_termios(void); /* {Prog raw} */
#endif
void sleep_us(unsigned int); /* {Ex sleepus} */
ssize_t readn(int, void *, size_t); /* {Prog readn_writen} */
ssize_t writen(int, const void *, size_t); /* {Prog readn_writen} */
void daemonize(const char *); /* {Prog daemoninit} */
int s_pipe(int *); /* {Progs streams_spipe sock_spipe} */
int recv_fd(int, ssize_t (*func)(int,
const void *, size_t));/* {Progs recvfd_streams recvfd_sockets} */
int send_fd(int, int); /* {Progs sendfd_streams sendfd_sockets} */
int send_err(int, int,
const char *); /* {Prog senderr} */
int serv_listen(const char *); /* {Progs servlisten_streams servlisten_sockets} */
int serv_accept(int, uid_t *); /* {Progs servaccept_streams servaccept_sockets} */
int cli_conn(const char *); /* {Progs cliconn_streams cliconn_sockets} */
int buf_args(char *, int (*func)(int,
char **)); /* {Prog bufargs} */
int ptym_open(char *, int); /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */
int ptys_open(char *); /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */
#ifdef TIOCGWINSZ
pid_t pty_fork(int *, char *, int, const struct termios *,
const struct winsize *); /* {Prog ptyfork} */
#endif
int lock_reg(int, int, int, off_t, int, off_t); /* {Prog lockreg} */
#define read_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) /
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
pid_t lock_test(int, int, off_t, int, off_t); /* {Prog locktest} */
#define is_read_lockable(fd, offset, whence, len) /
(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) /
(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)
void err_dump(const char *, ...); /* {App misc_source} */
void err_msg(const char *, ...);
void err_quit(const char *, ...);
void err_exit(int, const char *, ...);
void err_ret(const char *, ...);
void err_sys(const char *, ...);
void log_msg(const char *, ...); /* {App misc_source} */
void log_open(const char *, int, int);
void log_quit(const char *, ...);
void log_ret(const char *, ...);
void log_sys(const char *, ...);
void TELL_WAIT(void); /* parent/child from {Sec race_conditions} */
void TELL_PARENT(pid_t);
void TELL_CHILD(pid_t);
void WAIT_PARENT(void);
void WAIT_CHILD(void);
#endif /* _APUE_H */
复制代码
复制代码
#include "apue.h"
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* 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 */
}
复制代码
这样涉及到两个文件的同时编译问题,如果你更懒,那么还有办法解决。
给个批处理用用吧。
复制代码
#!/bin/sh
#gg
#以下是修改的gg脚本,添加了一个函数,indent每一个.c和.h的功能
#wraper the gcc
#set -x
#用indent所有的c文件和头文件(因为我向ssh中拷代码的时候缩进总是出问题,所以需要indent)
go2indent()
{
for tt in $*
do
char=`echo $tt|cut -d. -f2`
if [ $char = "c" -o $char = "h" ]; then
char=""
#此处indent格式自己修改。
indent -ts2 -bli0 $tt
rm $tt~ >/dev/null 2>&1
fi
done
}
#不需要indent的话,直接注释下面这条语句
go2indent $*
exec_obj=`echo $1|cut -d. -f1`
if gcc error.c $* -Wall -o $exec_obj; then
echo "gcc Done! Create executable file /"$exec_obj/"."
else
echo "gcc failed!"
exit 1
fi
#end
复制代码
把批处理命名为gg,是不是迫不及待的想试试了?
不过,还有等一下,应该此时你还没有执行权限,敲命令sudo chmod 755 gg提升权限。然后别忘了用ls -l看一下,如果是该文件权限是-rwxr-xr-x,即本用户和超级用户读写执行,本组用户读执行,其他用户执行。
以后只要把这3个文件拷贝到你编写的APUE例程文件夹中,以./gg 文件名,来进行编译就可以了
相关文章推荐
- APUE 《UNIX环境高级编程》编译问题彻底解决方案
- 《UNIX环境高级编程》编译问题彻底解决方案
- 《UNIX环境高级编程》编译问题彻底解决方案
- linux下《UNIX环境高级编程》第2版 单个源码编译方法
- 《UNIX环境高级编程》单个源码编译方法(转)
- Cygwin下编译MySQL源码遇到的问题解决方法
- 编译 x3288 开发板 android5.1 源码包时 遇到的问题和解决方法
- 非官网下载的android 源码编译问题 及解决方法
- 《UNIX环境高级编程》单个源码编译方法
- Android源码编译JNI遇到的问题与解决方法
- 《UNIX环境高级编程》单个源码编译方法
- 编译Android源码时常见问题以及解决方法
- 《UNIX环境高级编程》单个源码编译方法
- 3D游戏编程大师技巧第一章 vs2013编译书本源码问题解决方案
- 编译android源码提示/usr/bin/jar命令不存在问题的解决方法
- live555源码研究(十)------在编译过程中遇到的问题及解决方法
- Ubuntu/UbuntuKylin平台下源码编译Ambari 2.4(含较多问题解决方案)
- 使用centos linux-2.6.32-220.23.1.el6源码编译时出现的问题及其解决方法
- QtCreator 4.0 Q5.7 VS2015 源码中有中文导致编译失败的问题的解决方法
- centos7中编译《UNIX环境高级编程》时遇到的问题及解决方法