7月30日

来源:互联网 发布:美容美发会员收银软件 编辑:程序博客网 时间:2024/04/28 11:24

Using extern to Specify Linkage


extern string-literal { declaration-list }extern string-literal declaration

Remarks

The extern keyword declares a variable or function and specifies that it has external linkage 

http://msdn.microsoft.com/en-us/library/0603949d(v=VS.80).aspx


 文章 C和C++函数的相互引用 extern "c"深入理解 谈到了一些extern"C"的方法。引用文章中的几个结论:

       (1)在C++中引用C的函数和变量时,头文件中要有 extern "C" { #include "Cfile.h" }

       (2)在C中引用C++语言中的函数和变量时,C++的头文件需添加extern "C",但是在C语言中不能直接引用声明了extern "C"的该头文件,应该仅将C文件中将C++中定义的extern "C"函数声明为extern类型。

       但是对于文中提到“如果在模块A中函数声明了foo为extern "C"类型,而模块B中包含的是extern int foo( int x, int y ) ,则模块B找不到模块A中的函数;反之亦然。”这和B.cpp的包

含头文件的顺序有关系。如果是 #include "a.h" 然后 #include "b.h" 则没有问题,如果是 #include "b.h" 然后#include "a.h" 则报错 “error C2732: linkage specification contradicts 

earlier specification for 'foo'”。或者在后者的基础上在b.h中将extern int foo( int x, int y ); 改为:extern "C" {extern int foo( int x, int y );} 编译即可通过。


http://blog.sina.com.cn/s/blog_675662490100i81b.html