`
bamboobee
  • 浏览: 5591 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

[转载]nix高级编程及unix网络编程(进程通信)编译方法

阅读更多
[size=medium]转自:http://frenchleaf.iteye.com/blog/779086
就是不改err_sys方法为printf,
我把代码放在/root/Desktop/unix/apue.2e
编辑源码解压生成的apue.2e文件夹下的Make.defines.linux
WKDIR=/home/var/apue.2e为/root/Desktop/unix/apue.2e
然后进入apue.2e/std 目录,编辑linux.mk。修改里面所有的nawk为awk
vim下
:%s/nawk/awk/
在/root/Desktop/unix/apue.2e下运行make
输出一堆...
结尾是
etenv1.c:4: error: ‘ARG_MAX’ undeclared here (not in a function)
make[2]: *** [getenv1.o] 错误 1
make[2]:正在离开目录 `/root/Desktop/unix/apue.2e/threadctl'
make[1]: *** [linux] 错误 1
make[1]:正在离开目录 `/root/Desktop/unix/apue.2e'
make: *** [all] 错误 2
基本没啥大错误
编辑在目录下编辑fig1.c为
Java代码  
#include "apue.h"  
#define BUFFSIZE 4096  
int main(void)  
{  
        int n;  
        char buf[BUFFSIZE];  
        while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0)  
                if(write(STDOUT_FILENO,buf,n)!=n)  
                        err_sys("write error");  
        if(n<0)  
                err_sys("read error");  
        exit(0);  
}  


然后运行
gcc fig1.c -I ./include/ -L ./lib -lapue
会生成a.out
./a.out 写啥就打印啥了
说明:
-o就不说了,不写就生成a.out
-I 指定apue.h所在的文件夹
-L 类库的目录
-l 找静态库,比如libapue.a的名称为apue
编译完成后可以用
ldd a.out看看库,是叫库吧,

ctags:
apt-get install ctags
在source目录下
ctags -R生成tags文件
在~/.vimrc文件中定义
set tags=生成的tags文件全路径
vim fig1.c后用
光标处Ctrl-]键:跳到光标所在单词的tag。Ctrl-T键:跳回原来的位置。g]键(先按g再按]):如果有同名的多个tag,可以用这两个键进行跳转,会提示选择序号



如果是《unix网络编程-卷2-进程间通信》
比如下载的源码在/root/Desktop/unpv22e/
cd /root/Desktop/unpv22e
./configure
make
vi config.h
注释掉56,57,58行
cd lib
make就成功了
cd ../pipe     # build and test a simple program
Java代码 
[root@localhost pipe]# make pipeconf 
gcc -g -O2 -D_REENTRANT -Wall   -c -o pipeconf.o pipeconf.c 
gcc -g -O2 -D_REENTRANT -Wall -o pipeconf pipeconf.o ../libunpipc.a -lrt -lpthread  
../libunpipc.a(wrapunix.o): In function `Mktemp': 
/root/haoning/unpv22e/lib/wrapunix.c:184: warning: the use of `mktemp' is dangerous, better use `mkstemp' 
[root@localhost pipe]# ./pipeconf /tmp/ 
PIPE_BUF = 4096, OPEN_MAX = 1024 

参考http://tieba.baidu.com/f?kz=327192705

在第三章 System V IPC里(在红帽5里测试)
svmsg里的类编译不过
Java代码 
[root@s121@251 svmsg]# make 
gcc -g -O2 -D_REENTRANT -Wall   -c -o ctl.o ctl.c 
ctl.c: In function ‘main’: 
ctl.c:8: 错误:‘buf’ 的存储大小未知 
ctl.c:10: 错误:‘MSG_R’ 未声明 (在此函数内第一次使用) 
ctl.c:10: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其 
ctl.c:10: 错误:所在的函数内只报告一次。) 
ctl.c:10: 错误:‘MSG_W’ 未声明 (在此函数内第一次使用) 
ctl.c:18: 错误:‘ulong_t’ 未声明 (在此函数内第一次使用) 
ctl.c:18: 错误:expected ‘)’ before ‘info’ 
ctl.c:19: 警告:格式字符串实参太少 
ctl.c:8: 警告:未使用的变量 ‘buf’ 
make: *** [ctl.o] 错误 1 

改三部分:
1.unpv22e里面的Make.defines
修改
#CFLAGS = -g -O2 -D_REENTRANT -Wall
CFLAGS = -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall
2.代码里面的,比如ctl.c里面的Msgget方法需要修改
//msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);
msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);
3. 把所有的ulong_t改成ulong
修改后ctl.c就没错误了,其他错误还有一堆,挨个改吧,诶
把svmsg/ctl.c改成:
Java代码 
#include        "unpipc.h"  
  
