How to add c compiler options to distutils
Question: I want to call Mathematica from C code and C code from Python. I have the
individual parts working, but I can't put everything together.

When I compile the C code that calls Mathematica, then I use the following
command in makefile

    
    
    $(CC) -O mlcall.c -I$(INCDIR) -L$(LIBDIR) -l${MLLIB} ${EXTRALIBS} -o $@
    

Where

    
    
    MLINKDIR = /opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit
    SYS=Linux-x86-64
    CADDSDIR = ${MLINKDIR}/${SYS}/CompilerAdditions
    INCDIR = ${CADDSDIR}
    LIBDIR = ${CADDSDIR}
    MLLIB = ML64i3
    

My question is how can I use distutils with those same options (currently I'm
getting undefined symbol: MLActivate error, when calling from Python and I
think the problem is because I'm not using these options)?

I saw the answer <http://stackoverflow.com/a/16078447/1335014> and tried to
use CFLAGS (by running the following script):

    
    
    MLINKDIR=/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit
    SYS="Linux-x86-64"
    CADDSDIR=${MLINKDIR}/${SYS}/CompilerAdditions
    INCDIR=${CADDSDIR}
    LIBDIR=${CADDSDIR}
    MLLIB=ML64i3
    EXTRALIBS="-lm -lpthread -lrt -lstdc++"
    export CFLAGS="-I$INCDIR -L$LIBDIR -l${MLLIB} ${EXTRALIBS}"
    

So I get the following output for `echo $CFLAGS`

    
    
    -I/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -L/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -lML64i3 -lm -lpthread -lrt -lstdc++
    

Which seems correct, but didn't have any effect. Maybe because I'm adding more
than one option.


Answer: I realized that if you don't modify the C source code then nothing is compiled
(the original error I had stayed because of that reason). Using CFLAGS, as I
did, actually does fix the problem.

After digging around in distutils documentations I found two additional fixes
(these only use `setup.py` and don't require any environment variables).

1:

    
    
    from distutils.core import setup, Extension
    
    module1 = Extension('spammodule',
                    sources = ['spammodule.c'],
                    extra_compile_args=["-I/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions"],
                    extra_link_args=["-L/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions", "-lML64i3", "-lm", "-lpthread", "-lrt", "-lstdc++"])
    
    setup (name = 'MyPackage',
                    version = '1.0',
                    description = 'This is a demo package',
                    ext_modules = [module1])
    

2:

    
    
    from distutils.core import setup, Extension
    
    module1 = Extension('spammodule',
                sources = ['spammodule.c'],
                include_dirs=['/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions'],
                library_dirs=['/opt/Mathematica-9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions'],
                libraries=["ML64i3", "m", "pthread", "rt", "stdc++"])
    
    setup (name = 'MyPackage',
                version = '1.0',
                description = 'This is a demo package',
                ext_modules = [module1])
    

