现在的位置: 首页 > 综合 > 正文

学习《Unix网络编程》 对其中的lib进行编译

2013年10月18日 ⁄ 综合 ⁄ 共 1270字 ⁄ 字号 评论关闭

在学习过程中,直接拿代码过来研究。

直接编译。

1.发现unp.h 不太好用,则自己创建一个头文件。

2.想对小写的socket 等函数进行包裹。

 

过程如下:先编写gxtunp.h

其中声明了大写字母开头的Socket

 

然后再gxtunp.c 中对其进行定义

 

需要把这个文件编译成so文件(好像是动态库)

 

参考了一下文章http://www.unixresources.net/linux/clf/program/archive/00/00/45/68/456806.html

 

 

 

According to walkman's explain,let me give you a little details.

1) edit func.c to provide function implementation;
2) edit main.c to call functions provided by func.c;
3) gcc -shared -fpic func.c -o libxxx.so;
4) gcc main.c -L ./ -lxxx
#now func.c and main.c are both in same directory,or you have to specify "-L /where_is_libxxx_so"
5) export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/where_is_your_libxxx_so
#tell loader where to search libxxx.so ,which is specified in the 4th step
6) ./a.out

 

 

========================我的操作如下 ==================

 

然后我在 命令行下敲下面命令

[root@localhost intro]# gcc -shared -fpic gxtunp.c -o libunp.so

 

编译出so文件

 

我的testgxtunp.h 文件如下

 

#include "gxtunp.h"

int main(int argc, char **argv)
{
 int fd = Socket(AF_INET,SOCK_DGRAM,0);
 return 1;
}

 

编译这个文件

 

[root@localhost intro]# gcc testgxtunp.c -L ./ libunp.so

 

如果这个时侯执行 ./a.out 会出现如下错误

[root@localhost intro]# ./a.out
./a.out: error while loading shared libraries: libunp.so: cannot open shared obj
ect file: No such file or directory

 

 

说明需要设置库文件的路径

 

命令如下:

[root@localhost intro]# export LD_LIBRARY_PATH=/home/fh/gxt/test/intro/

 

至此

成功执行

================================

高兴中

 

下面需要继续包裹(通俗点就是封装)其他函数

以及err_sys 等类似函数

 

 

 

 

抱歉!评论已关闭.