将外部“C”放入C++代码中具体做什么?
例如:
extern "C" {
void foo();
}
将外部“C”放入C++代码中具体做什么?
例如:
extern "C" {
void foo();
}
当前回答
extern“C”是一个链接规范,用于调用Cpp源文件中的C函数。我们可以调用C函数、编写变量和包含头。函数在外部实体中声明,并在外部定义。语法为
类型1:
extern "language" function-prototype
类型2:
extern "language"
{
function-prototype
};
eg:
#include<iostream>
using namespace std;
extern "C"
{
#include<stdio.h> // Include C Header
int n; // Declare a Variable
void func(int,int); // Declare a function (function prototype)
}
int main()
{
func(int a, int b); // Calling function . . .
return 0;
}
// Function definition . . .
void func(int m, int n)
{
//
//
}
其他回答
在不与其他好答案冲突的情况下,我将添加一点我的示例。
C++编译器的作用是:它在编译过程中破坏了名称,因此我们需要告诉编译器要特别对待C实现。
当我们制作C++类并添加外部“C”时,我们告诉C++编译器我们正在使用C调用约定。
原因(我们从C++调用C实现):要么我们想从C++调用C++函数,要么从C调用C++函数(C++类…等在C中不起作用)。
它以这样一种方式更改函数的链接,即该函数可以从C调用。实际上,这意味着函数名不会被破坏。
它通知C++编译器在链接时以C样式查找这些函数的名称,因为在链接阶段,用C和C++编译的函数的名称不同。
当混合使用C和C++时(即a.从C++调用C函数;b.从C调用C++函数),C++名称混乱会导致链接问题。从技术上讲,只有当被调用函数已经使用相应的编译器编译成二进制(很可能是*.a库文件)时,才会出现此问题。
因此,我们需要使用外部“C”来禁用C++中的名称篡改。
分解一个g++生成的二进制文件,看看发生了什么
主.cpp
void f() {}
void g();
extern "C" {
void ef() {}
void eg();
}
/* Prevent g and eg from being optimized away. */
void h() { g(); eg(); }
编译并反汇编生成的ELF输出:
g++ -c -std=c++11 -Wall -Wextra -pedantic -o main.o main.cpp
readelf -s main.o
输出包含:
8: 0000000000000000 7 FUNC GLOBAL DEFAULT 1 _Z1fv
9: 0000000000000007 7 FUNC GLOBAL DEFAULT 1 ef
10: 000000000000000e 17 FUNC GLOBAL DEFAULT 1 _Z1hv
11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _GLOBAL_OFFSET_TABLE_
12: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _Z1gv
13: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND eg
理解
我们看到:
ef和eg存储在与代码中同名的符号中其他符号都被弄乱了。让我们解开它们:$c++过滤器_Z1fvf()$c++过滤器_Z1hvh()$c++过滤器_Z1gvg()
结论:以下两种符号类型均未受损:
定义已声明但未定义(Ndx=UND),将在链接或运行时从另一个对象文件提供
因此,在调用以下两个函数时,都需要外部“C”:
C++中的C:告诉g++期望gcc生成的未成文法的符号来自C的C++:告诉g++生成未成文法的符号供gcc使用
在extern C中不起作用的东西
很明显,任何需要更改名称的C++特性都不会在外部C中工作:
extern "C" {
// Overloading.
// error: declaration of C function ‘void f(int)’ conflicts with
void f();
void f(int i);
// Templates.
// error: template with C linkage
template <class C> void f(C i) { }
}
C++示例中的最小可运行C
为了完整性和新手,请参阅:如何在C++项目中使用C源文件?
从C++调用C非常简单:每个C函数只有一个可能的非损坏符号,因此不需要额外的工作。
主.cpp
#include <cassert>
#include "c.h"
int main() {
assert(f() == 1);
}
c.h
#ifndef C_H
#define C_H
/* This ifdef allows the header to be used from both C and C++
* because C does not know what this extern "C" thing is. */
#ifdef __cplusplus
extern "C" {
#endif
int f();
#ifdef __cplusplus
}
#endif
#endif
c.c
#include "c.h"
int f(void) { return 1; }
Run:
g++ -c -o main.o -std=c++98 main.cpp
gcc -c -o c.o -std=c89 c.c
g++ -o main.out main.o c.o
./main.out
如果没有外部“C”,链接将失败:
main.cpp:6: undefined reference to `f()'
因为g++希望找到一个损坏的f,而gcc并没有产生。
GitHub上的示例。
C示例中的最小可运行C++
从C调用C++有点困难:我们必须手动创建要公开的每个函数的未损坏版本。
这里我们说明如何将C++函数重载暴露给C。
主.c
#include <assert.h>
#include "cpp.h"
int main(void) {
assert(f_int(1) == 2);
assert(f_float(1.0) == 3);
return 0;
}
每小时
#ifndef CPP_H
#define CPP_H
#ifdef __cplusplus
// C cannot see these overloaded prototypes, or else it would get confused.
int f(int i);
int f(float i);
extern "C" {
#endif
int f_int(int i);
int f_float(float i);
#ifdef __cplusplus
}
#endif
#endif
cpp.cpp
#include "cpp.h"
int f(int i) {
return i + 1;
}
int f(float i) {
return i + 2;
}
int f_int(int i) {
return f(i);
}
int f_float(float i) {
return f(i);
}
Run:
gcc -c -o main.o -std=c89 -Wextra main.c
g++ -c -o cpp.o -std=c++98 cpp.cpp
g++ -o main.out main.o cpp.o
./main.out
如果没有外部“C”,则失败:
main.c:6: undefined reference to `f_int'
main.c:7: undefined reference to `f_float'
因为g++生成了gcc找不到的损坏符号。
GitHub上的示例。
当我包含c++中的c头时,外部“c”在哪里?
C++版本的C头文件(如csdio)可能依赖于#pragma GCC system_headerhttps://gcc.gnu.org/onlinedocs/cpp/System-Headers.html提到:“在一些目标上,例如RS/6000AIX,当编译为C++时,GCC隐式地用‘extern‘C‘块包围所有系统头”,但我没有完全确认。像/usr/include/unistd.h这样的POSIX头包含在:我需要一个外部“C”块来包含标准的POSIX C头吗?通过__BEGIN_DECLS,在Ubuntu 20.04上再现__BEGIN_DECLS通过#include<features.h>包含。
在Ubuntu 18.04中测试。