V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nazor
V2EX  ›  C

C语言如何更换头部包含

  •  
  •   nazor · 2013-09-20 18:45:06 +08:00 via Android · 1090 次点击
    这是一个创建于 3843 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个程序,头部引用的库全是linux下的,请问如何更换为win下的库文件?
    以下是linux下的:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/ip.h>
    #include <netinet/udp.h>
    #include <arpa/inet.h>
    #include <net/if.h>
    #include <sys/socket.h>
    #include <syslog.h>
    #include <netinet/in.h>
    #include <stdio.h>
    5 条回复    1970-01-01 08:00:00 +08:00
    luikore
        1
    luikore  
       2013-09-20 19:38:14 +08:00
    这是移植问题, 很难

    sys/socket 和 net 系列可以换成 winsocks, ws2tcpip 和 iphlpapi, 但是不少 api 都不同
    unistd 可以找个替代的

    其他你就得自己弄明白都是干什么的, 一个个换替代品了... 可以参考一些跨平台语言的处理, 例如 https://github.com/ruby/ruby/blob/trunk/win32/win32.c

    另外一个简单很多的方法是在 cygwin 编译, 什么都不用做, 最后多打包一个 cygwin.dll 就可以了
    cctvsmg
        2
    cctvsmg  
       2013-09-20 20:16:37 +08:00
    一般都是在定义的时候
    #if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
    ......
    #enif

    头部定义是这么定义
    后面具体涉及到跨平台的一些操作
    也分win32 linux mac几种定义
    LazyZhu
        3
    LazyZhu  
       2013-09-20 20:19:31 +08:00
    我同意楼上的,还是用 Cygwin 编译吧,虽然中间多了一层,性能没MinG高,但确实简单。
    leavic
        4
    leavic  
       2013-09-20 21:03:05 +08:00
    其实C还好吧,各平台下的C库基本都还差不多,不像高级语言那样可能Windows平台有的库在Linux根本不存在,需要自己实现的。
    方法基本就是楼上几位所说的了,如果一开始就要做跨平台的,初期架构就要考虑到这些东西,宏定义是必须的,你看看ecos的宏定义多得,都必须再单独做一个工具来管理了。
    standin000
        5
    standin000  
       2013-09-21 23:16:45 +08:00
    Makefile也得改。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2666 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:24 · PVG 23:24 · LAX 08:24 · JFK 11:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.