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

  • Home
  • SEARCH
  • 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 4626644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:27:02+00:00 2026-05-22T03:27:02+00:00

is it possible to wrap a c++ library into c? how could i do

  • 0

is it possible to wrap a c++ library into c?

how could i do this?

are there any existing tools?

(need to get access to a existing c++ library but only with 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-22T03:27:03+00:00Added an answer on May 22, 2026 at 3:27 am

    You can write object-oriented code in C, so if it’s an object-oriented C++ library, it’s possible to wrap it in a C interface. However, doing so can be very tedious, especially if you need to support inheritance, virtual functions and such stuff.

    If the C++ library employs Generic Programming (templates), it might get really hairy (you’d need to provide all needed instances of a template) and quickly approaches the point where it’s just not worth doing it.

    Assuming it’s OO, here’s a basic sketch of how you can do OO in C:

    C++ class:

    class cpp {
      public:
        cpp(int i);
        void f();
    };
    

    C interface:

    #ifdef __cplusplus
    extern "C" {
    #endif
      typedef void* c_handle;
    
      c_handle c_create(int i)
      {
        return new cpp(i);
      }
    
      void c_f(c_handle hdl)
      {
        static_cast<cpp*>(hdl)->f();
      }
    
      void c_destroy(c_handle hdl)
      {
        delete static_cast<cpp*>(hdl);
      }
    #ifdef __cplusplus
    }
    #endif
    

    Depending on your requirements, you could amend that. For example, if this is going to be a public C interface to a private C++ API, handing out real pointers as handles might make it vulnerable. In that case you would hand out handles that are, essentially, integers, store the pointers in a handle-to-pointer map, and replace the cast by a lookup.

    Having functions returning strings and other dynamically sized resources can also become quite elaborate. You would need the C caller provide the buffer, but it can’t know the size before-hand. Some APIs (like parts of the WIn32 API) then allow the caller to call such a function with a buffer of the length 0, in which case they return the length of the buffer required. Doing so, however, can make calling through the API horribly inefficient. (If you only know the length of the required buffer after the algorithm executed, it needs to be executed twice.)
    One thing I’ve done in the past is to hand out handles (similar to the handle in the above code) to internally stored strings and provide an API to ask for the required buffer size, retrieve the string providing the buffer, and destroy the handle (which deletes the internally stored string).
    That’s a real PITA to use, but such is C.

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

Sidebar

Related Questions

Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
I'm not sure if this is even possible, but here goes: I have a
I'm not even sure if this is even possible, but I've just started WPF
I am trying to wrap an external library in more idiomatic Clojure. This includes
I am finally starting to get somewhere with WCF, but I have run into
I'm trying to condtionally link in. .a static libraries by defines. Is this possible
Is it possible to wrap long options within a select list? I have a
Anyone know if it is possible to wrap the xtick labels in matplotlib? Right
I know its probably possible, but is it practical and doable to try and
I know its probably possible, but is it practical and doable to try and

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.