简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM