用C extern函数VS C ++ [英] Extern functions in C vs C++

查看:132
本文介绍了用C extern函数VS C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*的.h A C 库的头文件,应声明函数

 的extern无效F();//或者只空隙f()的;


  1. 仅在下,用时

  2. 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();

  1. when using only in C
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