extern "C" {
时间: 2023-09-18 17:10:11 浏览: 31
This is a syntax used in C++ for indicating that the enclosed code should be compiled using C linkage instead of C++ linkage. This is useful in cases where you want to call C code from C++ code or vice versa, as C and C++ use different name mangling schemes for functions and variables. By using extern "C", you can ensure that the function names are not mangled and can be easily referenced by other code.
相关问题
extern C语言
extern 是 C 语言中的一个关键字,用于声明一个函数或变量是在其他文件中定义的。它可以用来在一个源文件中引用另一个源文件中定义的函数或变量。
当在一个源文件中使用 extern 关键字声明一个函数时,意味着该函数是在另一个源文件中定义的,编译器会在链接阶段将其与实际定义连接起来。
例如,在一个源文件 A.c 中使用 extern 声明一个函数 foo():
extern void foo(); // 函数 foo() 在其他地方定义
然后在另一个源文件 B.c 中定义该函数:
void foo() {
// 函数定义
}
这样,编译器会在链接阶段将 A.c 和 B.c 中的代码连接起来,使得 A.c 中对 foo() 函数的调用能正确执行。
extern c语言举例
C语言中,auto和static关键字用于定义变量的存储类型。
auto: 默认存储类型,在函数体内定义的变量默认为auto。这种类型的变量存储在栈中,作用域仅限于所在函数。
static: 存储类型为静态,在整个程序运行期间都存在。这种类型的变量存储在静态存储区中,作用域仅限于所在文件。
extern: 存储类型为外部,用于在一个文件中声明另一个文件中已经定义过的变量或函数。extern变量或函数只是声明,而不是定义。