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