How do I set up the python/c library correctly?
Question: I have been trying to get the python/c library to like my mingW compiler. The
python online doncumentation;
<http://docs.python.org/c-api/intro.html#include-files> only mentions that I
need to import the python.h file. I grabbed it from the installation directory
(as is required on the windows platform), and tested it by compiling the
script: `#include "Python.h"`. This compiled fine. Next, I tried out the
snippet of code shown a bit lower on the python/c API page:

    
    
    PyObject *t;
    t = PyTuple_New(3);
    PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
    PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
    PyTuple_SetItem(t, 2, PyString_FromString("three"));
    

For some reason, the compiler would compile the code if I'd remove the last 4
lines (so that only the pyObject variable definition would be left), yet
calling the actual constructor of the tuple returned errors.

I am probably missing something completely obvious here, given I am very new
to C, but does anyone know what it is?


Answer: I've done some crafty Googling, and if you are getting errors at the linker
stage (the error messages might have hex strings or references to `ld`), you
may need to make sure the Python library that ships with the Windows version
has been converted to a format that GCC (MinGW) can read; see
[here](http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-
with-distutils-and-mingw/), among other sites. Also ensure that GCC can find
and is using the library file if needs be, using `-L/my/directory` and
`-lpython26` (substituting appropriately for your path and Python version).

If the errors are at the compilation stage (if line numbers are given in the
messages), make sure that you don't need to add any other directories to the
include search path. Python might (I've not used its C API) include other
header files in `Python.h` which are stored in some other directory. If this
is the case, use the `-I/my/directory/` flag to GCC to tell it to search there
as well.

Exact (copied-and-pasted) error messages would help, though.

* * *

_**Warning_** : The text below does not answer the question!

Did you put the code inside a function? Try putting it in `main`, like so:

    
    
    int main(int argc, char *argv[]) {
        PyObject *t;
        t = PyTuple_New(3);
        PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
        PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
        PyTuple_SetItem(t, 2, PyString_FromString("three"));
    
        return 0;
    }
    

This code will be run on execution of the program. You can then use whatever
other methods are provided to examine the contents of the tuple. If it isn't
to be run separately as an executable program, then stick it in a differently-
named method; I assume you have another way to invoke the function.

The `PyObject *t;` definition is valid outside the function as a global
variable definition, as well as inside a function, declaring it as a local
variable. The other four lines are function calls, which must be inside
another function.

The above code on its own does not a program make. Are you trying to write a C
extension to Python? If so, look at some more complete documentation
[here](http://docs.python.org/extending/extending.html).

