自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++如何調(diào)用寫好的C接口?

開發(fā) 后端
如何在C++代碼中調(diào)用寫好的C接口?你可能會奇怪,C++不是兼容C嗎?直接調(diào)用不就可以了,那么我們來測試一下,先看看C++如何調(diào)用C代碼接口的。

[[428009]]

本文轉(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

  1. #include <stdio.h> 
  2. void print(int a,int b) 
  3.     printf("這里調(diào)用的是C語言的函數(shù):%d,%d\n",a,b); 

一個頭文件test.h

  1. #ifndef _TEST_H 
  2. #define _TEST_H 
  3.  
  4. void print(int a,int b); 
  5.  
  6. #endif 

C++文件調(diào)用C函數(shù)

  1. #include <iostream> 
  2. using namespace std; 
  3. #include "test.h" 
  4. int main() 
  5.    cout<<"現(xiàn)在調(diào)用C語言函數(shù)\n"
  6.    print(3,4); 
  7.    return 0; 

執(zhí)行命令

  1. gcc -c test.c 
  2. 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文件.

  1. $ gcc -c test.c 
  2. $ nm test.o  
  3.                  U _GLOBAL_OFFSET_TABLE_ 
  4. 0000000000000000 T print 
  5.                  U printf 

我們鏈接的時候采用的是 g++ 進(jìn)行鏈接,也就是 C++ 鏈接方式,程序在運(yùn)行到調(diào)用 print 函數(shù)的代碼時,會在符號表中尋找 _Z5printii(是按照C++的鏈接方法來尋找的,所以是找 _Z5printii 而不是找 print)的名字,發(fā)現(xiàn)找不到,所以會提示“未定義的引用”

  1. $ g++ -c test.c 
  2. $ ls 
  3. main.cpp  makefile  test.c  test.h  test.o 
  4. $ nm test.o 
  5.                  U _GLOBAL_OFFSET_TABLE_ 
  6.                  U printf 
  7. 0000000000000000 T _Z5printii 

此時如果我們在對print的聲明中加入 extern “C” ,這個時候,g++編譯器就會按照C語言的鏈接方式進(jìn)行尋找,也就是在符號表中尋找print(這才是C++兼容C),這個時候是可以找到的,是不會報錯的。

總結(jié)

編譯后底層解析的符號不同,C語言是 _print,C++是 __Z5printii

解決調(diào)用失敗問題

修改test.h文件

  1. #ifndef _TEST_H 
  2. #define _TEST_H 
  3. extern "C"
  4. void print(int a,int b); 
  5. #endif 

修改后再次執(zhí)行命令

  1. gcc -c test.c 
  2. g++ -o main main.cpp test.o 
  3. ./main 

運(yùn)行無報錯

思考:那C語言能夠調(diào)用C接口嗎

實(shí)驗:定義main.c函數(shù)如下

  1. #include <stdio.h> 
  2. #include "test.h" 
  3. int main() 
  4.     printf("現(xiàn)在調(diào)用C語言函數(shù)\n"); 
  5.     print(3,4); 
  6.     return 0; 

重新執(zhí)行命令如下

  1. gcc -c test.c 
  2. 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修改如下

  1. #ifndef __TEST_H__ 
  2. #define __TEST_H__ 
  3.  
  4. #ifdef __cplusplus 
  5. #if __cplusplus 
  6. extern "C"
  7. #endif 
  8. #endif /* __cplusplus */ 
  9.  
  10. extern void print(int a,int b); 
  11.  
  12. #ifdef __cplusplus 
  13. #if __cplusplus 
  14. #endif 
  15. #endif /* __cplusplus */ 
  16. #endif /* __TEST_H__ */ 

ps:下期介紹一個Source Insight的插件,快速生成上面的代碼

再次執(zhí)行命令

  1. gcc -c test.c 
  2. gcc -o main main.c test.o 
  3. ./main 

結(jié)果示意:

 

責(zé)任編輯:武曉燕 來源: 編程學(xué)習(xí)基地
相關(guān)推薦

2019-08-28 14:21:39

C++C接口代碼

2020-07-31 18:33:56

C++編程語言

2010-01-28 13:35:41

調(diào)用C++函數(shù)

2019-06-10 19:00:23

Cmain函數(shù)編程語言

2023-11-09 23:31:02

C++函數(shù)調(diào)用

2010-01-26 15:51:06

C++變量

2011-04-08 09:52:44

C++C#DLL

2014-01-02 10:46:35

PostgreSQLC++

2010-01-20 14:35:55

C++調(diào)用

2014-05-15 16:33:05

C++CLI調(diào)用C#

2010-01-20 09:54:27

C++數(shù)據(jù)類型

2010-01-26 14:10:22

Visual C++

2010-01-21 11:23:58

C++函數(shù)調(diào)用

2010-01-28 10:33:10

C++開發(fā)程序

2010-01-14 17:13:53

C++接口

2010-01-21 14:07:14

CC++聲明

2010-01-21 09:34:57

C++語法

2010-01-27 16:05:06

C++堆棧

2023-03-15 15:58:11

Python動態(tài)庫C++

2010-01-28 13:45:06

C++數(shù)組
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號