c/c++ 互相调用

1
2
3

  1. C如何调用CPP代码
    在C中如何调用C++函数的问题,简单回答是将函数用extern "C"声明; 然后C代码中不要include C++的头文件, 而采用直接在C中增加函数声明的方式;
    例子
/*C++ code*/
extern "C" void f(int);
void f(int i)
{
// your code
}

/*C code*/
void f(int);  // 不引入, 而只是直接声明
void cc(int i)
{
f(i);  //调用
// other code
}

如果想要调用C++类中的成员函数, 由于C中没有类, 因此需要一个包装函数来调用这个成员函数, 并返回结果;
如果你想要在C里调用成员函数(包括虚函数),则需要提供一个简单的包装(wrapper)。例如:

// C++ code:
class C
{
// ...
virtual double f(int);
};

extern "C" double call_C_f(C* p, int i) // wrapper function
{
return p->f(i);
}

然后,你就可以这样调用C::f():

/* C code: */
double call_C_f(struct C* p, int i);
void ccc(struct C* p, int i)
{
double d = call_C_f(p,i);
/* ... */
}
  1. C++中如何调用C代码
    在include的时候, 要采用extern "C" 代码块形式.
extern "C" {
    #include "lua.h"  // lua.h整个头文件中的所有函数都是分布在多个xxx.c文件中的, 因此肯定xxx.obj是按照Ccompiler规则编译, 函数名无改动, 
                               // 那么, C++中引用头文件的时候, 要在外部加上extern "C"包裹, 表示说我知道这部分是采用Ccompiler规则的函数名, 我会采用这个规则去找函数;
}
//-----------------------------------
#include <iostream>
extern "C" {
    #include "add.h"  // 由add.h和add.c组成
}
using namespace std;

int main() {
    cout << addTwoNumber(10, 20) << endl;
    system("pause");
    return 0;
}

如果去掉extern "C"代码块形式, 则出现LNK2019错误: vs ide

著名的LNK2019
note:
大多数情况下, 我们为了使用C++的类和方便的map, vector等库, 采用的是CPP为主, 调用一些C库为辅助.
那么, 这种情况下, 其实第二种情形是更为常见的;

C代码调用C++函数
hello.h

#ifndef H_HELLO
#define H_HELLO
#ifdef __cplusplus
extern "C" {
#endif
int getAge();
int getCount();
#ifdef __cplusplus
}
#endif
#endif

hello.cpp

#include <iostream>
#include "hello.h"
int getAge() {
    std::cout << "get age" << std::endl;
    return 99;
}
int getCount() {
    std::cout << "get count" << std::endl;
    return 123456;
}

编译为动态链接库

 g++ -fPIC -shared -o libhello.so hello.cpp

main.c

include <stdio.h>
#include "hello.h"

int main() {
    int age = getAge();
    int count = getCount();
    printf("%d:%d\n", age, count);
    return 0;
}

gcc main.c -L. -lhello -o main

makefile自动化

main: main.c libhello.so
    gcc main.c -L. -lhello -o main
libhello.so: hello.cpp
    g++ -fPIC -shared -o libhello.so hello.cpp
clean:
    rm -f *.o *.so main

至此,已经实现了C代码调用C++自定义库函数
验证混合调用
main.cpp

#include <iostream>
#include "hello.h"

int main() {
    int age = getAge();
    std::cout << age << ":" << getCount() << std::endl;
    return 0;
}

g++ main.cpp -L. -lhello -o main

可以看出,C++、C代码可以共享函数getAge(), getCount()
注意事项
__cplusplus前面是两个下划线

极简式从C调用C++类方法

如果要从C内部从C ++调用类方法,可以使用类包装器。
此方法的一个优点是C ++类保持不变,
甚至可以存在于库中。
首先,让我们定义C ++类“ Circle”。为简单起见,我们将
在.h文件中进行所有操作,但对于在.h中声明并
在.cpp文件中定义的类,它也同样有效。

// Circle.h - a C++类
 
#ifndef CIRCLE_H
#define CIRCLE_H
 
class Circle {
    public:
        Circle(float radius):_radius(radius) {}
        float getArea() { return 3.14159 * _radius * _radius; }
    private:
        float _radius;
};
 
#endif

现在让我们为该类声明一个C ++包装器,该包装器声明
可以在C内部使用的extern C方法。此代码必须在C ++和C文件中都可以编译。
使用void *指向类实例。注意使用#ifdef __cplusplus。

/* Circle_C.h - 必须同时在C和C ++中进行编译*/
 
#ifndef Circle_C_H
#define Circle_C_H
 
    #ifdef __cplusplus
    extern "C" {
    #endif
 
    extern void *Circle_C_new(float radius);
    extern void  Circle_C_delete(void *circle);
    extern float Circle_C_getArea(void *circle);
 
    #ifdef __cplusplus
    }
    #endif
 
#endif

现在定义外部函数。它们只会编译为C ++,因此

可以引用类。

// Circle_C.cpp - 外部C函数定义
 
#include "Circle_C.h"
#include "Circle.h"
 
extern void *Circle_C_new(float radius) {
    return new Circle(radius);
}
 
extern void Circle_C_delete(void *circle) {
    Circle *c = (Circle *)circle;
    delete c;
}
 
extern float Circle_C_getArea(void *circle) {
    Circle *c = (Circle *)circle;
    return c->getArea();
}

现在,我们可以使用这些extern C函数来从C访问C ++类。这

是C主函数示例:

 /* mixed.c - 访问C ++方法的C文件*/
 
#include <stdio.h>
#include "Circle_C.h"
 
void main() {
    float radius = 1.5;
 
    // 获取指向Circle对象的指针
    void *circle = Circle_C_new(radius);
 
    // 将Circle对象传递给wrapper方法
    float area = Circle_C_getArea(circle);
 
    printf ("Circle of radius %f has area %f\n", radius, area);
 
    // 释放Circle对象的内存
    Circle_C_delete(circle);
}

这是一个示例制作文件,用于创建“混合”可执行文件。

# Makefile-创建可执行文件“ mixed”。必须链接stdc ++库
 
mixed: main.o Circle.o
    gcc -lstdc++ -o mixed main.o Circle_C.o
 
#将main编译为C
main.o: main.c
    gcc -c main.c -o main.o
 
# 将Circle_C编译为C ++
Circle_C.o: Circle_C.cpp
    g++ -c Circle_C.cpp -o Circle_C.o

原文地址:https://www.cnblogs.com/marklove/p/15620438.html