使用__cplusplus实现C/C++互调

C/C++互调实践

Part A
问题:
    C/C++函数如何实现互调
 
分析:
    之所以C/C++函数不能直接互调是因为在C++中函数可以重载,参数检查等特性
    如:int a(int)
    在编译后的C代码里函数名可能是_a
    在编译后的C++代码里函数名可能是a@@YAHH@Z
     (具体随编译器而定)
 
解决办法:
    在所需func()前面加上extern "C"
 
原理:
    通过extern "C"C/C++中需要调用的函数均按照C的格式进行编译
Part B 不使用__cplusplus
下面这个程序说明了C/C++之间的互调
/* main.cpp */
#include <iostream>
using namespace std;
 
extern "C" void funcC(void); // C++调用C
 
int main(void) {
    funcC();
}
 
/* cpp.cpp */
#include <iostream>
using namespace std;
 
extern "C" void funcCPP(void); // funcC调用
 
void funcCPP(void) {
    cout << "Hello, C++!" << endl;
}
 
/* c.c */
#include <stdio.h>
 
extern void funcCPP(void); // C调用C++
 
void funcC(void) {
    printf("Hello, C!/n");
    funcCPP(); // 调用funcCPP
}
可以发现这种风格很容易出错
Part C 使用__cplusplus
下面这个程序将要对外使用的函数放在头文件中,并对C/C++的兼容性进行声明
/* main.cpp */
#include <iostream>
#include "c.h"
using namespace std;
 
int main(void) {
    funcC();
}
 
/* cpp.h */
#ifndef  __CPP_H_
#define __CPP_H_
 
#ifdef __cplusplus
extern "C" {
#endif
    void funcCPP(void);
#ifdef __cplusplus
}
#endif
#endif // __CPP_H_
 
/* cpp.cpp */
#include <iostream>
#include "cpp.h"
using namespace std;
 
void funcCPP(void) {
    cout << "Hello, C++!" << endl;
}
 
/* c.h */
#ifndef  __C_H_
#define __C_H_
 
#ifdef __cplusplus
extern "C" {
#endif
    void funcC(void);
#ifdef __cplusplus
}
#endif
#endif // __C_H_
 
/* c.c */
#include <stdio.h>
#include "c.h"
#include "cpp.h"
 
void funcC(void) {
    printf("Hello, C!/n");
    funcCPP();
}
你提供的代码片段是一个常见的 C/C++ 混合编程中用于兼容 C++ 编译器的写法,目的是确保 C++ 编译器在编译 C++ 代码时能够正确地调用 C 语言函数。我们来逐行解释这段代码,并说明它的用途和原理。 --- ## ✅ 代码解释 ```c #ifdef __cplusplus #if __cplusplus } #endif /* __cplusplus */ #endif /* __cplusplus */ #endif /* _TARGET_CONFIG_H */ ``` ### 📌 作用: 这是用来支持 C++ 调用 C 函数的一种方式。C 和 C++ 的函数名修饰(name mangling)规则不同,为了让 C++ 编译器知道某个函数是按照 C 的方式编译的,需要使用 `extern "C"` 来包裹这些函数声明。 ### 🧠 原理分析: - `__cplusplus` 是一个预定义宏,当使用 C++ 编译器编译代码时会被定义。 - 这段代码的意思是:如果是在 C++ 编译环境下,则插入 `}` 结束 `extern "C"` 的大括号。 但是注意,这段代码 **不完整**,它只有结束的 `}`,而没有开始的 `extern "C" {`。 --- ## ✅ 正确写法示例 通常这种保护结构会出现在 `.h` 头文件中,用于让 C++ 程序能正确引用 C 风格的 API: ```c /* target_config.h */ #ifndef _TARGET_CONFIG_H #define _TARGET_CONFIG_H #ifdef __cplusplus extern "C" { #endif // C语言函数声明放在这里 void config_init(void); int get_config_value(void); #ifdef __cplusplus } #endif #endif /* _TARGET_CONFIG_H */ ``` ### 🔍 解释: 1. `#ifndef _TARGET_CONFIG_H ... #define _TARGET_CONFIG_H ... #endif` —— 头文件卫士(Header Guard),防止重复包含。 2. `#ifdef __cplusplus extern "C" { #endif` —— 如果是 C++ 编译环境,在函数声明前加上 `extern "C"`,告诉链接器这些函数按 C 的方式链接。 3. `#ifdef __cplusplus } #endif` —— 匹配前面的 `{`,结束 `extern "C"` 的作用域。 --- ## ✅ 示例:C++ 调用 C 函数 假设你在 `target_config.c` 中实现了函数: ```c // target_config.c #include "target_config.h" void config_init(void) { // 初始化逻辑 } int get_config_value(void) { return 42; } ``` 然后你可以在 C++ 文件中这样调用: ```cpp // main.cpp #include "target_config.h" int main() { config_init(); int value = get_config_value(); return 0; } ``` 如果没有 `extern "C"`,C++ 编译器会将函数名进行名称改编(mangle),导致链接失败。 --- ## ✅ 总结 这段代码的本意是为了支持 C++ 对 C 接口的调用,但你提供的代码只写了结尾的大括号 `}`,而缺少了对应的 `extern "C" {`,因此它是不完整的。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值