欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

windows上编译linux程序代码需要用到的头文件

程序员文章站 2022-03-25 17:34:45
项目的代码是在linux上运行的,但我比较习惯在vs上写代码。由于linux下有不少函数在windows上是没有的,所以在vs直接编译是不行的。 为了方便在vs上编译项目代码,我把一些常用的linux函数原型整理成一个特殊的头文件。只要在代码前面引用上这个头文件,就可以很方便地编译代码。 完整的教程 ......

  项目的代码是在linux上运行的,但我比较习惯在vs上写代码。由于linux下有不少函数在windows上是没有的,所以在vs直接编译是不行的。

为了方便在vs上编译项目代码,我把一些常用的linux函数原型整理成一个特殊的头文件。只要在代码前面引用上这个头文件,就可以很方便地编译代码。

完整的教程可以参考我的头条号文章: https://www.toutiao.com/i6599068854754738695/   《在vs2010编译linux下c语言项目的解决方案》

#ifndef __hch_complie_on_win__
#define __hch_complie_on_win__



#ifdef win32

#ifdef __cplusplus
extern "c" {
#endif

//定义一些windows上没有的函数或者类型,方便在vs上写程序
typedef int pid_t;
typedef long key_t;
typedef unsigned long msgqnum_t;
typedef unsigned long msglen_t;
typedef unsigned int uint32_t;

typedef unsigned uid_t;
typedef unsigned gid_t;

#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <io.h>
#include <winsock2.h>
#include <sys/stat.h>

#define __u32_type            unsigned int
typedef __u32_type socklen_t;


struct ipc_perm
{
    int __key;
    int uid;
    int gid;
    int cuid;
    int cgid;
    unsigned short mode;
    unsigned short __pad1;
    unsigned short __seq;
    unsigned short __pad2;
    unsigned short __unused1;
    unsigned short __unused2;
};


/* semop flags */
#define sem_undo        0x1000  /* undo the operation on exit */

/* semctl command definitions. */
#define getpid  11       /* get sempid */
#define getval  12       /* get semval */
#define getall  13       /* get all semval's */
#define getncnt 14       /* get semncnt */
#define getzcnt 15       /* get semzcnt */
#define setval  16       /* set semval */
#define setall  17       /* set all semval's */

/* ipcs ctl cmds */
#define sem_stat 18
#define sem_info 19

struct shmid_ds {
    struct ipc_perm shm_perm;
    int shm_segsz;
    time_t shm_atme;
    time_t shm_dtime;
    time_t shm_ctime;
    pid_t shm_cpid;
    pid_t shm_lpid;
    unsigned short shm_nattch;
    unsigned short shm_unused;
    void *shm_unused2;
    void *shm_unused3;
};

struct tms
{
    clock_t tms_utime;
    clock_t tms_stime;
    clock_t tms_cutime;
    clock_t tms_cstime;
};

/* structure used for argument to `semop' to describe operations.  */
struct sembuf
{
    unsigned short int sem_num;   /* semaphore number */
    short int sem_op;             /* semaphore operation */
    short int sem_flg;            /* operation flag */
};

int semop(int semid, struct sembuf *sops, unsigned nsops);

           struct msginfo {
                      int msgpool; /* size in kibibytes of buffer pool
                                      used to hold message data;
                                      unused within kernel */
                      int msgmap;  /* maximum number of entries in message
                                      map; unused within kernel */
                      int msgmax;  /* maximum number of bytes that can be
                                      written in a single message */
                      int msgmnb;  /* maximum number of bytes that can be
                                      written to queue; used to initialize
                                      msg_qbytes during queue creation
                                      (msgget(2)) */
                      int msgmni;  /* maximum number of message queues */
                      int msgssz;  /* message segment size;
                                      unused within kernel */
                      int msgtql;  /* maximum number of messages on all queues
                                      in system; unused within kernel */
                      unsigned short int msgseg;
                                   /* maximum number of segments;
                                      unused within kernel */
                  };
                  
           struct msqid_ds {
               struct ipc_perm msg_perm;     /* ownership and permissions */
               time_t          msg_stime;    /* time of last msgsnd(2) */
               time_t          msg_rtime;    /* time of last msgrcv(2) */
               time_t          msg_ctime;    /* time of last change */
               unsigned long   __msg_cbytes; /* current number of bytes in
                                                queue (nonstandard) */
               msgqnum_t       msg_qnum;     /* current number of messages
                                                in queue */
               msglen_t        msg_qbytes;   /* maximum number of bytes
                                                allowed in queue */
               pid_t           msg_lspid;    /* pid of last msgsnd(2) */
               pid_t           msg_lrpid;    /* pid of last msgrcv(2) */
           };

//          struct ipc_perm {
//              key_t          __key;       /* key supplied to msgget(2) */
//              uid_t          uid;         /* effective uid of owner */
//              gid_t          gid;         /* effective gid of owner */
//              uid_t          cuid;        /* effective uid of creator */
//              gid_t          cgid;        /* effective gid of creator */
//              unsigned short mode;        /* permissions */
//              unsigned short __seq;       /* sequence number */
//          };
           
           int msgctl(int msqid, int cmd, struct msqid_ds *buf);
           
int kill(pid_t, int);
unsigned sleep(int);
unsigned usleep(int);
pid_t wait (int *);
pid_t fork();
pid_t getpid();
int pipe(int *fd);
int getpgrp();
int killpg(int, int);
int gettimeofday(struct timeval *tp, void *tzp);
int random();
void srandom(unsigned int seed);
key_t ftok(const char *path_name, int proj_id);

int shmget(key_t key, size_t size, int shmflg);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);

int msgget(key_t key, int msgflg);
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
size_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

void *dlopen(const char *filename, int flag);
int dlclose(void *handle);
char *dlerror();
void *dlsym(void *hadle, const char *symbol);
unsigned int alarm(unsigned int seconds);
// int dup(int oldfd);
// int dup2(int oldfd, int newfd);

int mkdir(const char *path, int mode);

char *strsep(char **stringp, const char *delim);

#define dup2 _dup2

#define open _open
#define read _read
#define close _close
#define write _write

typedef void (*sighandler_t)(int);

sighandler_t sigset(int sig, sighandler_t disp);

int sighold(int sig);

int sigrelse(int sig);

int sigignore(int sig);


clock_t times(struct tms *buf);
#ifdef __cplusplus
}
#endif

#define snprintf _snprintf
#define popen _popen
#define pclose _pclose

//信号常量
/*
const int sighup    = 15;
const int sigquit    = 15;
const int sigpwr    = 15;
const int sigusr2    = 15;
const int sigalrm    = 15;

const int sigusr1 = 10; 
const int sigurg = 30; 
const int sigchld = 17;
const int sigpipe = 13;
*/
#ifndef rtld_lazy
#define  rtld_lazy 1
#endif
#ifndef eintr
#define eintr 4
#endif

#define sighup 1
#define sigint 2
#define sigquit 3
#define sigill 4
#define sigtrap 5
#ifndef sigabrt
#define sigabrt 6
#endif
#define sigiot 6
#define sigbus 7
#define sigepe 8
#define sigkill 9
#define sigusr1 10
#define sigsegv 11
#define sigusr2 12
#define sigpipe 13
#define sigalrm 14
#define sigterm 15
#define sigstkflt 16
#define sigchld 17
#define sigcld sigchld
#define sigcont 18
#define sigstop 19
#define sigtstp 20
#define sigttin 21
#define sigttou 22
#define sigurg 23
#define sigxcpu 24
#define sigxfsz 25
#define sigvtalrm 26
#define sigprof 27
#define sigwinch 28
#define sigio 29
#define sigpwr 30
#define sigsys 31
#define sigunused 31
#define sigrtmin 32

#ifndef sigemt
#define sigemt 1122
#endif

#define r_ok 4
#define w_ok 2
#define x_ok 1
#define f_ok 0

#define ipc_stat 0
#define ipc_set 1
#define ipc_rmid 2

#define ipc_creat 01000
#define ipc_excl  02000
#define ipc_nowait 00004000   /* return error on wait */
//#define ipc_rmid  0

#define shm_rdonly 010000

#define wnohang 0x00000001

#define stdin_fileno 0
#define stdout_fileno 1
#define stderr_fileno 2

#define close _close //减少warning c4996警告
#define access _access //减少warning c4996警告

void herror(const char *s); //gethostbyname出错时调用
void bzero(void *s, size_t n);

#ifndef wifexited
#define wifexited(status)    (((status) & 0xff) == 0)
#endif /* !defined wifexited */
#ifndef wexitstatus
#define wexitstatus(status)    (((status) >> 8) & 0xff)
#endif /* !defined wexitstatus */
#ifndef wifsignaled 
#define wifsignaled(x) ((((x) & 255) != 0x255 && ((x) & 255) != 0)) 
#endif /* !defined wifsignaled */
#ifndef wtermsig 
#define wtermsig(x) ((x) & 255) 
#endif /* !defined wtermsig */

struct  sockaddr_un {
    short   sun_family;             /* af_unix */
    char    sun_path[108];          /* path name (gag) */
};


#endif //win32

/*定义电话支付特别参数*/
#ifdef win32
/*定义主共享内存的大小*/
#ifdef gr_shm_size
#undef gr_shm_size
#endif

#define gr_shm_size     2048 + ( sizeof( devinfo ) * 2048 ) + ( sizeof( sockinfo ) * 256 )
#define atminfo_offset  2048
#define devinfo_offset  2048
#define sockinfo_offset atminfo_offset + sizeof( devinfo ) * 2048

/*定义前置机的信息键值*/
#define keyget(a)       ftok( getenv( "home" ), a )

#define shm_key         keyget( 10 )    /*前置机管理器的共享内存的键值*/

#define crd_key         keyget( 11 )    /*c端读队列的键值 -- 指前端调度程序而言*/
#define cwt_key         keyget( 12 )    /*c端写队列的键值 -- 指前端调度程序而言*/
#define hrd_key         keyget( 13 )    /*h端读队列的键值 -- 指后台调度程序而言*/
#define hwt_key         keyget( 14 )    /*h端写队列的键值 -- 指后台调度程序而言*/

#define mon_key         keyget( 15 )    /*unix交易显示队列的键值*/
#define trn_mon_key     keyget( 16 )    /*监控信息传送服务队列的键值*/
#define trn_shm_key     keyget( 17 )    /*监控信息传送服务共享内存的键值*/

#define shm_dict_key    keyget( 18 )    /*数据字典的共享内存的键值*/
#define firewall_key    keyget( 19 )    /*防火墙标志的键值*/

#define cproc_id        keyget( 0  )    /*c端处理系统的id,必须有*/
#define kproc_id        keyget( 1  )    /*k端处理系统的id,它可以有,可以没有*/
#define hproc_id        keyget( 2  )    /*h端处理系统的id,必须有*/
#define monitor_id      keyget( 3  )    /*uinx监控的id,可有可无*/
#define moncli_id       keyget( 4  )    /*交易转发系统的id,可有可无*/
#define syssemp_key     keyget( 30 )    /*定义系统信号量的键值*/

#define cre_shm_key     keyget( 40 )    /*管理中心的共享内存的键值*/
#define cli_mon_key     keyget( 41 )    /*管理中心队列的键值*/

/*冠融前置机系统xml的固定路径的定义,特定的模块的配置文件的名字为:模块名(大写字母或原名)+“.xml”*/
#define dbgflg          keyget( 127 )     /*调试信息设定*/
#define servinfo_key    keyget( 126 )     /*系统信息*/
#define servinfo_size   8192              /*最大为255个信息*/
#define devinfo_key     keyget( 125 )     /*设备接入信息*/
#define sockinfo_key    keyget( 124 )     /*sockinfo信息,用于长链管理*/
#define access_sn_key   keyget( 123 )     /*访问平台流水的key,用于信号量*/
#define devinfo_size    sizeof( devinfo ) * 2048
#define sockinfo_size   sizeof( sockinfo ) * 256

/*集成平台新加的定义*/
#define dbgfile         "gr_debug_file"        /*缺省的调试信息文件*/
#define grsysconf       "gr_sys_conf"       /*系统配置路径的环境变量*/
#define grdbgconf       "gr_debug_path"     /*调试信息存放路径的环境变量*/
#define grlogconf       "gr_log_path"       /*log日志存放的路径的环境变量*/
#define trtmp           "gr_tmp_path"       /*交易临时文件的路径*/
#define grfile          "gr_files"          /*文件所在的路径*/
#define grbkfile        "gr_backfile"       /*备份文件所在的路径*/

/*增强型调试信息*/
#define dbgopen         "gr_debug_on"       /*调试信息是否开启0不开启*/
#define logopen         "gr_log_on"         /*调试信息是否开启0不开启*/
#define dbglevel        "gr_debug_level"    /*定义调试信息的级别,默认的情况下为5级,5级输出所有的信息,兼容原来的调试程序*/
#define dbgl1file        "gr_debug_l1"        /*定义调试信息一级的文件名*/
#define dbgl2file        "gr_debug_l2"        /*定义调试信息二级的文件名*/
#define dbgl3file        "gr_debug_l3"        /*定义调试信息三级的文件名*/
#define dbgl4file        "gr_debug_l4"        /*定义调试信息四级的文件名*/
#define dbgl5file        "gr_debug_l5"        /*定义调试信息五级的文件名*/
/*#define writebyday      "writebyday"  */      /*按日输出调试信息*/

#define loglevel        "gr_log_level"      /*定义log的级别*/
#define logl1file       "gr_log_l1"         /*定义log的一级文件名*/
#define logl2file       "gr_log_l2"         /*定义log的二级文件名*/
#define logl3file       "gr_log_l3"         /*定义log的三级文件名*/
#define logl4file       "gr_log_l4"         /*定义log的四级文件名*/
#define logl5file       "gr_log_l5"         /*定义log的五级文件名*/

/*定义系统中固定配置信息的节点名*/
#define cfgroot         "<gr>"              /*在所有的配置文件中的根结点*/
#define brdaddr         "<broadcastaddr>"  /*广播地址*/
#define brdname         "<broadname>"      /*绑定广播端口的服务名,在/etc/services中配置的名字*/
#define svraddr         "<svraddr>"        /*交易报文需要写入的ip地址,非模块间通讯的ip地址*/
#define svrport         "<svrport>"        /*交易报文件需要写入的端口,非模块间通讯的端口*/
#define svrtimeout      "<svrtimeout>"     /*server读写超时,单位为秒*/
#define clitimeout      "<clrtimeout>"     /*client读写超时,单位为秒*/
#define svridle         "<svridletime>"    /*空闲最长时间,单位为秒*/
#define cliidle         "<cliidletime>"    /*client空闲最长时间,单位为秒*/
#define lnkstyle        "<linkstyle>"      /*接入或请求的链路形式*/
#define illegal         "<noneip>"         /*非法的接入*/
#define refuses         "<notallow>"       /*不允许接入的次数*/
#define shmload         "<shmfile>"        /*装入共享内存的文件名*/
#define fixedkey        "<fixedkey>"       /*固定主密钥*/
#define lnchktime       "<lnchktime>"      /*链路检测时间*/

