Wrapping C++ class with SWIG, Mac OS X


P

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.

----------------------------
word.h:
----------------------------
// Define the interface to the word library.

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

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

%Module word 0

class Word {

%TypeHeaderCode
#include <word.h>
%End

public:
Word(const char *w);

char *reverse() const;

};
----------------------------
configure.py:
----------------------------
import os
import sipconfig

# The name of the SIP build file generated by SIP and used by the
build
# 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,
"word.sip"]))

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

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

# Generate the Makefile itself.
makefile.generate()
----------------------------

As I said, the code is just from this example:
http://www.riverbankcomputing.com/static/Docs/sip4/sipref.html#using-sip

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
'libword.dylib'.

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


Cheers!
Paul.

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

libword.dylib

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

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.

Diez
 
Ad

Advertisements


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

Top