问题描述
我发现了一些code最近在何处源文件也为功能加入的externC。他们还加入了他们所宣称的头文件。
I found some code recently where extern "C" was added in source file also for functions. They were also added in the header files where they were declared.
我是假设,即在头文件中加入'的externC就足够了下。
I was under the assumption that adding 'extern "C" in header files was sufficient.
应为externC块被添加?
Where should extern "C" blocks be added?
更新:
假设我编译使用CPP编译我的C code和已经添加的externC卫士在头文件中的所有功能(即我所有的功能都有他们的头原型),但在源文件中我没有加相同。这会带来什么问题?
UPDATE:Suppose I am compiling my C code using a CPP compiler and have added extern "C" guards for all the functions in header files (i.e. all my functions have their prototypes in headers), but in source files I have not added the same. Will this cause a problem?
推荐答案
由于你的意思
extern "C" { ... }
风格警卫,这些声明一些功能是C链接,而不是C ++链接(通常有一堆额外的名称修饰的支持像重载函数的东西)。
style guards, these declare some functions to be of "C" linkage, rather than "C++" linkage (which typically has a bunch of extra name decoration to support things like overloaded functions).
的目的,当然,是允许的C ++ code键用C code,这通常是在库接口。如果库的头,用C ++记不写,的那么他们将不包括的externC
看守C ++ 的
The purpose, of course, is to allow C++ code to interface with C code, which is usually in a library. If the library's headers weren't written with C++ in mind, then they won't include the extern "C"
guards for C++.
记住用C ++编写的C头将包括沿
A C header written with C++ in mind will include something along the lines of
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
,以确保C ++程序看到正确的联动。然而,并不是所有的库用C ++记写的,所以有时你必须做的。
to make sure C++ programs see the correct linkage. However, not all libraries were written with C++ in mind, so sometimes you have to do
extern "C" {
#include "myclibrary.h"
}
,以获得正确的联动。如果头文件是别人提供那么它是不是好的做法改变它(因为你不能轻易地更新它),所以最好换行头文件与自己的后卫(在你自己的头文件可能)。
to get the linkage correct. If the header file is provided by someone else then it's not good practice to change it (because then you can't update it easily), so it's better to wrap the header file with your own guard (possibly in your own header file).
的externC
不是(据我所知)ANSI C,所以不能被包括在普通的C code,而不preprocessor警卫。
extern "C"
isn't (AFAIK) ANSI C, so can't be included in normal C code without the preprocessor guards.
在回答您的编辑:
如果您使用的是C ++编译器,你声明一个函数在头文件的externC,你不也需要声明功能在实现文件为externC。从C ++标准(重点煤矿)第7.5节:
If you are using a C++ compiler, and you declare a function as extern "C" in the header file, you do not need to also declare that function as extern "C" in the implementation file. From section 7.5 of the C++ standard (emphasis mine):
如果相同的两个声明
函数或对象指定不同
联动规范(即,
这些联动规范
声明指定不同
字符串常量),程序
病态的,如果出现的声明
在同一个翻译单元,并且
一个定义规则
如果适用的声明出现在
不同的翻译单位。除
与C ++联动功能,一
函数声明没有联动
规格不得precede的
对于第一个链接规范
功能。 函数可以声明
后不链接规范
一个明确的联动规格有
被看见;联动明确
在前面的声明中指定
不受这样的功能
声明。
我不相信这是很好的做法,虽然,因为没有为联动规范意外分歧(如果,例如,是不是在执行文件中包含包含链接规范的头文件)的潜力。我认为这是更好地在实现文件中明确的。
I'm not convinced it's good practice though, since there's the potential for the linkage specifications to diverge by accident (if, for example, the header file containing the linkage specification isn't included in the implementing file). I think it's better to be explicit in the implementation file.
这篇关于需要它在源文件中还添加了“外部C”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!