user1202733
user1202733

Reputation: 623

How do I compile a C++ extension (rather than C) for Python on OSX?

I'm using distutils, and have a module spammodule.c that compiles,links and works perfectly.

But if I rename that module to .cpp and re-run the distutils build/install sequence, the module builds but gives me an error on import in python:

ImportError: dynamic module does not define init function (initspam)

Is there a different format for new modules in CPP rather than C ?

Upvotes: 3

Views: 172

Answers (1)

You need to enclose your init function within

#ifdef __cplusplus
extern "C" {
#endif

//initspam goes here

#ifdef __cplusplus
}
#endif

in order to prevent its name from getting mangled.

Upvotes: 6

Related Questions