int  
main(int argc, char **argv)  
{  
        int                             msqid;  
        struct msqid_ds info;  
        struct msgbuf   buf;  
  
//      msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  
        msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);  
        buf.mtype = 1;  
        buf.mtext[0] = 1;  
        Msgsnd(msqid, &buf, 1, 0);  
  
        Msgctl(msqid, IPC_STAT, &info);  
        printf("read-write: %03o, cbytes = %lu, qnum = %lu, qbytes = %lu\n", info.msg_perm.mode & 0777, (ulong) info.msg_cbytes, (ul  
ong) info.msg_qnum, (ulong) info.msg_qbytes);//这里的ulong_t全改成ulong了  
  
        system("ipcs -q");  
  
        Msgctl(msqid, IPC_RMID, NULL);  
        exit(0);  
}  



Java代码 
[root@localhost svmsg]# make 
gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall    -c -o ctl.o ctl.c 
gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall  -o ctl ctl.o ../libunpipc.a -lrt -lpthread  
../libunpipc.a(wrapunix.o): In function `Mktemp': 
/root/haoning/unpv22e/lib/wrapunix.c:184: warning: the use of `mktemp' is dangerous, better use `mkstemp' 
gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall    -c -o limits.o limits.c 
limits.c: In function ‘main’: 
limits.c:19: 错误:‘MSG_R’ 未声明 (在此函数内第一次使用) 
limits.c:19: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其 
limits.c:19: 错误:所在的函数内只报告一次。) 
limits.c:19: 错误:‘MSG_W’ 未声明 (在此函数内第一次使用) 
make: *** [limits.o] 错误 1 

继续改limits.c
Java代码 
#include    "unpipc.h"  
  
#define MAX_DATA    64*1024  
#define MAX_NMESG   4096  
#define MAX_NIDS    4096  
int     max_mesg;  
  
struct mymesg {  
  long  type;  
  char  data[MAX_DATA];  
} mesg;  
  
int  
main(int argc, char **argv)  
{  
    int     i, j, msqid, qid[MAX_NIDS];  
  
        /* 4first try and determine maximum amount of data we can send */  
//  msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  
    msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);  
    mesg.type = 1;  
    for (i = MAX_DATA; i > 0; i -= 128) {  
        if (msgsnd(msqid, &mesg, i, 0) == 0) {  
            printf("maximum amount of data per message = %d\n", i);  
            max_mesg = i;  
            break;  
        }  
        if (errno != EINVAL)  
            err_sys("msgsnd error for length %d", i);  
    }  
    if (i == 0)  
        err_quit("i == 0");  
    Msgctl(msqid, IPC_RMID, NULL);  
  
        /* 4see how many messages of varying size can be put onto a queue */  
    mesg.type = 1;  
    for (i = 8; i <= max_mesg; i *= 2) {  
    //  msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  
        msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);      
        for (j = 0; j < MAX_NMESG; j++) {  
            if (msgsnd(msqid, &mesg, i, IPC_NOWAIT) != 0) {  
                if (errno == EAGAIN)  
                    break;  
                err_sys("msgsnd error, i = %d, j = %d", i, j);  
                break;  
            }  
        }  
        printf("%d %d-byte messages were placed onto queue,", j, i);  
        printf(" %d bytes total\n", i*j);  
        Msgctl(msqid, IPC_RMID, NULL);  
    }  
  
        /* 4see how many identifiers we can "open" */  
    mesg.type = 1;  
    for (i = 0; i <= MAX_NIDS; i++) {  
        //if ( (qid[i] = msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT)) == -1) {  
  
        if ( (qid[i] = msgget(IPC_PRIVATE, IPC_CREAT|0660)) == -1) {  
            printf("%d identifiers open at once\n", i);  
            break;  
        }  
    }  
    for (j = 0; j < i; j++)  
        Msgctl(qid[j], IPC_RMID, NULL);  
  
    exit(0);  
}  

slot.c---->System V IPC .P26 第三章
Java代码 
#include        "unpipc.h"  
int  
main(int argc, char **argv)  
{  
        int             i, msqid;  
  
        for (i = 0; i < 10; i++) {  
        //      msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  
                msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);  
                printf("msqid = %d\n", msqid);  
  
                Msgctl(msqid, IPC_RMID, NULL);  
        }  
        exit(0);  
}  

unpv22e/shm/svmsgread.c中

Java代码 
[root@122226 shm]# make 
gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall    -c -o svmsgread.o svmsgread.c 
svmsgread.c: In function ‘main’: 
svmsgread.c:27: 错误:‘MSG_R’ 未声明 (在此函数内第一次使用) 
svmsgread.c:27: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其 
svmsgread.c:27: 错误:所在的函数内只报告一次。) 
svmsgread.c:55: 警告:格式 ‘%d’ 需要类型 ‘int’,但实参 2 的类型为 ‘s 


O_RDWR替换MSG_R或者MSG_W,网上说的 O_RDWD没找到 [/size]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics