Linux程序设计基础

2009-11-18 16:40:01 Network Content Audit Views(344)

1.条件编译:因为所有的源代码行都将参加编译,所以如果使用条件编译则可显著减少目标代码的长度,如可以根据不同计算机的体系结构只编译与当前计算机体系结构相关的源代码。

实现方式:#if、#elif、#else、#endif 或者 #ifdef/ifndef、#else、#endif 或者 宏函数defined(param)

#ifndef lib_pcap_pcap_h

#def lib_pcap_pcap_h

#if defined(WIN32)

#include <pcap-stdinc.h>

#elif defined(MSDOS)

#include <sys/types.h>

#include <sys/socket.h>

#else

#include <sys/types.h>

#include <sys/time.h>

#endif /** WIN32/MSDOS/*NUX */

#endif

宏函数的好处在于可以判断多个宏,如:#if defined( PCAP1 ) && defined( PCAP2 )

2.C++编译器宏 __cplusplus

字面意思已经很清楚了 C Plus Plus 就是C++的意思啦,在几乎所有的C语言开源项目中都经常会见到如下的条件编译语句:

#ifdef __cplusplus

extern "C" {

#endif

...C语句相关代码段...

#ifdef __cplusplus

}

#endif

说明:exter "C"就是告诉C++编译器{}号内的代码是按照C的object文件格式编译的,要连接的话应该按照C的命名规则去找。exter "C"是让C++能够访问用C写的函数库的一种手段,因为C++和C对函数的处理方式是不同的,所以如果要提示C++编译器按C的方式来处理函数就需要用extern "C"进行说明。

2. typedef作用

该关键字在Linux中的本质其实是为兼容性考虑(如:typedef char int8; 代表着int8为8个bit长度,目前char的长度为8bit,如果几十年以后char不再是8个bit长度而由xxx代表8个bit,则你只需要重新定义一下int8 --- typedef xxx int8; 就可以了,代码中其它用到int8的地方可不用做任何修改

Tags:   linux  c

Comments

Leave a Comment

Name
Content
Verification Type the characters you see in the picture below