Wrapping C++ class with SWIG, Mac OS X


Paul Anton Letnes

Thanks a lot for the comments. So, I post the code, which should let
you recreate the circumstances. I'm on 10.5.2, python2.5 (from
Fink), and got SIP from FINK recently - should be the latest version.

I know C++ to some extent - the linking is the hardest part for me
to grasp. I don't have a clear idea on how these things take place,
nor what all the words mean.

// Define the interface to the word library.

#ifndef _WORD_H_
#define _WORD_H_
class Word {
Word(const char *w);
char *reverse() const;

const char* the_word;
#endif /* _WORD_H_ */
#include "word.h"
Word::Word(const char *w)
this->the_word = w;
char* Word::reverse() const
return this->the_word;
// Define the SIP wrapper to the word library.

%Module word 0

class Word {

#include <word.h>

Word(const char *w);

char *reverse() const;

import os
import sipconfig

# The name of the SIP build file generated by SIP and used by the
# system.
build_file = "word.sbf"

# Get the SIP configuration information.
config = sipconfig.Configuration()

# Run SIP to generate the code.
os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file,

# Create the Makefile.
makefile = sipconfig.SIPModuleMakefile(config, build_file)

# Add the library we are wrapping. The name doesn't include any
# specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or
# ".dll" extension on Windows).
makefile.extra_libs = ["word"]

# Generate the Makefile itself.

As I said, the code is just from this example:

It is possible that this is not well suited for OSX. The makefile
generated includes the flag '-bundle' which, as far as I can tell,
is OSX specific. However, the 'TARGET' is 'word.so', which is not

Do you know a good web resource for learning the basics of linking?
Man pages are a bit brief for newbies.


Not knowing C/C++ & linking is certainly something that will get you
when trying to wrap libs written in these languages.

I'm on OSX myself, and can say that as a unixish system, it is rather
friendly to self-compliation needs.

However, without having the complete sources & libs, I can't really
comment much - the only thing that is clear from above is that the
linker does not find the file


which you of course need to have somewhere. The good news is that
you've teached the linker where to find it (using LDFLAGS or other
means) you are (modulo debugging) done - SIP has apparently grokked

Of course you also must make the library available at runtime. So it
must be installed on a location (or that location given with
DYLD_LIBRARY_PATH) where the dynamic loader will find it.



Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question