最近在温习UNP(《Unix网络套接字 V1》),以前都是在linux下搞,现在打算在自己机器弄下,于是google了下,把编译的事情搞定了,修改了一些教程的一些错误,比如下载链接之类的。
- 下载源文件
| wget http://www.unpbook.com/unpv13e.tar.gz |
| git clone https://github.com/unpbook/unpv13e |
- 解压(可选)
- 编译
| cd unpv13e |
| ./configure |
| cd lib |
| make |
| |
| cd ../libfree |
| make |
- 报错及修改
| |
| inet_ntop.c:56:1: error: conflicting types for 'inet_ntop' |
| inet_ntop(af, src, dst, size) |
| ^ |
| /usr/include/arpa/inet.h:77:13: note: previous declaration is here |
| const char *inet_ntop(int, const void *, char *, socklen_t); |
| ^ |
| 1 error generated. |
在 unpv13e/libfree
找到inte_ntop.c文件,把
#include<arph/inet.h>注释掉,再次执行
make`编译。
具体操作如下:
| vim unpv13e/libfree/inte_ntop.c |
| |
| /arph/inet.h |
- 拷贝头文件
回到主目录,首先修改./lib/unp.h/
,将其中#include "../config.h"修改为
#include "config.h",然后执行拷贝操作。
| vim unpv13e/lib/unp.h |
| |
| /config.h |
| |
| sudo cp ./config.h /usr/local/include |
| sudo cp ./lib/unp.h /usr/local/include |
| |
| sudo cp ./libunp.a /usr/local/lib |
- 测试
| |
| #include "unp.h" |
| |
| int main(int argc, char *argv[]) { |
| union { |
| short s; |
| char c[sizeof(short)]; |
| } un; |
| un.s = 0x0102; |
| printf("%s: ", CPU_VENDOR_OS); |
| if (sizeof(short) == 2) { |
| if (un.c[0] == 1 && un.c[1] == 2) { |
| printf("big-endian\n"); |
| } else if (un.c[0] == 2 && un.c[1] == 1) { |
| printf("little-endian\n"); |
| } else { |
| printf("unknown\n"); |
| } |
| } else { |
| printf("sizeof(short) = %d\n", sizeof(short)); |
| } |
| exit(0); |
| } |
编译时制定链接库文件-lunp
| gcc byteorder.c -lunp -o byteorder |
| ./byteorder |
| i386-apple-darwin17.7.0: little-endian |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步