是否需要在源文件中添加“extern C”? [英] Is it required to add 'extern C' in source file also?

查看:287
本文介绍了是否需要在源文件中添加“extern C”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近找到一些代码,其中externC被添加到源文件中也用于函数。它们也被添加到声明它们的头文件中。



我假设在头文件中添加externC是足够的。



应在何处添加externC块?



UPDATE:
假设我正在编译我的C代码使用CPP编译器,并为头文件中的所有函数添加了externC保护(即所有我的函数都有他们的原型在头文件),但在源文件中我没有添加相同。这会造成问题吗?

解决方案

因为你是指

  externC{...} 

声明一些函数为C链接,而不是C ++链接(通常有一堆额外的名称装饰来支持类似重载函数)。



当然,目的是允许C ++代码与C代码接口,C代码通常在库中。如果图书馆的标题不是用C ++编写的,那么它们不会包括 externC用于C ++的守卫。

使用C ++编写的AC标题将包括

行中的内容

  #ifdef __cplusplus 
externC{
#endif

...

#ifdef __cplusplus
}
#endif

以确保C ++程序看到正确的链接。但是,并不是所有的库都是用C ++编写的,所以有时你必须做

  externC{
#includemyclibrary.h
}

如果头文件是由别人提供的,那么改变它是不好的做法(因为你不能轻松地更新它),所以最好用你自己的头文件(可能在你自己的头文件)。



externC不是(AFAIK)ANSI C,因此不能包含在正常C



如果您使用的是C ++编译器,并且在头文件中声明一个函数为externC,则不需要在实现文件中将该函数声明为externC。从C ++标准的第7.5节(强调我):


如果同一个
函数或对象的两个声明指定不同的
linkage-specifications(即,
链接 - 这些
声明的规范指定不同的
字符串文字),程序是
,如果声明出现
在同一翻译单元中,并且如果声明出现在
不同的翻译单元中,则应用
一个定义规则
。对于具有C ++链接的函数,
除外,不带链接的
函数声明
规范不应在
函数的
第一个链接规范之前。 一个函数可以声明为
,没有连接规范,
后显式链接规范已经看到
;在早期声明
中指定的链接显式
不受这样的函数
声明影响。


我不相信这是一个好的做法,虽然,因为有潜在的链接规格发生意外分歧(例如,如果包含链接规范的头文件不包括在实现文件中) 。我认为最好是在实现文件中显式。


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.

I was under the assumption that adding 'extern "C" in header files was sufficient.

Where should extern "C" blocks be added?

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?

解决方案

Since you mean

extern "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).

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++.

A C header written with C++ in mind will include something along the lines of

#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif

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).

extern "C" isn't (AFAIK) ANSI C, so can't be included in normal C code without the preprocessor guards.

In response to your edit:

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):

If two declarations of the same function or object specify different linkage-specifications (that is, the linkage-specifications of these declarations specify different string-literals), the program is ill-formed if the declarations appear in the same translation unit, and the one definition rule applies if the declarations appear in different translation units. Except for functions with C++ linkage, a function declaration without a linkage specification shall not precede the first linkage specification for that function. A function can be declared without a linkage specification after an explicit linkage specification has been seen; the linkage explicitly specified in the earlier declaration is not affected by such a function declaration.

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.

这篇关于是否需要在源文件中添加“extern C”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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