Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:54:25+00:00 2026-05-13T10:54:25+00:00

I’m looking to develop a set of C APIs that will wrap around our

  • 0

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++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:54:26+00:00Added an answer on May 13, 2026 at 10:54 am

    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)

    All we ended up doing was:

    1. Every object is passed about in C an opaque handle.
    2. Constructors and destructors are wrapped in pure functions
    3. Member functions are pure functions.
    4. Other builtins are mapped to C equivalents where possible.

    So a class like this (C++ header)

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

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

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

    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.

    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

    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++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.