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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:30:57+00:00 2026-06-01T22:30:57+00:00

I have a C++ library (let’s call it mylib ) which compiles to libmylib.so

  • 0

I have a C++ library (let’s call it mylib) which compiles to libmylib.so file in /usr/local/lib and I have a bunch of header files in a directory called my lib in /usr/local/include.

Now the thing I wanted to do (for starters) is just use one of the header files (it contains information about a class my library is offering) with SWIG to generate the mylib_wrap.cxx file and then compile it and link it against the existing mylib.so. So that I can instance my class in Python.

Is this the right approach/idea? How would the compile and linking command look like (not exactly of course)? I am trying to generate a Python binding.

  • 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-06-01T22:30:58+00:00Added an answer on June 1, 2026 at 10:30 pm

    I’ve put together a complete example for you:

    Header file:

    (mylib.h)

    class Foo {
    };
    
    void bar(const Foo&);
    

    Implementation:

    #include "mylib.h"
    #include <iostream>
    
    void bar(const Foo& f) {
      std::cout << &f << std::endl;
    }
    

    Compile the library:

    g++ -fPIC -Wall -Wextra -shared mylib.cc -o libmylib.so
    

    SWIG interface to wrap the library:

    %module mylib
    
    // Make mylib_wrap.cxx include this header:
    %{
    #include "mylib.h"
    %}
    
    // Make SWIG look into this header:
    %include "mylib.h"
    

    Compile Python module:

    swig -Wall -c++ -python mylib.i  
    g++ -fPIC -Wall -Wextra -shared mylib_wrap.cxx -o _mylib.so -L. -lmylib -I/usr/include/python2.7/ -lpython2.7 
    

    Note that we linked the Python module against the library. If it wasn’t in the current directory you’d need to specify the library path. SWIG expects the native part of Python module to be called _module.so

    Run

    LD_LIBRARY_PATH=. python                                                 
    Python 2.7.2+ (default, Nov 30 2011, 19:22:03) 
    [GCC 4.6.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mylib
    >>> i=mylib.Foo()
    >>> mylib.bar(i)
    0x28cc100
    >>> mylib.bar(i)
    0x28cc100
    >>> mylib.bar(mylib.Foo())
    0x28b3b10
    

    Here I made sure the shared objects we just built are on the library path by setting LD_LIBRARY_PATH appropriately.

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

Sidebar

Related Questions

Let's say I have a library Lib.dll, which uses Castle.Windsor to initialize its services.
I have a static library (let's call it S) that uses a category (NSData+Base64
Let’s say I have a library, in which I added a few Console.WriteLine(..) statements
I have a class library project that i have made. Let's call it ClassA.
I have a library project which contains an abstract class, let's say ClassA .
Using this guide I have created a static library (let's call it AppCore )
I have a library in c:\cppunit\lib, and a header files in c:\cppunit\include. I come
I have a static library. Let's call it MyLibrary. I am using it in
I have a native C++ library (let's call it CoreLib.dll) and it exposes two
Let's say you are writing a library and you have a bunch of utility

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.