portable extensions options for external libraries

Robert Kern robert.kern at gmail.com
Wed Oct 18 18:47:29 CEST 2006

Alexandre Guimond wrote:

> so my question is: what is the right way of specifying extensions
> options (include_dirs, libraries, library_dirs) so that they are
> portable between windows and linux? i'm thinking environment variables.

The user can already use command-line options and CFLAGS if he so desires, so I 
wouldn't add any new environment variables.

   $ CFLAGS=-I/opt/local/include python setup.py build_ext -L/opt/local/lib

> Though fairly easy to do, i was wondering if python/distutils provided
> something more convenient, like searching through "common" directories,
> though those aren't very standard on windows?

distutils wouldn't do any searching at all. Your compiler will, though.

> Optimally, i would like
> to have something like:
> imaging = Extension( 'pyag.imaging._imaging',
>                      sources = ( glob.glob(
> 'Source/pyag/imaging/Src/*.cpp' ) +
>                                  glob.glob(
> 'Source/pyag/imaging/Src/*.h' ) ),
>                      include_dirs = ( get_numpy_include_dirs() +
>                                       [ 'Source/pyag/imaging/Src/' ] +
>                                         boost_include_dirs +
>                                         gsl_include_dirs ),
>                      library_dirs = boost_library_dirs +
> gsl_library_dirs,
>                      libraries = boost_libraries + gsl_libraries )

That's pretty much how everyone else does it. Just make sure that 
boost_{include,library}_dirs and gsl_{include,library}_dirs are defined near the 
top of the file with suitable comments to draw attention to them.

However, I might suggest making a setup.cfg with something like the following 
section (unindented):

   # Uncomment and modify the following section to configure the locations of the
   # Boost and GSL headers and libraries.


Changing data in a data file feels better to me than changing data in a program 
for some reason.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list