diff --git a/english/basic_content/extern/README.md b/english/basic_content/extern/README.md index 41c1ebe..8402ad6 100644 --- a/english/basic_content/extern/README.md +++ b/english/basic_content/extern/README.md @@ -1,24 +1,24 @@ # extern "C" -## 关于作者: +## About Author: -个人公众号: ![](../img/wechat.jpg) -## 1.C++与C编译区别 +## 1. Compiler difference between C and C ++ -在C++中常在头文件见到extern "C"修饰函数,那有什么作用呢? 是用于C++链接在C语言模块中定义的函数。 +In C + +, we often see extern "C" modifier function in the header file. What's the effect. Is a function defined in C language module for C + + link. -C++虽然兼容C,但C++文件中函数编译后生成的符号与C语言生成的不同。因为C++支持函数重载,C++函数编译后生成的符号带有函数参数类型的信息,而C则没有。 +Although C + + is compatible with C, the symbols generated by function compilation in C + + files are different from those generated by C language.Because C + + supports function overloading, the symbols generated by C + + function compilation have the information of function parameter type, while C does not. -例如`int add(int a, int b)`函数经过C++编译器生成.o文件后,`add`会变成形如`add_int_int`之类的, 而C的话则会是形如`_add`, 就是说:相同的函数,在C和C++中,编译后生成的符号不同。 -这就导致一个问题:如果C++中使用C语言实现的函数,在编译链接的时候,会出错,提示找不到对应的符号。此时`extern "C"`就起作用了:告诉链接器去寻找`_add`这类的C语言符号,而不是经过C++修饰的符号。 +Take `int add(int a, int b)` for example. The C + + compiler generates the. O file, `add` becomes `add_int_int` and so on, while C would be like this `_add`, that is:For the same function, in C and C + +, the symbols generated after compilation are different. -## 2.C++调用C函数 +This leads to a problem: if the function implemented in C + + is implemented in C language, an error will occur when compiling the link, indicating that the corresponding symbol cannot be found. At this time`extern "C"` works:Tell the linker to find its `_ C `language symbols such as `add` are not modified by C ++. -C++调用C函数的例子: 引用C的头文件时,需要加`extern "C"` +## 2.C ++ calls C functions + +When referring to the header file of C, you need to add `extern "C"` ```c++ //add.h @@ -44,20 +44,20 @@ int main() { } ``` -编译: +Compile: ``` //Generate add.o file gcc -c add.c ``` -链接: +Link: ``` g++ add.cpp add.o -o main ``` -没有添加extern "C" 报错: +Without extern "C": ```c++ > g++ add.cpp add.o -o main @@ -71,7 +71,7 @@ add.cpp:(.text+0xf):对‘add(int, int)’未定义的引用 collect2: error: ld returned 1 exit status ``` -添加extern "C"后: +With extern "C": `add.cpp` @@ -86,20 +86,20 @@ int main() { return 0; } ``` +When compiling, you must pay attention to generating intermediate files add.o through GCC -编译的时候一定要注意,先通过gcc生成中间文件add.o。 ``` gcc -c add.c ``` -然后编译: +Compile: ``` g++ add.cpp add.o -o main ``` -上述案例源代码见: +Code: - [add.h](extern_c++/add.h) @@ -107,9 +107,10 @@ g++ add.cpp add.o -o main - [add.cpp](extern_c++/add.cpp) -## 2.C中调用C++函数 +## 2.Calling C++ function in C -`extern "C"`在C中是语法错误,需要放在C++头文件中。 + +`extern "C"` It is a syntax error in C, which needs to be put in the C + + header file. ```c // add.h @@ -135,19 +136,19 @@ int main() { } ``` -编译: +Compile: ```c g++ -c add.cpp ``` -链接: +Link: ``` gcc add.c add.o -o main ``` -上述案例源代码见: +Code: - [add.h](extern_c/add.h) @@ -155,11 +156,12 @@ gcc add.c add.o -o main - [add.cpp](extern_c/add.cpp) -综上,总结出使用方法,在C语言的头文件中,对其外部函数只能指定为extern类型,C语言中不支持extern "C"声明,在.c文件中包含了extern "C"时会出现编译语法错误。所以使用extern "C"全部都放在于cpp程序相关文件或其头文件中。 +In the header file of C language, the external function can only be specified as extern type. The declaration of extern "C" is not supported in C language. There will be compiler syntax error when the. C file contains extern "C". Therefore, the use of external "C" is all placed in CPP program related files or its header files. -总结出如下形式: -(1)C++调用C函数: +The following forms are summarized: + +(1)C + + calls C functions: ```c++ //xx.h @@ -176,7 +178,7 @@ extern "C" { } ``` -(2)C调用C++函数 +(2)C calls C + + functions ```c //xx.h