偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

[[428009]]

本文轉(zhuǎn)載自微信公眾號(hào)「編程學(xué)習(xí)基地」,作者deroy 。轉(zhuǎn)載本文請(qǐng)聯(lián)系編程學(xué)習(xí)基地公眾號(hào)。

前言

如何在C++代碼中調(diào)用寫(xiě)好的C接口?你可能會(huì)奇怪,C++不是兼容C嗎?直接調(diào)用不就可以了,那么我們來(lái)測(cè)試一下,先看看C++如何調(diào)用C代碼接口的。

C++調(diào)用C文件

一個(gè)C語(yǔ)言文件test.c

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

一個(gè)頭文件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語(yǔ)言函數(shù)\n"
  6.    print(3,4); 
  7.    return 0; 

執(zhí)行命令

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

編譯后鏈接出錯(cuò):main.cpp對(duì)print(int, int)未定義的引用。

那么g++編譯器為什么找不到print(int,int)呢,其實(shí)在我們學(xué)C++重載的時(shí)候就提到過(guò)C++底層的編譯原理。

原因分析

test.c我們使用的是C語(yǔ)言的編譯器gcc進(jìn)行編譯的,其中的函數(shù)print編譯之后,在符號(hào)表中的名字為 print,通過(guò)nm查看.o文件.

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

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

  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 

此時(shí)如果我們?cè)趯?duì)print的聲明中加入 extern “C” ,這個(gè)時(shí)候,g++編譯器就會(huì)按照C語(yǔ)言的鏈接方式進(jìn)行尋找,也就是在符號(hào)表中尋找print(這才是C++兼容C),這個(gè)時(shí)候是可以找到的,是不會(huì)報(bào)錯(cuò)的。

總結(jié)

編譯后底層解析的符號(hào)不同,C語(yǔ)言是 _print,C++是 __Z5printii

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

修改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)行無(wú)報(bào)錯(cuò)

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

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

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

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

  1. gcc -c test.c 
  2. gcc -o mian main.c test.o 

報(bào)錯(cuò):C語(yǔ)言里面沒(méi)有extern “C“這種寫(xiě)法

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:下期介紹一個(gè)Source Insight的插件,快速生成上面的代碼

再次執(zhí)行命令

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

結(jié)果示意:

 

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

2020-07-31 18:33:56

C++編程語(yǔ)言

2019-08-28 14:21:39

C++C接口代碼

2010-01-28 13:35:41

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

2010-01-26 15:51:06

C++變量

2019-06-10 19:00:23

Cmain函數(shù)編程語(yǔ)言

2023-11-09 23:31:02

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

2010-01-20 14:35:55

C++調(diào)用

2014-01-02 10:46:35

PostgreSQLC++

2011-04-08 09:52:44

C++C#DLL

2014-05-15 16:33:05

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

2025-05-13 10:17:40

C++現(xiàn)代版AI

2010-01-20 09:54:27

C++數(shù)據(jù)類(lèi)型

2010-01-28 10:33:10

C++開(kāi)發(fā)程序

2010-01-26 14:10:22

Visual C++

2010-01-21 11:23:58

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

2010-01-14 17:13:53

C++接口

2010-01-21 14:07:14

CC++聲明

2010-01-15 16:25:48

學(xué)習(xí)C++

2010-01-15 19:28:59

C++

2010-01-21 09:34:57

C++語(yǔ)法
點(diǎn)贊
收藏

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