/*守护进程所使用的参数*/
#define srvname         "<servicesname>"    /*前置机绑定服务端口的服务名,在/etc/services中配置的名字*/
#define firewallen      "<firewallenabled>"/*防火墙使能,1为能,0为不能*/
#define maxlnkcnt       "<maxlinkcount>"   /*最大接入数,默认值为512,最大不超过2048*/
#define socksend        "<socksendval>"    /*通讯的socket是否按传值进入子进程*/
#define schedulerflg    "<schflag>"        /*是否启用调度程式标志*/
#define schedulernam    "<schname>"        /*调度进程的名字*/

/*交易调度系统所用的参数 cscheduler.c中用到的信息*/
#define sndmon          "<datagram>/<montag>"       /*是否发往监控*/
#define codesw          "<datagram>/<dgcodechg>"    /*字符的转换模式*/
#define codedll         "<datagram>/<dgextchglib>"  /*外置转换程式的库名*/

#define dgtype          "<datagram>/<dgtype>"       /*报文类型*/
#define dghdlen         "<datagram>/<dgheadlen>"    /*报文头长度*/
#define dgverify        "<datagram>/<dgverify>"     /*报文头是否验证*/
#define dgconfirm       "<datagram>/<dgconfirm>"    /*报文头验证信息*/
#define dglnsta         "<datagram>/<dglenstyle>"   /*报文长度标识*/
#define dgflag          "<datagram>/<dgflag>"       /*报文具信息,如长度、结束符等*/
#define trstyle         "<datagram>/<trstyle>"      /*交易码的特点*/
#define trpos           "<datagram>/<trstartpos>"   /*交易码的起始位置*/
#define trlen           "<datagram>/<trcodelen>"    /*交易码的长度*/

#define commtype        "<commtype%d>"              /*构成通讯类型*/

/*---------transinfo----------*/
#define anstype         "<t%s>/<mustanswer>"        /*此交易是否必须返回数据*/
#define anstrcode       "<t%s>/<answercode>"        /*交易的应答交易码*/
#define ansrtname       "<t%s>/<rootname>"          /*应答交易的xml报文的根节点*/
#define trproc          "<t%s>/<proc>"              /*交易后处理模块的函数名*/
#define trcorr          "<t%s>/<autocorection>"     /*自动冲正标志*/
#define tswlib          "<t%s>/<tswlib>"            /*交易处理的动态库名*/
#define trcrproc        "<t%s>/<corproc>"           /*交易自动冲正的函数名*/

#define mknode( buff, node, trans ) sprintf( buff, node, trans ) /*构造交易信息节点名*/

#if 0
struct timeb
{
    time_t time;
    unsigned short millitm;
    short timezone;
    short dstflag;
};

int ftime(struct timeb *tp);
#endif


# define fappend        o_append
# define ffsync         o_fsync
# define fasync         o_async
# define fnonblock      o_nonblock
# define fndelay        o_ndelay


#ifndef _asm_generic_fcntl_h
#define _asm_generic_fcntl_h

/*
 * fmode_exec is 0x20
 * fmode_nonotify is 0x1000000
 * these cannot be used by userspace o_* until internal and external open
 * flags are split.
 * -eric paris
 */

/*
 * when introducing new o_* bits, please check its uniqueness in fcntl_init().
 */

#define o_accmode       00000003
#define o_rdonly        00000000
#define o_wronly        00000001
#define o_rdwr          00000002
#ifndef o_creat
#define o_creat         00000100        /* not fcntl */
#endif
#ifndef o_excl
#define o_excl          00000200        /* not fcntl */
#endif
#ifndef o_noctty
#define o_noctty        00000400        /* not fcntl */
#endif
#ifndef o_trunc
#define o_trunc         00001000        /* not fcntl */
#endif
#ifndef o_append
#define o_append        00002000
#endif
#ifndef o_nonblock
#define o_nonblock      00004000
#endif
#ifndef o_dsync
#define o_dsync         00010000        /* used to be o_sync, see below */
#endif
#ifndef fasync
#define fasync          00020000        /* fcntl, for bsd compatibility */
#endif
#ifndef o_direct
#define o_direct        00040000        /* direct disk access hint */
#endif
#ifndef o_largefile
#define o_largefile     00100000
#endif
#ifndef o_directory
#define o_directory     00200000        /* must be a directory */
#endif
#ifndef o_nofollow
#define o_nofollow      00400000        /* don't follow links */
#endif
#ifndef o_noatime
#define o_noatime       01000000
#endif
#ifndef o_cloexec
#define o_cloexec       02000000        /* set close_on_exec */
#endif

