久久久久久久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 仍然相關嗎?)
主站蜘蛛池模板: 亚洲美女一区 | 天天爽天天操 | 亚洲精品aaa | 一级特黄aaaaaa大片 | 国产h在线| 91综合网 | 久久99精品国产麻豆91樱花 | 九九热在线精品 | 天天干天天干天天 | 成人午夜网 | 麻豆精品一区 | 不卡中文字幕 | 亚洲另类色图 | 亚洲第一av网站 | 欧美精品一区二区在线观看 | 欧美激情小视频 | 破处视频在线观看 | 簧片在线免费观看 | 亚洲伊人影院 | 一区二区三区四区精品 | 日韩激情久久 | 免费a在线观看 | 国产草草| 日韩三级一区 | 欧美精品综合 | 国产日韩欧美日韩大片 | 在线国产一区 | 色婷婷综合在线 | 精产国产伦理一二三区 | 亚洲欧美成人 | 91插插插插插 | 久久久久国产视频 | 一区二区在线视频 | 国产蜜臀av | 亚洲视频在线免费观看 | 特黄aaaaaaaaa真人毛片 | 国产精品羞羞答答 | 久久精品欧美 | 91性视频 | 日本视频免费观看 | 免费的黄色网址 |