C是如何调用C++接口的?

✨有的时候,当我想在c代码中调用c++接口,然后就编译报错了!!!

引出问题

C++支持函数重载的, 函数名相同但是参数不同的重载函数在编译后链接的名字并不相同而可以被识别, 这种情况下, 我们引入一个中间层的方法同样可以实现C中调用C++的函数接口。

其实这与C中调用C++非重载基本函数成员的实现没有什么区别, 只是为各个重载函数均实现一套接口而已。

通常的做法

//test.cpp
#include <iostream>
#include "hello.h"
int getNum() {
    std::cout << "get number" << std::endl;
    return 123456;
}

这时,我们需要对该c++函数,进行声明,告诉编译器,需要按c的命名规则来:

//test.h
#ifdef __cplusplus
extern "C" {
#endif

int getNum();

#ifdef __cplusplus
}
#endif
#endif

这时,我们就可以在C源码中调用getNum接口了。

重载函数

如果你想在 C 里调用重载函数,则必须提供不同名字的包装,这样才能被 C 代码调用。首先是我们的C++接口, 如下所示:

//  add.cpp
//#include <iostream>
int add(const int a, const int b)
{
    return (a + b);
}

double add(const double a, const double b)
{
    //std::cout <<a <<", " <<b <<std::endl;
    return (a + b);
}

我们为此实现一个中间层libadd.cpp, 通过C++编译器用extern "C"将其编译成C编译器可识别的接口:

// libadd.cpp
int add(const int a, const int b);
double add(const double a, const double b);

#ifdef __cplusplus
extern "C"
{
#endif

int call_cpp_add_int(const int a, const int b)
{
    return add(a, b);
}

double call_cpp_add_double(const double a, const double b)
{
    return add(a, b);
}

#ifdef __cplusplus
}
#endif

最后是我们的C源程序, 调用我们的中间层:

//  main.c
#include <stdio.h>
#include <stdlib.h>


int call_cpp_add_int(const int a, const int b);
double call_cpp_add_double(const double a, const double b);

int main( )
{
    printf("2 + 4 = %d\n", call_cpp_add_int(2, 4));
    printf("2.1 + 4.5 = %lf\n", call_cpp_add_double(2.1, 4.5));

    return 0;
}

唔,完美!

当然也可以把函数声明括起来~

// libadd.cpp
int add(const int a, const int b);
double add(const double a, const double b);

#ifdef __cplusplus
extern "C"
{
#endif

int call_cpp_add_int(const int a, const int b);
double call_cpp_add_double(const double a, const double b);

#ifdef __cplusplus
}
#endif
int call_cpp_add_int(const int a, const int b)
{
    return add(a, b);
}

double call_cpp_add_double(const double a, const double b)
{
    return add(a, b);
}

关注&&联系

gitee: https://gitee.com/cmcc-oneos/OneOS-Lite

docs: https://oneos-lite.com/

引用自:https://gitee.com/cmcc-oneos/OneOS-Lite/blob/dev/docs/quick_guide/cplusplus/c-call-c++.md

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值