用C extern函数VS C ++ [英] Extern functions in C vs C++
问题描述
在 *的.h
A C
库的头文件,应声明函数
的extern无效F();//或者只空隙f()的;
- 仅在
下,用时
- 。 从
C ++
使用时有[近]从来没有任何需要使用关键字的extern声明一个功能时,无论是用C
或者C ++。在C和C ++中的所有功能都默认外部链接。声明在头文件中的功能与怪习惯的extern
可能有一定的历史根源,但它已经完全不相干了几十年了。
还有一个[模糊?]从C,这可能是没有直接关系的上述异常你问一下:C语言(C99),如果在一些翻译单元功能被定义为在线
,并声明为的extern
(显式的的extern
使用),那么内嵌该函数的定义也作为的外部定义的。如果有明确的extern没有声明
是翻译单元present,那么内联定义用作内部的定义只。
P.S。还有如的externC
在C ++中这样的事情,但是这是一个完全不同的事情。
In *.h
header files of a C
library, should one declare functions
extern void f();
// or only
void f();
- when using only in
C
- when using from
C++
.
There's [almost] never any need to use the keyword extern
when declaring a function, either in C or in C++. In C and in C++ all functions have external linkage by default. The strange habit of declaring functions in header files with extern
probably has some historical roots, but it has been completely irrelevant for decades already.
There's one [obscure?] exception from the above in C, which is probably not directly related to what you are asking about: in C language (C99) if in some translation unit a function is defined as inline
and also declared as extern
(an explicit extern
is used) then the inline definition of that function also serves as an external definition. If no declarations with explicit extern
are present in the translation unit, then the inline definition is used as "internal" definition only.
P.S. There's such thing as extern "C"
in C++, but that is a completely different matter.
这篇关于用C extern函数VS C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!