From 0c337b49cfa9f03670d74eb001bd331c8033e261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=88=E6=9C=9B?= Date: Fri, 24 Jul 2020 18:53:52 +0800 Subject: [PATCH] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 示例常规做法及其原因。 --- basic_content/extern/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/basic_content/extern/README.md b/basic_content/extern/README.md index 41c1ebe..dd75f50 100644 --- a/basic_content/extern/README.md +++ b/basic_content/extern/README.md @@ -99,6 +99,21 @@ gcc -c add.c g++ add.cpp add.o -o main ``` +而通常为了C代码能够通用,即既能被C调用,又能被C++调用,头文件通常会有如下写法: + +```c +#ifdef __cplusplus +extern "C"{ +#endif +int add(int x,int y); +#ifdef __cplusplus +} +#endif +``` + +即在C++调用该接口时,会以C接口的方式调用。这种方式使得C++者不需要额外的extern C,而标准库头文件通常也是类似的做法,否则你为何不需要extern C就可以直接使用stdio.h中的C函数呢? + + 上述案例源代码见: - [add.h](extern_c++/add.h) @@ -191,3 +206,5 @@ int add(){ extern int add(); ``` + +不过与C++调用C接口不同,C++确实是能够调用编译好的C函数,而这里C调用C++,不过是把C++代码当成C代码编译后调用而已。也就是说,C并不能直接调用C++库函数。