Newbie - C API Question

Robin Munn rmunn at pobox.com
Wed Apr 2 11:52:51 EST 2003


Natalia C. <n4t4lia at yahoo.com> wrote:
> I wrote this code that I got somewhere from a book 
> 
[snip sample embedded-Python code]
> 
> ********************************************************
> and I just do a simple cc testProj.c
> and it gives me this compiling error which I don't know what to do
> with
> 
> Undefined                       first referenced
>  symbol                             in file
> Py_BuildValue                       testProj.o
> PyArg_ParseTuple                    testProj.o
> ld: fatal: Symbol referencing errors. No output written to a.out
> 
> I don't really understand what do you mean (and how do you do) the
> loading of libpython.a??

When you compile a program, the compiler takes your source code (in this
case the testProj.c file) and produces an "object file". The internal
details of object files are complicated, but basically it contains
multiple sections: one section contains the binary, compiled version of
your code, and another section contains a list of all the symbols
(names) that your code tries to reference. This list includes things
like printf, strcpy, PyArg_ParseTuple... For each of those names, there
is a library file (usually ending in .a or .so) somewhere on your disk
that contains the code that defines those names. printf and strcpy, for
instance, are defined in the standard C library, which is usually found
in /lib with a filename like /lib/libc.so.# (where # is the version
number of your C library; on my system it's 6). The names starting in Py
would be defined in the Python library, which is usually called
libpython.a or libpython.so; I would expect to find that file in
/usr/lib, or maybe in /usr/local/lib.

Now: once a compiler has built an object file, it then needs to "link"
that object file with the appropriate libraries in order to produce an
executable. The process of "linking" resolves all those name references,
so that when your code calls printf(), the executable actually has a
function call that jumps to the right printf() code. If there are names
referenced in your object file that the linker can't find any
definitions for, then it will refuse to produce an executable. The
standard C library is usually linked in by default, so you don't have to
specify it explicitly on your command line. But the Python library is
not linked by default, so you would specify it by giving the "-lpython"
parameter to your compiler. "-lfoo" tells the compiler than in the
linking stage, it should look for a file named libfoo.a or libfoo.so in
the standard library search path, which usually includes /lib, /usr/lib,
/usr/local/lib, and sometimes a few other directories as well.

So you should do "cc testProj.c -lpython".

I hope this wasn't a long-winded explanation of something you understood
perfectly well already. If so, I'm sorry for having wasted your time.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list