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

如何從 C 調用 C++ 函數?

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

問題描述

這個我知道.

從 C++ 調用 C 函數:

如果我的應用程序是用 C++ 編寫的,而我必須從用 C 編寫的庫中調用函數.那么我會使用

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 調用 C++ 函數???

但是在這里我要擴展一個用 C 編寫的大型應用程序,我需要使用一個用 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

推薦答案

您需要創建一個 C API 來公開您的 C++ 代碼的功能.基本上,您將需要編寫聲明為 extern "C" 并且具有包裝 C++ 庫的純 C API(例如不使用類)的 C++ 代碼.然后使用您創建的純 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 可以選擇遵循面向對象的風格,即使 C 不是面向對象的.例如:

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);
 }

這篇關于如何從 C 調用 C++ 函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
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 仍然相關嗎?)
主站蜘蛛池模板: 中文在线播放 | 精品一区二区三区91 | 99精品国自产在线观看 | 国产传媒在线观看 | 人人看人人爽 | 在线视频h| 成人中文网 | 国产日韩欧美一区 | 欧美性视频在线播放 | 国外成人在线视频网站 | 综合自拍| 九九九久久国产免费 | 日本精品裸体写真集在线观看 | 96国产精品久久久久aⅴ四区 | 欧美成人一区二区三区 | 久久久久久国产精品免费免费 | 日韩在线免费视频 | 欧美一二区 | 亚洲男女视频在线观看 | 亚洲精品成人免费 | 国产目拍亚洲精品99久久精品 | 亚洲国产精品一区在线观看 | 欧美日韩国产在线观看 | 欧洲毛片 | 91高清在线视频 | www.久久.com | 亚洲成人在线免费 | 麻豆久久久9性大片 | 中文av在线播放 | 日韩视频中文字幕 | 91网站在线播放 | 伊人激情网 | 孰女乱色一区二区三区 | 国产高清在线精品一区二区三区 | 99亚洲精品 | 国产日产精品一区二区三区四区 | 久久久久久久久91 | 中文字幕亚洲视频 | 中文字幕在线免费视频 | 美人の美乳で授乳プレイ | 97caoporn国产免费人人 |