当为extern&QUOT使用; C"在简单的话? [英] When to use extern "C" in simple words?

查看:106
本文介绍了当为extern&QUOT使用; C"在简单的话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我不理解C和C ++,但什么时候,为什么之间的区别做我们需要使用

Maybe I'm not understanding the differences between C and C++, but when and why do we need to use

extern "C" {

?显然,它是一个联动公约。

? Apparently its a "linkage convention".

我简要地了解它,发现包含MSVS所有的.H头文件围绕其code吧。什么类型的code的到底是C code,而不是C ++ code?我认为C ++包括所有的C code?

I read about it briefly and noticed that all the .h header files included with MSVS surround their code with it. What type of code exactly is "C code" and NOT "C++ code"? I thought C++ included all C code?

我猜,这是情况并非如此,C ++是不同的,在一个存在的标准特性/功能或其他,但不能两者都(如:printf的是C和cout是C ++),但C ++是倒退虽然外部的C声明兼容。这是正确的?

I'm guessing that this is not the case and that C++ is different and that standard features/functions exist in one or the other but not both (ie: printf is C and cout is C++), but that C++ is backwards compatible though the extern "C" declaration. Is this correct?

我的下一个问题要看回答第一个,但我会在这里问问也无妨:因为这是用C MSVS头文件是由外部的C{...}包围,你什么时候会需要使用这个自己在自己的code?如果你的code是C code和你试图编译它在C ++编译器,不应该是没有问题的工作,因为所有的标准.h文件您包括将已经在他们的externC的事与C ++编译器?

My next question depends on the answer to the first, but I'll ask it here anyway: Since MSVS header files that are written in C are surrounded by extern "C" { ... }, when would you ever need to use this yourself in your own code? If your code is C code and you are trying to compile it in a C++ compiler, shouldn't it work without problem because all the standard h files you include will already have the extern "C" thing in them with the C++ compiler?

你有没有用C ++编译,但链接到已建成的C库什么时候用呢?

Do you have to use this when compiling in C++ but linking to already built C libraries or something?

推荐答案

您需要使用的externC声明编译已实现的函数/时在C ++中C.使用的externC告诉编译器/连接器使用C命名和调用约定,而不是C ++名称重整和C ++调用约定将用于除此以外。对于其他图书馆提供的功能,你几乎永远不会需要使用的externC,以及编写库就已经有这个在那里,它出口的公共API这两个C和C ++。但是,如果你写的你想无论是在C和C ++中提供一个图书馆,那么你将不得不把有条件地在你的头。

You need to use extern "C" in C++ when declaring a function that was implemented/compiled in C. The use of extern "C" tells the compiler/linker to use the C naming and calling conventions, instead of the C++ name mangling and C++ calling conventions that would be used otherwise. For functions provided by other libraries, you will almost never need to use extern "C", as well-written libraries will already have this in there for the public APIs that it exports to both C and C++. If, however, you write a library that you want to make available both in C and in C++, then you will have to conditionally put that in your headers.

对于所有的C code是C ++ code ......不,这是不正确的。这是一个流行的神话,C ++是一个C的超集。虽然C ++肯定力争用C尽可能兼容的,也有一些不兼容性的。例如,布尔是有效的C ++,但不是有效的C,而 _Bool 的存在于C99,而不是用在C ++中。

As for whether all C code is C++ code... no, that is not correct. It is a popular myth that C++ is a "superset of C". While C++ certainly strives to be as compatible with C as possible, there are some incompatibilities. For example, bool is valid C++ but not valid C, while _Bool exists in C99, but is not available in C++.

至于是否会永远需要使用externC与系统的.H文件....任何精心设计的实施将有那些在那里等你,这样你就不需要使用它们。不过,可以肯定,提供他们,你应该包括以C开头,并且省略.H相当于头文件。例如,如果包括<文件ctype.h>中几乎任何合理的制度将有外部的C加入;但是,为了保证一个C ++ - 兼容的头,您应该包含头< cctype>

As to whether you will ever need to use extern "C" with the system's ".h" files.... any well-designed implementation will have those in there for you, so that you do not need to use them. However, to be certain that they are provided, you should include the equivalent header file that begins with "c" and omits ".h". For example, if you include <ctype.h>, almost any reasonable system will have the extern "C" added; however, to be assured a C++-compatible header, you should instead include the header <cctype>.

您还可能有兴趣在从C ++ FAQ精简版<混合C和C ++ / A>

You may also be interested in Mixing C and C++ from the C++ FAQ Lite.

这篇关于当为extern&QUOT使用; C&QUOT;在简单的话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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