C++如何調(diào)用寫好的C接口?
本文轉(zhuǎn)載自微信公眾號「編程學(xué)習(xí)基地」,作者deroy 。轉(zhuǎn)載本文請聯(lián)系編程學(xué)習(xí)基地公眾號。
前言
如何在C++代碼中調(diào)用寫好的C接口?你可能會奇怪,C++不是兼容C嗎?直接調(diào)用不就可以了,那么我們來測試一下,先看看C++如何調(diào)用C代碼接口的。
C++調(diào)用C文件
一個C語言文件test.c
- #include <stdio.h>
- void print(int a,int b)
- {
- printf("這里調(diào)用的是C語言的函數(shù):%d,%d\n",a,b);
- }
一個頭文件test.h
- #ifndef _TEST_H
- #define _TEST_H
- void print(int a,int b);
- #endif
C++文件調(diào)用C函數(shù)
- #include <iostream>
- using namespace std;
- #include "test.h"
- int main()
- {
- cout<<"現(xiàn)在調(diào)用C語言函數(shù)\n";
- print(3,4);
- return 0;
- }
執(zhí)行命令
- gcc -c test.c
- g++ -o main main.cpp test.o
編譯后鏈接出錯:main.cpp對print(int, int)未定義的引用。
那么g++編譯器為什么找不到print(int,int)呢,其實(shí)在我們學(xué)C++重載的時候就提到過C++底層的編譯原理。
原因分析
test.c我們使用的是C語言的編譯器gcc進(jìn)行編譯的,其中的函數(shù)print編譯之后,在符號表中的名字為 print,通過nm查看.o文件.
- $ gcc -c test.c
- $ nm test.o
- U _GLOBAL_OFFSET_TABLE_
- 0000000000000000 T print
- U printf
我們鏈接的時候采用的是 g++ 進(jìn)行鏈接,也就是 C++ 鏈接方式,程序在運(yùn)行到調(diào)用 print 函數(shù)的代碼時,會在符號表中尋找 _Z5printii(是按照C++的鏈接方法來尋找的,所以是找 _Z5printii 而不是找 print)的名字,發(fā)現(xiàn)找不到,所以會提示“未定義的引用”
- $ g++ -c test.c
- $ ls
- main.cpp makefile test.c test.h test.o
- $ nm test.o
- U _GLOBAL_OFFSET_TABLE_
- U printf
- 0000000000000000 T _Z5printii
此時如果我們在對print的聲明中加入 extern “C” ,這個時候,g++編譯器就會按照C語言的鏈接方式進(jìn)行尋找,也就是在符號表中尋找print(這才是C++兼容C),這個時候是可以找到的,是不會報錯的。
總結(jié)
編譯后底層解析的符號不同,C語言是 _print,C++是 __Z5printii
解決調(diào)用失敗問題
修改test.h文件
- #ifndef _TEST_H
- #define _TEST_H
- extern "C"{
- void print(int a,int b);
- }
- #endif
修改后再次執(zhí)行命令
- gcc -c test.c
- g++ -o main main.cpp test.o
- ./main
運(yùn)行無報錯
思考:那C語言能夠調(diào)用C接口嗎
實(shí)驗:定義main.c函數(shù)如下
- #include <stdio.h>
- #include "test.h"
- int main()
- {
- printf("現(xiàn)在調(diào)用C語言函數(shù)\n");
- print(3,4);
- return 0;
- }
重新執(zhí)行命令如下
- gcc -c test.c
- gcc -o mian main.c test.o
報錯:C語言里面沒有extern “C“這種寫法
C接口既能被C++調(diào)用又能被C調(diào)用
為了使得test.c代碼既能被C++調(diào)用又能被C調(diào)用
將test.h修改如下
- #ifndef __TEST_H__
- #define __TEST_H__
- #ifdef __cplusplus
- #if __cplusplus
- extern "C"{
- #endif
- #endif /* __cplusplus */
- extern void print(int a,int b);
- #ifdef __cplusplus
- #if __cplusplus
- }
- #endif
- #endif /* __cplusplus */
- #endif /* __TEST_H__ */
ps:下期介紹一個Source Insight的插件,快速生成上面的代碼
再次執(zhí)行命令
- gcc -c test.c
- gcc -o main main.c test.o
- ./main
結(jié)果示意: