[Numpy-discussion] gcc compile / link questions

Konrad Hinsen hinsen at cnrs-orleans.fr
Sat Nov 1 13:17:10 CST 2003

On Saturday 01 November 2003 03:12, Edward C. Jones wrote:

> I compile and link Python extension modules using the script
> gcc -fPIC -g -I/usr/local/include/python2.3 \
>   -Wall -Wstrict-prototypes -c mymodule.c
> g++ -shared mymodule.o -L/usr/local/lib -o mymodule.so
> It works for me but it isn't pretty. Is there a better way to write it?

Yes, the distutils module. It's part of the Python standard library and 
documented there.

> Gcc finds all the libraries that need to be linked in. For example,
> "/usr/local/lib/python2.3/site-packages/numarray/libnumarray.so". How
> does gcc do this?

It doesn't :-)  And it doesn't have to. You are not creating a stand-alone 
executable, but a shared library for use in Python. Everything is linked 
together dynamically at runtime when Python imports all the modules.

> I created a .so file "utilities.so" that contains some C functions that
> are called in mymodule.c but are not visible from Python. Both
> "utilities.c" and "mymodule.c" use numarray. What changes do I make in
> the script above? Must I use the nasty "libnumarray_UNIQUE_SYMBOL" trick?

I don't know enough about numarray to answer that question, but that is 
certainly one option. Alternatively you could include utilities.c into 

> What is a script for creating "utilities.a" using gcc? How do I change
> the script above to include "utilities.a"?

To create the archive:

  gcc -c utilities.c
  ar r libutilities.a utilies.o

You might have to add some libraries or library paths to the compilation step. 
To use the archive, just add -lutilities to the gcc command line, plus 
eventually the path with -L.

Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-
Rue Charles Sadron                       | Fax:  +33-
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais

More information about the Numpy-discussion mailing list