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

為面向?qū)ο蟮?C++ 代碼開發(fā) C 包裝器 API

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

問題描述

我希望開發(fā)一組 C API,這些 API 將環(huán)繞我們現(xiàn)有的 C++ API 以訪問我們的核心邏輯(用面向?qū)ο蟮?C++ 編寫).這本質(zhì)上是一個膠水 API,允許我們的 C++ 邏輯被其他語言使用.有哪些好的教程、書籍或最佳實踐介紹了將 C 封裝在面向?qū)ο蟮?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. 構(gòu)造函數(shù)和析構(gòu)函數(shù)被包裹在純函數(shù)中
  3. 成員函數(shù)是純函數(shù).
  4. 其他內(nèi)置函數(shù)盡可能映射到 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 );

接口的實現(xiàn)看起來像這樣(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);
  }
}

我們從原始類派生我們的不透明句柄以避免需要任何強制轉(zhuǎn)換,并且 (這似乎不適用于我當前的編譯器).我們必須使句柄成為結(jié)構(gòu)體,因為 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

現(xiàn)在有趣的部分是確保您將所有必需的 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++.

這篇關(guān)于為面向?qū)ο蟮?C++ 代碼開發(fā) C 包裝器 API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)嗎?)
主站蜘蛛池模板: 亚洲综合一区二区三区 | 国产午夜精品一区二区 | 久久精品中文字幕 | 91在线观 | 亚洲欧美视频一区 | av在线一区二区 | 狠狠干天天干 | 狠狠涩 | 日本手机在线 | 久久精品一区二区 | 国产在线永久免费 | 中文字幕一区二区三区四区五区 | 91免费版在线观看 | 91视视频在线观看入口直接观看 | 久草在线在线精品观看 | 99久久久久久久久 | 日韩和的一区二区 | 久久久.com| 国产成人精品免费 | 91影院| 18av在线播放 | 人人爽日日躁夜夜躁尤物 | 国产剧情一区 | 狠狠干网 | 一级毛片在线播放 | 天天操天天天干 | 亚洲精品久久久久中文字幕欢迎你 | 久久久久国产一区二区三区 | 一区二区三区日韩 | 亚洲一区二区三区四区五区午夜 | 婷婷桃色网 | 中文字幕成人 | 精品自拍视频 | 日本天天操 | 日本精品久久久久久久 | 毛片站| 日本一区二区影视 | 自拍视频在线观看 | 天色综合网 | 99久久精品免费看国产高清 | 欧美色999|