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

為面向對象的 C++ 代碼開發 C 包裝器 API

Developing C wrapper API for Object-Oriented C++ code(為面向對象的 C++ 代碼開發 C 包裝器 API)
本文介紹了為面向對象的 C++ 代碼開發 C 包裝器 API的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望開發一組 C API,這些 API 將環繞我們現有的 C++ API 以訪問我們的核心邏輯(用面向對象的 C++ 編寫).這本質上是一個膠水 API,允許我們的 C++ 邏輯被其他語言使用.有哪些好的教程、書籍或最佳實踐介紹了將 C 封裝在面向對象的 C++ 中所涉及的概念?

I'm looking to develop a set of C APIs that will wrap around our existing C++ APIs to access our core logic (written in object-oriented C++). This will essentially be a glue API that allows our C++ logic to be usable by other languages. What are some good tutorials, books, or best-practices that introduce the concepts involved in wrapping C around object-oriented C++?

推薦答案

這并不難手工完成,但取決于界面的大小.我所做的案例是在純 C 代碼中啟用我們的 C++ 庫,因此 SWIG 沒有太大幫助.(好吧,也許 SWIG 可以用來做到這一點,但我不是 SWIG 大師,這似乎很重要)

This is not too hard to do by hand, but will depend on the size of your interface. The cases where I've done it were to enable use of our C++ library from within pure C code, and thus SWIG was not much help. (Well maybe SWIG can be used to do this, but I'm no SWIG guru and it seemed non-trivial)

我們最終做的就是:

  1. 每個對象都在 C 中傳遞一個不透明的句柄.
  2. 構造函數和析構函數被包裹在純函數中
  3. 成員函數是純函數.
  4. 其他內置函數盡可能映射到 C 等效項.

這樣的類(C++ 頭文件)

So a class like this (C++ header)

class MyClass
{
  public:
  explicit MyClass( std::string & s );
  ~MyClass();
  int doSomething( int j );
}

將映射到這樣的 C 接口(C 頭文件):

Would map to a C interface like this (C header):

struct HMyClass; // An opaque type that we'll use as a handle
typedef struct HMyClass HMyClass;
HMyClass * myStruct_create( const char * s );
void myStruct_destroy( HMyClass * v );
int myStruct_doSomething( HMyClass * v, int i );

接口的實現看起來像這樣(C++源代碼)

The implementation of the interface would look like this (C++ source)

#include "MyClass.h"

extern "C" 
{
  HMyClass * myStruct_create( const char * s )
  {
    return reinterpret_cast<HMyClass*>( new MyClass( s ) );
  }
  void myStruct_destroy( HMyClass * v )
  {
    delete reinterpret_cast<MyClass*>(v);
  }
  int myStruct_doSomething( HMyClass * v, int i )
  {
    return reinterpret_cast<MyClass*>(v)->doSomething(i);
  }
}

我們從原始類派生我們的不透明句柄以避免需要任何強制轉換,并且 (這似乎不適用于我當前的編譯器).我們必須使句柄成為結構體,因為 C 不支持類.

We derive our opaque handle from the original class to avoid needing any casting, and (This didn't seem to work with my current compiler). We have to make the handle a struct as C doesn't support classes.

這樣就為我們提供了基本的 C 接口.如果您想要一個更完整的示例來展示您可以集成異常處理的一種方式,那么您可以在 github 上嘗試我的代碼:https://gist.github.com/mikeando/5394166

So that gives us the basic C interface. If you want a more complete example showing one way that you can integrate exception handling, then you can try my code on github : https://gist.github.com/mikeando/5394166

現在有趣的部分是確保您將所有必需的 C++ 庫正確鏈接到更大的庫中.對于 gcc(或 clang),這意味著只使用 g++ 進行最后的鏈接階段.

The fun part is now ensuring that you get all the required C++ libraries linked into you larger library correctly. For gcc (or clang) that means just doing the final link stage using g++.

這篇關于為面向對象的 C++ 代碼開發 C 包裝器 API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 仍然相關嗎?)
主站蜘蛛池模板: 中文字幕在线免费看 | 日韩毛片在线播放 | 中文在线字幕免费观 | 亚洲二区在线观看 | 精品一二三 | 国产综合在线视频 | 日韩精品久久久久久 | 欧美黄网站 | 亚洲另类色图 | 国产农村妇女aaaaa视频 | 亚洲精品视频在线观看免费 | 久久艹精品 | 欧美色图在线观看 | 日本在线网站 | 国产三级在线看 | 国产视频一二三区 | 999久久久国产精品 亚洲黄色三级 | 日日夜夜人人 | 国产精品海角社区 | 午夜精品视频在线观看 | 午夜性色| 日韩精品三区 | 国产伦精品一区二区三区视频网站 | 国产精品成人免费一区久久羞羞 | 精品一区二区三区免费看 | 日本欧美久久久久免费播放网 | 一区二区三区高清 | 久久久天堂国产精品女人 | 久婷婷| 午夜在线免费观看 | 午夜视频一区 | 香蕉视频一直看一直爽 | 久久久中文字幕 | 亚洲激情一区 | av网站在线免费观看 | 小sao货撅起屁股扒开c微博 | 在线观看av网站 | 丁香五香天堂网 | 日韩精品视频在线免费观看 | 天天爽夜夜爽夜夜爽 | 亚洲黄色成人 |