/*
 * before linux 2.6.33 only o_dsync semantics were implemented, but using
 * the o_sync flag.  we continue to use the existing numerical value
 * for o_dsync semantics now, but using the correct symbolic name for it.
 * this new value is used to request true posix o_sync semantics.  it is
 * defined in this strange way to make sure applications compiled against
 * new headers get at least o_dsync semantics on older kernels.
 *
 * this has the nice side-effect that we can simply test for o_dsync
 * wherever we do not care if o_dsync or o_sync is used.
 *
 * note: __o_sync must never be used directly.
 */
#ifndef o_sync
#define __o_sync        04000000
#define o_sync          (__o_sync|o_dsync)
#endif

#ifndef o_path
#define o_path          010000000
#endif

#ifndef o_ndelay
#define o_ndelay        o_nonblock
#endif

#define f_dupfd         0       /* dup */
#define f_getfd         1       /* get close_on_exec */
#define f_setfd         2       /* set/clear close_on_exec */
#define f_getfl         3       /* get file->f_flags */
#define f_setfl         4       /* set file->f_flags */
#ifndef f_getlk
#define f_getlk         5
#define f_setlk         6
#define f_setlkw        7
#endif
#ifndef f_setown
#define f_setown        8       /* for sockets. */
#define f_getown        9       /* for sockets. */
#endif
#ifndef f_setsig
#define f_setsig        10      /* for sockets. */
#define f_getsig        11      /* for sockets. */
#endif

#ifndef config_64bit
#ifndef f_getlk64
#define f_getlk64       12      /*  using 'struct flock64' */
#define f_setlk64       13
#define f_setlkw64      14
#endif
#endif

#ifndef f_setown_ex
#define f_setown_ex     15
#define f_getown_ex     16
#endif

#ifndef f_getowner_uids
#define f_getowner_uids 17
#endif

#define f_owner_tid     0
#define f_owner_pid     1
#define f_owner_pgrp    2


/* for f_[get|set]fl */
#define fd_cloexec      1       /* actually anything with low bit set goes */

/* for posix fcntl() and lockf() */
#ifndef f_rdlck
#define f_rdlck         0
#define f_wrlck         1
#define f_unlck         2
#endif

struct flock {
    short   l_type;
    short   l_whence;
    size_t  l_start;
    size_t  l_len;
    size_t  l_pid;
};

/* for old implementation of bsd flock () */
#ifndef f_exlck
#define f_exlck         4       /* or 3 */
#define f_shlck         8       /* or 4 */
#endif

/* operations for bsd flock(), also used by the kernel implementation */
#define lock_sh         1       /* shared lock */
#define lock_ex         2       /* exclusive lock */
#define lock_nb         4       /* or'd with one of the above to prevent
                                   blocking */
#define lock_un         8       /* remove lock */

#define lock_mand       32      /* this is a mandatory flock ... */
#define lock_read       64      /* which allows concurrent read operations */
#define lock_write      128     /* which allows concurrent write operations */
#define lock_rw         192     /* which allows concurrent read & write ops */

#define f_linux_specific_base   1024



#endif /* _asm_generic_fcntl_h */

#else

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef _xopen_source
#include <sys/ipc.h> 
#endif
#include <netinet/in.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/timeb.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <sys/select.h>
#include <sys/un.h>
#include <sys/ipc.h>
#include <sys/msg.h>
# ifdef _xopen_source_extended
#  include <arpa/inet.h>
# endif
#include <sys/socket.h>


#endif //win32

#define xml_index_test 1
#define sleep sleepcustom

#endif //__hch_complie_on_win__