cpp关键字extern C,C_Cpp代码相互调用

⌚Time: 2023-08-10 18:21:44

👨‍💻Author: Jack Ge

extern “C”

工程中经常会遇到C和C++混合编程的情况,有时是C++的工程中需要使用C语言编写的库,有时是C的工程需要使用C++;但如果不加任何修饰,直接调用的话,就会遇到链接问题,提示找不到函数名称。

这是由于C++在编译时候,会将函数名做一些修饰,在函数名前加上函数名的长度,在函数名后面加上参数类型。链接的时候也使用相同的策略。C++这么做是由于C++语言支持函数重载,在函数名相同参数不同的几个函数也可以共存。C语言不支持重载,所以编译和链接时对函数名不会加修饰。加extern ”C“就是为了让编译器以C语言的函数名处理方式来编译。

例如函数void fun(int, int),编译后的可能是_fun_int_int(不同编译器可能不同,但都采用了类似的机制,用函数名和参数类型来命名编译后的函数名);而C语言没有类似的重载机制,一般是利用函数名来指明编译后的函数名的,对应上面的函数可能会是_fun这样的名字。

c++调用c语言函数

c语言代码,实现两个int数相加

ma.c


#include <stdio.h>



void print_add(int a, int b){

    printf("%d + %d = %d", a, b, a+b);

}

编译成静态库


gcc ma.c -c

ar rcs liab.lib ma.o

c++代码调用

main.cpp


void print_add(int ,int);

int main(){



    print_add(1, 2);



    return 0;

}

链接过程报错


g++ -static main.cpp liba.lib -o a.exe

C:\Users\m\AppData\Local\Temp\ccWh6m9a.o:main.cpp:(.text+0x18): undefined reference to `print_add(int, int)'

collect2.exe: error: ld returned 1 exit status



也是编译后的函数修饰名的问题,使用nm查看符号名


nm liba.lib



ma.o:

0000000000000000 b .bss

0000000000000000 d .data

0000000000000000 p .pdata

0000000000000000 r .rdata

0000000000000000 r .rdata$zzz

0000000000000000 t .text

0000000000000000 r .xdata

0000000000000000 T print_add

                 U printf

c语言编译器编译后修饰名和原函数名字一样,而c++编译器并不能使用。

解决办法,使用extern "C"告诉c++编译器用c语言的方式去编译它:


extern "C" void print_add(int ,int);

int main(){



    print_add(1, 2);



    return 0;

}

或者


extern "C"{

void print_add(int ,int);



}

int main(){



    print_add(1, 2);



    return 0;

}

编译


g++ -static main.cpp liba.lib -o a.exe

a

1 + 2 = 3

建立一个头文件

ma.h


#ifndef _MA_H_

#define _MA_H_

void print_add(int ,int);

#endif

ma.c


#include <stdio.h>

#include "ma.h"

void print_add(int a, int b){

    printf("%d + %d = %d", a, b, a+b);

}

main.cpp


extern "C"{

#include "ma.h"

}

int main(){



    print_add(1, 2);



    return 0;

}

编译


gcc ma.c -c

ar rcs lia.lib ma.o

c++代码使用


g++ -static main.cpp liba.lib -o a.exe

c语言调用c++函数

一段c++代码

mb.cpp


#include <stdio.h>



void print_hello(){

    printf("hello world!\n");

}

使用g++编译器编译成静态库文件


g++ mb.cpp -c

ar rcs libb.lib mb.o

有一段c语言代码要使用c++的函数

main.c


void print_hello();

int main(){



    print_hello();



    return 0;

}

如果使用c语言编译器gcc进行编译,会报错,找不到符号引用


gcc -static main.c libb.lib - o a.exe

C:\Users\m\AppData\Local\Temp\ccKgFUTo.o:main.c:(.text+0xe): undefined reference to `print_hello'

collect2.exe: error: ld returned 1 exit status

使用nm命令查看静态库文件符号


C:\Users\m\Desktop>nm libb.lib



mb.o:

0000000000000000 b .bss

0000000000000000 d .data

0000000000000000 p .pdata

0000000000000000 r .rdata

0000000000000000 r .rdata$zzz

0000000000000000 t .text

0000000000000000 r .xdata

0000000000000000 T _Z11print_hellov

                 U puts

可以看到导出函数名为_Z11print_hellov,属于c++编译器的函数修饰名形式,而c语言编译器需要的是print_hello,如果要让c语言代码能够使用c++库,方法有两种

第一种

直接使用c++编译器编译c语言代码,c++编译器想要的就是_Z11print_hellov这个符号


g++ -static main.c libb.lib -o a.exe

a

hello world!

但是c++编译器对于语法会进行更严格的检查,就会导致原本在c语言编译器下能够编译通过的代码不能在c++编译器下编译通过,因此这个方法是有缺陷的。

第二种

使用extern "C"修饰导出函数

c++代码可以写成


#include <stdio.h>

extern "C" void print_hello(){

    printf("hello world!\n");

}

或者


#include <stdio.h>

extern "C"{

void print_hello(){

    printf("hello world!\n");

}

}

或者


#include <stdio.h>

extern "C" void print_hello();

void print_hello(){

    printf("hello world!\n");

}

再查看导出符号,发现确实变成了c语言修饰名形式print_hello


nm libb.lib



mb.o:

0000000000000000 b .bss

0000000000000000 d .data

0000000000000000 p .pdata

0000000000000000 r .rdata

0000000000000000 r .rdata$zzz

0000000000000000 t .text

0000000000000000 r .xdata

0000000000000000 T print_hello

                 U puts

使用c语言编译器进行编译,成功输出


gcc -static main.c libb.lib -o a.exe

a

hello world!

为了方便,可以建立一个头文件

mb.h


#ifndef _MB_H_

#define _MB_H_

#ifdef __cplusplus

extern "C"{

#endif

void print_hello();

#ifdef __cplusplus

}

#endif

#endif

源文件

mb.cpp


#include <stdio.h>

#include "mb.h"

void print_hello(){

    printf("hello world!\n");

}

main.c


#include "mb.h"

int main(){



    print_hello();



    return 0;

}

c++编译器再编译时会有定义宏__cplusplus,通过条件编译,使c++编译器能够编译出c语言命名形式的修饰名。对于c语言编译器,不识别extern "C"关键字,在条件编译下也会跳过

需要注意的是,extern “C”中不能出现重载函数,因为这破坏了C++语言函数重载的机制,会导致出现同名函数的冲突。