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 6807935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:53:37+00:00 2026-05-26T19:53:37+00:00

I have the source to a C library with a C API. I want

  • 0

I have the source to a C library with a C API. I want to expose the functions and C data structures to python, so that I can use the library in my python scripts.

There are over 200+ functions and about 50 data structures, so it would be preferable if I could automate the generation of the “glue” code between C and Python.

I am currently reading up on SWIG, as it seems to be the way forward. Are there other alternatives I should be looking at (and why?).

Last but not the least, (assuming SWIG is the way forward), does anyone have a link to a good tutorial that shows how to write a Python extension module from an existing C library?

  • 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-26T19:53:38+00:00Added an answer on May 26, 2026 at 7:53 pm

    I have used SWIG 2.x to convert a pretty complex C and C++ API and it saved me a lot of time.

    One important limitation in SWIG is that structs within structs are not handled very well. The same applies to classes in C++. I had full control over the C/C++ APIs, so beforehand I flattened the couple of structs that I had that used inner structs and avoided this problem.

    My first recommendation would be that you define a basic module for SWIG that just imports your header files, and see how SWIG likes it. Like a compiler, SWIG will output warning and errors it finds, and those will be what you will need to work on.

    After I had all the functions working, I was not completely happy with the result. SWIG translates all your functions literally, but the problem is that some idioms that are very common in C/C++ look pretty weird in Python. For example, functions that have output arguments specified as pointers will not be very Pythonic. For example, consider this function, which is pretty common in C/C++:

    unsigned int getPath(char* buffer, unsigned int size);
    

    This function takes a buffer and a size, and fills in the buffer with a string, returning how many bytes were actually used, or 0 in case of an error. In Python having to send a buffer (a string) and a size as arguments would be awfully odd, so for functions of this style I’ve added alternative versions that translate to more friendly functions. In this case, I did something like this:

    const char* getPath();
    

    The implementation of this function simply passes a static buffer of some maximum size into the original getPath(), and returns the address of the static buffer, or NULL if there is an error. Disclaimer: I use a C++ compiler, so I could define two getPath() functions with different arguments. In C you would need to use different names for each function. SWIG converts char* to Python strings automatically, so this translates very nicely.

    Instead of defining alternative functions in C/C++, you can also provide instructions to SWIG on how you want to convert certain functions and its arguments. For example, SWIG allows you to tie a pair of buffer and size arguments and treat them like a single string argument on the translated function. There are also many more convenience translations like this.

    As far as tutorials I did not find anything useful outside of the official SWIG documentation, which is pretty good, and has a section that is specific to Python.

    I hope this helps.

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

Sidebar

Related Questions

I'm publishing an open-source library on CodePlex, and want the dll files to have
I have a library consisting of approx 100 source files. I want one of
I have this small code library that I'm considering releasing into Open Source. I
I have source XMLfiles that come in with multiple root elements and there is
I have a C++ library that provides various classes for managing data. I have
I'm using a closed source library (by Activ financial) that includes with their API
I have to set a function pointer in a library API, such that it
I have a simple tank wars style game using the allegro open source library.
I have a .NET application, which is using an open source C++ compression library
I have an executable that defaults to 32-bit. It doesn't have source code 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.