envy2002 发表于 2013-1-26 15:40:22

linux extern关键字的作用2

extern int O_RDONLY;#include <stdio.h>//包含include <fcntl.h>时,会报错/*main.c:5: error: conflicting types for 'open'/usr/include/fcntl.h:72: error: previous declaration of 'open' was here*/#include<fcntl.h>//#include <sys/stat.h>int open( const char * pathname, int flags){printf("string is %s\n",pathname);return 0;}int main(int argc, char **argv){printf("flag is %d",O_RDONLY);//open("my lady gaga",34);return 0;}//我们在这里故意写和fcntl.h中定义的函数 int open( const char * pathname, int flags);一致//看看为什么在不包含fcntl.h时不报错,但是包含这个fcntl.h头文件时报错//why, because 编译器链接范围的问题!//c语言编译是个单个文件编译过程。//以现在main.c为中心,这是个闭包,include的符号(函数,变量,包括include<头文件>,extern声明//为外部符号,在链接时用到。//1.当有extern int O_RDONLY时,如果没有include<fcntl.h>,那么会报未定义的错误,//增加则不会//2.如果有int open函数,如果没有 include <fcntl.h>是不会报错的,有会报错这些错误的猜想:应该归于宏定义的问题,而不是链接的问题。应该连接器还是知道去哪里链接的,但是由于宏定义的问题,使其出错。
页: [1]
查看完整版本: linux extern关键字的作用2