C語言與C++項目實現(xiàn)相互調(diào)用
前言
extern “c”的作用可以實現(xiàn)c語言和c++相互調(diào)用。
1.當我們寫c語言代碼,要調(diào)用c++代碼時,可以將c++的類型配置為靜態(tài)庫,然后直接調(diào)用頭文件。
2.當我們寫c++代碼,要調(diào)用c代碼時,可以將c的類型配置為靜態(tài)庫,然后直接調(diào)用頭文件。
由于c++支持函數(shù)重載,而c語言不支持函數(shù)重載,c語言和c++的函數(shù)名修飾規(guī)則有所不同,所以在鏈接的時候就C和C++之間無法找到對應(yīng)的函數(shù)地址。這時候就要引入extern “C”了。
如果是C調(diào)用C項目或是C++調(diào)用C++項目就不需要使用extern "C"了。
下面介紹的是不同項目之間的調(diào)用。
1.在C++項目中調(diào)用C的靜態(tài)庫時,告訴C++編譯器,extern "C"{}里面的函數(shù)是C編譯器編譯的,鏈接的時候用C的函數(shù)名規(guī)則去找,就可以鏈接上。
2.在C項目中調(diào)用C++的靜態(tài)庫時,同樣告訴C++編譯器,extern "C"{}里面的函數(shù)要用C語言的修飾規(guī)則修飾。
下面我們通過代碼來了演示,實驗環(huán)境:VS2019。
一、C++項目調(diào)用C的靜態(tài)庫
我們先將寫好的C程序配置成.lib的靜態(tài)庫:
此時該靜態(tài)庫的debug目錄下就有.lib的靜態(tài)庫
然后在需要調(diào)用庫的C++項目中,引入靜態(tài)庫:
將附加庫目錄的路徑設(shè)置為配置好的靜態(tài)庫的debug路徑下。
然后在鏈接器的輸入下添加 c的lib.lib;(創(chuàng)建的項目名.lib)
調(diào)用靜態(tài)庫的C++代碼:
其中include內(nèi)的 ..是跳轉(zhuǎn)到上一級目錄。
#include <iostream> using namespace std; // C++項目 // 告訴C++編譯器,extern "C"{}里面的函數(shù)是C編譯器編譯的,鏈接的時候用C的函數(shù)名規(guī)則去找,就可以鏈接上 extern "C" { #include "../c的lib/Stack.h" } bool isValid(const char * s){ ST st = { 0 }; StackInit(&st); while (*s) { if (*s == '(' || *s == '{' || *s == '[') { StackPush(&st, *s); ++s; } else { // 遇到右括號了,但是棧里面沒有數(shù)據(jù),說明 // 前面沒有左括號,不匹配,返回false if (StackEmpty(&st)) { StackDestroy(&st); return false; } STDataType top = StackTop(&st); StackPop(&st); if ((*s == '}' && top != '{') || (*s == ']' && top != '[') || (*s == ')' && top != '(')) { StackDestroy(&st); return false; } else { ++s; } } } // 如果棧不是空,說有棧中還有左括號未出 // 沒有匹配,返回是false bool ret = StackEmpty(&st); StackDestroy(&st); return ret; } int main() { cout << isValid("{[]}") << endl; cout << isValid("([)]") << endl; return 0; }
配置靜態(tài)庫的c代碼:
Stcak.h
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <stdbool.h> typedef int STDataType; typedef struct Stack { STDataType* a; int top; int capacity; }ST; void StackInit(ST* ps); void StackDestroy(ST* ps); void StackPush(ST* ps, STDataType x); void StackPop(ST* ps); STDataType StackTop(ST* ps); int StackSize(ST* ps); bool StackEmpty(ST* ps);
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "Stack.h" void StackInit(ST* ps) { assert(ps); ps->a = NULL; ps->top = 0; // ps->top = -1; ps->capacity = 0; } void StackDestroy(ST* ps) { assert(ps); free(ps->a); ps->a = NULL; ps->capacity = ps->top = 0; } void StackPush(ST* ps, STDataType x) { assert(ps); if (ps->top == ps->capacity) { int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity); if (tmp == NULL) { printf("realloc fail\n"); exit(-1); } ps->a = tmp; ps->capacity = newCapacity; } ps->a[ps->top] = x; ps->top++; } void StackPop(ST* ps) { assert(ps); assert(!StackEmpty(ps)); ps->top--; } STDataType StackTop(ST* ps) { assert(ps); assert(!StackEmpty(ps)); return ps->a[ps->top - 1]; } int StackSize(ST* ps) { assert(ps); return ps->top; } bool StackEmpty(ST* ps) { assert(ps); /*if (ps->top == 0) { return true; } else { return false; }*/ return ps->top == 0; }
二、C項目調(diào)用C++的靜態(tài)庫
實現(xiàn)方法與上面類似。只需要將上面步驟的.cpp與.c文件后綴互換,然后通過條件編譯,將C++靜態(tài)庫中的頭文件的函數(shù)用extern "C"作用:
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <stdbool.h> typedef int STDataType; typedef struct Stack { STDataType* a; int top; int capacity; }ST; //void StackInit(ST* ps); //void StackDestroy(ST* ps); //void StackPush(ST* ps, STDataType x); //void StackPop(ST* ps); //STDataType StackTop(ST* ps); //int StackSize(ST* ps); //bool StackEmpty(ST* ps); #ifdef __cplusplus extern "C" { #endif void StackInit(ST* ps); void StackDestroy(ST* ps); void StackPush(ST* ps, STDataType x); void StackPop(ST* ps); STDataType StackTop(ST* ps); int StackSize(ST* ps); bool StackEmpty(ST* ps); #ifdef __cplusplus } #endif
其中__cplusplus是c++中定義好的宏。所以在c++中就會展開extern "C"{},告訴編譯器按照c語言的函數(shù)修飾規(guī)則修飾,而c項目調(diào)用頭文件時,就沒有__cplusplus這個宏就不會展開extern "C"{},只會將修飾好的函數(shù)聲明展開。
還有另一種條件編譯:
#ifdef __cplusplus #define E extern "C" #else #defien E #endif E void StackInit(ST* ps); E void StackDestroy(ST* ps); E void StackPush(ST* ps, STDataType x); E void StackPop(ST* ps); E STDataType StackTop(ST* ps); E int StackSize(ST* ps); E bool StackEmpty(ST* ps);
然后在C項目中調(diào)用頭文件#include "../c的lib/Stack.h",(這是調(diào)用的頭文件在我的電腦中的存放路徑,大家調(diào)用的時候跳轉(zhuǎn)到自己存放頭文件的路徑即可)因為C中沒有定義__cplusplus,這樣C項目調(diào)用時,將E替換為空 ,直接展開函數(shù)聲明。
三、總結(jié)
1??通過extern "C",我們可以實現(xiàn)C項目調(diào)C++的庫,C++項目調(diào)C的庫。不需要源碼,只需要靜態(tài)庫和頭文件就可以實現(xiàn)功能。
2??在多人協(xié)作時尤為方便,只需要將寫好的代碼配置成.lib的靜態(tài)庫,然后將頭文件一起打包發(fā)給對方,對方在不知道具體的源碼和函數(shù)的實現(xiàn)下,只需要知道函數(shù)的功能就可以直接調(diào)用,也加強的多人協(xié)作之間的保密性
3??因為extern "C"只在C++中 起作用,所以不管是調(diào)用C的庫還是C++的庫,extern "C"都只在C++中處理。
到此這篇關(guān)于C語言與C++項目實現(xiàn)相互調(diào)用的文章就介紹到這了,更多相關(guān)C語言與C++ 相互調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C++進行Cocos2d-x游戲開發(fā)入門過程中的要點解析
這篇文章主要介紹了使用C++進行Cocos2d-x游戲開發(fā)入門過程中的要點解析,主要針對畫面變化以及觸摸響應(yīng)方面,需要的朋友可以參考下2015-12-12關(guān)于C語言中數(shù)據(jù)在內(nèi)存中的存儲詳解
這篇文章主要給大家介紹了關(guān)于C語言中數(shù)據(jù)在內(nèi)存中的存儲的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04C++ STL入門教程(7) multimap、multiset的使用
這篇文章主要介紹了C++ STL入門教程第七篇,multimap一對多索引,multiset多元集合的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08