Mac OS配置UNP(Unix网络套接字 V1)环境

本文档记录了如何在Mac OS系统中配置Unix网络套接字V1(UNP)的环境。从下载源文件、解压(如果需要)、编译过程,到解决遇到的头文件缺失问题和编译错误,最后进行测试并确保正确链接库文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在温习UNP(《Unix网络套接字 V1》),以前都是在linux下搞,现在打算在自己机器弄下,于是google了下,把编译的事情搞定了,修改了一些教程的一些错误,比如下载链接之类的。

  1. 下载源文件
wget http://www.unpbook.com/unpv13e.tar.gz #使用wget
git clone https://github.com/unpbook/unpv13e #使用git
  1. 解压(可选)
tar -zxf unpv13e.tar.gz #使用wget下载需要
  1. 编译
cd unpv13e
./configure
cd lib
make

cd ../libfree
make
  1. 报错及修改
#报错
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
  1. 拷贝头文件
    回到主目录,首先修改./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
  1. 测试
// byteorder.c
#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值