C 与 C++ 接口函数相互调用

一、C 或 C++ 编译的四个步骤

(一) 预处理

在该步骤中,编译器将源程序中以“#”开头的语句进行处理。其中,#include 的原理是将目标文件内容导入本文件。

(二) 编译

在该步骤中,编译器将第一步生成的各个文件分别转换成汇编语言文件。在该过程中,所有函数的名称都会被转换成一个符号作为汇编文件中的唯一标识,对 C 语言函数一般直接用函数名称作为其唯一标识的符号,而对于 C++ 函数在多数情况下需要在函数名称加上各种前缀或后缀才能作为其标识,比如函数 void Print(int num),如果编译器将其视为 C 语言编译,则该函数在汇编文件中的符号为 Print,若视为 C++,则其符号可能为 Print_int(在 gcc 或 g++ 中函数名称的改变还会考虑命名空间等因素),这也是 C++ 支持函数重载的原因。

(三) 汇编

在该步骤中,编译器将第二步生成的各个文件分别转换为二进制文件,但还不是可执行文件。

(四) 链接

在该步骤中,编译器会为第三步生成的每一个文件“穿针引线”,比如 main() 函数中调用了 Print() 函数,还不知道 Print() 函数在哪里,而在 Print() 函数主体所在的那个文件中,已经标明了 Print() 函数的地址,所以编译器会在 main() 函数中调用 Print() 函数的地方标注 Print() 函数的地址,为程序执行过程中的地址跳转提供目标地址,而编译器能做到这一步的前提,是 main() 函数中 Print() 函数的标识,和 Print() 函数主体所在的那个文件中 Print() 函数的标识是一模一样的,如果不一样,就会触发链接错误。


二、C 与 C++ 接口相互调用的关键

从上文可以得知,要调用一个函数有一个重要条件就是调用处的符号和函数主体处的符号要一模一样,而 C 和 C++ 在编译过程中将函数名称改编成标识符号的方法是不一样的,因此相互调用的关键在于统一接口函数的标识符号,而一般采取的方法是,用 C 函数改编的方法统一接口函数的改编方式。


三、extern “C”

extern "C" 的作用是告诉编译器按 C 函数名称改编的方法将修饰的函数改编成标识符号。extern "C" 一般用在 C++ 文件中。

extern "C" void Print(int num);
extern "C" {
    void Input(int* num);
    void Output(int num);
};

以上是 extern "C" 的两种写法。如此一来,以上三个函数都会按 C 的方式被改编成符号,在 gcc 或 g++ 编译下就会被改变成 PrintInputOutput


四、C 函数调用 C++ 接口

(一) 调用非成员函数

被调用函数的声明和定义如下。

/**
 * called.h
 */
#ifndef CALLED_H
#define CALLED_H

extern "C" void PrintCpp(void);

#endif
/**
 * called.cpp
 */
#include <iostream>
#include "called.h"

using namespace std;

void
PrintCpp(void) {
    cout << "I\'m cpp." << endl;
}

最终调用如下。

/**
 * call.c
 */
#include "called.h"

int
main(int argc, char const* argv[]) {
    PrintCpp();
    return 0;
}

(二) 调用类成员函数(接口函数没有类指针)

被调用函数声明和定义如下。

/**
 * called.h
 */
#ifndef CALLED_H
#define CALLED_H

class Console {
public:
    Console();
    virtual void PrintDouble(double num);
};

extern "C" void CppPrintDouble(double num);

#endif
/**
 * called.cpp
 */
#include <iostream>
#include "called.h"

using namespace std;

Console::Console() {}

void
Console::PrintDouble(double num) {
    cout << num << endl;
}

Console* console = new Console();

void
CppPrintDouble(double num) {
    console->PrintDouble(num);
}

最终调用如下。

/**
 * call.c
 */
#include "called.h"

int
main(int argc, char const* argv[]) {
    CppPrintDouble(3.14);
    return 0;
}

五、C++ 函数调用 C 接口

被调用函数的声明和定义如下。

/**
 * called.h
 */
#ifndef CALLED_H
#define CALLED_H

void PrintC(void);

#endif
/**
 * called.c
 */
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
#include "called.h"
#ifdef __cplusplus
};
#endif

void
PrintC(void) {
    printf("I\'m C.\n");
}

最终调用如下。

/**
 * call.cpp
 */
#ifdef __cplusplus
extern "C" {
#endif
#include "called.h"
#ifdef __cplusplus
};
#endif

int
main(int argc, char const* argv[]) {
    PrintC();
    return 0;
}

在 called.c 文件中,#ifdef __cplusplus /*...*/ #endifextern "C" 的作用是防止 g++ 编译器对“.c”文件用 C++ 的方式编译,如果用 gcc 进行编译,则直接写 #include "called.h" 就行。

猜你喜欢

转载自blog.csdn.net/weixin_36725931/article/details/85231240