[Distutils] C extension modules

Torsten Mohr tmohr at s.netic.de
Tue Jan 26 18:30:33 CET 2010

Hello Tarek,

great, good to know i can get some help here.

> > I write a C++ extension module in which i need to tell if i compile on
> > Linux or Windows.  What is the preferred way to do this?
> What are you doing exactly with this information ?

Basically what i try to do is write an extension module in C++ for Python.
In the source code there are some parts for 
#ifdef HOST_MSWin32 and also for #ifdef HOST_linux.

I already got an idea, as setup.py is normal pyhton code i could try os.name .
That could help distinguishing in setup.py.  But i'm not sure on how to
forward this information so it is available in the source code.
It could be also something like #ifdef HOST_posix or HOST_nt .

What is the preferred way to make this information available to the compiler
so i can use code like #ifdef HOST_posix?

After looking in the documentation i found "define_macros", seems like the way 
to go, right?

Another thing that i'm not sure about is that i need to use other C libraries
from that module i write.  For example i want to load JPEG files, so i need
to find the header for libjpeg.h and also to link libjpeg to my module.

If i write a configure script (autoconf / autotools) there are code snippets 
to test for libraries and paths (to find the library and to set the include 
path).  Also to test for a certain version.

Is that possible to use in distutils / setup.py?

That link you gave below is a good example, the bad news is that i have to 
program a part for each OS.

Thanks a lot for your hints.  If any of the above is wrong then please correct 

Best regards,

Am Dienstag, 26. Januar 2010 17:32:37 schrieb Tarek Ziadé:
> On Mon, Jan 25, 2010 at 11:22 PM, Torsten Mohr <tmohr at s.netic.de> wrote:
> > Hello,
> >
> > i'm not sure if this is the preferred way to get help.  If it is not, can
> > you then please tell me how?
> That's the best way :)
> > Also, i use libjpeg, libtiff and libpng.  Can i portably test for
> > presence and version of these libs on Linux, Windows and other OSes?
> >
> > The documentation on docs.python.org is quite good, but are there some
> > additional examples available?
> What's usually done is testing for the platform in setup.py to decide which
> extensions are going in.
> see http://bitbucket.org/kang/python-keyring-lib/src/  and in
> particular "extensions.py" used in setup.py
> Regards
> Tarek
> > Thanks for any hints,
> > Torsten.
> > _______________________________________________
> > Distutils-SIG maillist  -  Distutils-SIG at python.org
> > http://mail.python.org/mailman/listinfo/distutils-sig

More information about the Distutils-SIG mailing list