久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從 C 調(diào)用 C++ 函數(shù)?

How to call C++ function from C?(如何從 C 調(diào)用 C++ 函數(shù)?)
本文介紹了如何從 C 調(diào)用 C++ 函數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這個我知道.

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

如果我的應(yīng)用程序是用 C++ 編寫的,而我必須從用 C 編寫的庫中調(diào)用函數(shù).那么我會使用

If my application was in C++ and I had to call functions from a library written in C. Then I would have used

//main.cpp

extern "C" void C_library_function(int x, int y);//prototype
C_library_function(2,4);// directly using it.

這不會破壞名稱 C_library_function 并且鏈接器會在其輸入的 *.lib 文件中找到相同的名稱,問題就解決了.

This wouldn't mangle the name C_library_function and linker would find the same name in its input *.lib files and problem is solved.

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

但是在這里我要擴展一個用 C 編寫的大型應(yīng)用程序,我需要使用一個用 C++ 編寫的庫.C++ 的名稱修改在這里造成了麻煩.鏈接器抱怨未解析的符號.好吧,我不能在我的 C 項目上使用 C++ 編譯器,因為那會破壞很多其他東西.出路是什么?

But here I'm extending a large application which is written in C and I need to use a library which is written in C++. Name mangling of C++ is causing trouble here. Linker is complaining about the unresolved symbols. Well I cannot use C++ compiler over my C project because thats breaking lot of other stuff. What is the way out?

順便說一下,我正在使用 MSVC

By the way I'm using MSVC

推薦答案

您需要創(chuàng)建一個 C API 來公開您的 C++ 代碼的功能.基本上,您將需要編寫聲明為 extern "C" 并且具有包裝 C++ 庫的純 C API(例如不使用類)的 C++ 代碼.然后使用您創(chuàng)建的純 C 包裝器庫.

You need to create a C API for exposing the functionality of your C++ code. Basically, you will need to write C++ code that is declared extern "C" and that has a pure C API (not using classes, for example) that wraps the C++ library. Then you use the pure C wrapper library that you've created.

您的 C API 可以選擇遵循面向?qū)ο蟮娘L格,即使 C 不是面向?qū)ο蟮?例如:

Your C API can optionally follow an object-oriented style, even though C is not object-oriented. Ex:

 // *.h file
 // ...
 #ifdef __cplusplus
 #define EXTERNC extern "C"
 #else
 #define EXTERNC
 #endif

 typedef void* mylibrary_mytype_t;

 EXTERNC mylibrary_mytype_t mylibrary_mytype_init();
 EXTERNC void mylibrary_mytype_destroy(mylibrary_mytype_t mytype);
 EXTERNC void mylibrary_mytype_doit(mylibrary_mytype_t self, int param);

 #undef EXTERNC
 // ...


 // *.cpp file
 mylibrary_mytype_t mylibrary_mytype_init() {
   return new MyType;
 }

 void mylibrary_mytype_destroy(mylibrary_mytype_t untyped_ptr) {
    MyType* typed_ptr = static_cast<MyType*>(untyped_ptr);
    delete typed_ptr;
 }

 void mylibrary_mytype_doit(mylibrary_mytype_t untyped_self, int param) {
    MyType* typed_self = static_cast<MyType*>(untyped_self);
    typed_self->doIt(param);
 }

這篇關(guān)于如何從 C 調(diào)用 C++ 函數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 日韩av专区 | 免费在线观看av | 天堂中文字幕免费一区 | 久久午夜影院 | 精久久 | 国产又色又爽又黄又免费 | 欧美一级黄色大片 | 中文字幕av一区二区三区谷原希美 | 欧美在线性爱视频 | 久久在线 | 国产精品尤物 | 日韩激情视频 | 欧美日韩国产激情 | 黄色片免费 | 日韩av手机在线 | 国产免费高清视频 | 亚洲国产精品va在线看黑人 | 欧美一级淫片免费视频黄 | 丁香六月综合 | 在线天堂av | 丰满少妇高潮无套内谢 | 精品久久一区二区 | 午夜精品在线观看 | 欧美极品一区 | 国产尤物视频 | 99精品视频在线观看 | 欧美色图一区二区三区 | 国产精品黄 | 欧美日韩一区二区三区视频 | 日本不卡一区 | 欧美黄色一区 | 欧美日韩免费视频 | 少妇xxxx69 | 国产成人免费 | 天天干天天操天天爽 | 欧美一级网站 | 久久久久久91 | av观看免费 | 黄色一级视频在线观看 | 国产吃瓜黑料一区二区 | 日本中文字幕在线播放 |