System: Xubuntu, Karmic 9.10
Compiler: gcc
Python: python2.6
IDE: CODE::BLOCKS

Trying the following example:

#include "Python.h"

void initxyzzy(void); /* Forward */

int main(int argc, char **argv)
{
    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(argv[0]);

    /* Initialize the Python interpreter.  Required. */
    Py_Initialize();

    /* Add a static module */
    initxyzzy();

    /* Define sys.argv.  It is up to the application if you
       want this; you can also let it undefined (since the Python
       code is generally not a main program it has no business
       touching sys.argv...) */
    PySys_SetArgv(argc, argv);

    /* Do some application specific code */
    printf("Hello, brave new world\n\n");

    /* Execute some Python statements (in module __main__) */
    PyRun_SimpleString("import sys\n");
    PyRun_SimpleString("print sys.builtin_module_names\n");
    PyRun_SimpleString("print sys.modules.keys()\n");
    PyRun_SimpleString("print sys.executable\n");
    PyRun_SimpleString("print sys.argv\n");

    /* Note that you can call any public function of the Python
       interpreter here, e.g. call_object(). */

    /* Some more application specific code */
    printf("\nGoodbye, cruel world\n");

    /* Exit, cleaning up the interpreter */
    Py_Exit(0);
    /*NOTREACHED*/
}

/* A static module */

/* 'self' is not used */
static PyObject *
xyzzy_foo(PyObject *self, PyObject* args)
{
    return PyInt_FromLong(42L);
}

static PyMethodDef xyzzy_methods[] = {
    {"foo",     xyzzy_foo,  METH_NOARGS,
     "Return the meaning of everything."},
    {NULL,      NULL}       /* sentinel */
};

void
initxyzzy(void)
{
    PyImport_AddModule("xyzzy");
    Py_InitModule("xyzzy", xyzzy_methods);
}

Either in CODE::BLOCKS or from the command line, I get the following error:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:8: error: ‘Py_SetProgramName’ was not declared in this scope
main.cpp:11: error: ‘Py_Initialize’ was not declared in this scope
main.cpp:20: error: ‘PySys_SetArgv’ was not declared in this scope
main.cpp:23: error: ‘printf’ was not declared in this scope
main.cpp:26: error: ‘PyRun_SimpleString’ was not declared in this scope
main.cpp:39: error: ‘Py_Exit’ was not declared in this scope
main.cpp: At global scope:
main.cpp:46: error: expected initializer before ‘*’ token
main.cpp:52: error: ‘PyMethodDef’ does not name a type
main.cpp: In function ‘void initxyzzy()’:
main.cpp:61: error: ‘PyImport_AddModule’ was not declared in this scope
main.cpp:62: error: ‘xyzzy_methods’ was not declared in this scope
main.cpp:62: error: ‘Py_InitModule’ was not declared in this scope

I'm not specifying the libraries properly...the following lib-s for python2.6 appear to be available:

/usr/lib/libpython2.5.so.1
/usr/lib/libpython2.5.so.1.0
/usr/lib/libpython2.6.a
/usr/lib/libpython2.6.so
/usr/lib/libpython2.6.so.1
/usr/lib/libpython2.6.so.1.0
/usr/lib/python2.6/config/libpython2.6-pic.a
/usr/lib/python2.6/config/libpython2.6.a
/usr/lib/python2.6/config/libpython2.6.so

...but my attempts to 'link' to these lib-s have been unsuccessful...wrong libs, perhaps, or wrong parameters...I'm stuck...any pointers?

HS

System: Xubuntu, Karmic 9.10
Compiler: gcc
Python: python2.6
IDE: CODE::BLOCKS

Trying the following example:

#include "Python.h"

void initxyzzy(void); /* Forward */

int main(int argc, char **argv)
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);

/* Initialize the Python interpreter. Required. */
Py_Initialize();

/* Add a static module */
initxyzzy();

/* Define sys.argv. It is up to the application if you
want this; you can also let it undefined (since the Python
code is generally not a main program it has no business
touching sys.argv...) */
PySys_SetArgv(argc, argv);

/* Do some application specific code */
printf("Hello, brave new world\n\n");

/* Execute some Python statements (in module __main__) */
PyRun_SimpleString("import sys\n");
PyRun_SimpleString("print sys.builtin_module_names\n");
PyRun_SimpleString("print sys.modules.keys()\n");
PyRun_SimpleString("print sys.executable\n");
PyRun_SimpleString("print sys.argv\n");

/* Note that you can call any public function of the Python
interpreter here, e.g. call_object(). */

/* Some more application specific code */
printf("\nGoodbye, cruel world\n");

/* Exit, cleaning up the interpreter */
Py_Exit(0);
/*NOTREACHED*/
}

/* A static module */

/* 'self' is not used */
static PyObject *
xyzzy_foo(PyObject *self, PyObject* args)
{
return PyInt_FromLong(42L);
}

static PyMethodDef xyzzy_methods[] = {
{"foo", xyzzy_foo, METH_NOARGS,
"Return the meaning of everything."},
{NULL, NULL} /* sentinel */
};

void
initxyzzy(void)
{
PyImport_AddModule("xyzzy");
Py_InitModule("xyzzy", xyzzy_methods);
}

Either in CODE::BLOCKS or from the command line, I get the following error:

main.cpp:1:20: error: Python.h: No such file or directory
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8: error: ‘Py_SetProgramName’ was not declared in this scope
main.cpp:11: error: ‘Py_Initialize’ was not declared in this scope
main.cpp:20: error: ‘PySys_SetArgv’ was not declared in this scope
main.cpp:23: error: ‘printf’ was not declared in this scope
main.cpp:26: error: ‘PyRun_SimpleString’ was not declared in this scope
main.cpp:39: error: ‘Py_Exit’ was not declared in this scope
main.cpp: At global scope:
main.cpp:46: error: expected initializer before ‘*’ token
main.cpp:52: error: ‘PyMethodDef’ does not name a type
main.cpp: In function ‘void initxyzzy()’:
main.cpp:61: error: ‘PyImport_AddModule’ was not declared in this scope
main.cpp:62: error: ‘xyzzy_methods’ was not declared in this scope
main.cpp:62: error: ‘Py_InitModule’ was not declared in this scope

I'm not specifying the libraries properly...the following lib-s for python2.6 appear to be available:

/usr/lib/libpython2.6.a
/usr/lib/libpython2.6.so
/usr/lib/libpython2.6.so.1
/usr/lib/libpython2.6.so.1.0

...but my attempts to 'link' to these lib-s have been unsuccessful...wrong libs, perhaps, or wrong parameters...I'm stuck...any pointers?

HS

> from the command line

Can you show the command line?

main.cpp:1:20: error: Python.h: No such file or directory

Python.h is under /usr/include/pythonx.y on my Slackware box. If you don't have it then you will have to install the source.

You can get the python include directory like this

>>> from distutils.sysconfig import get_python_inc
>>> print(get_python_inc())
/usr/include/python2.6

Now, if you don't have Python.h, you must install a package named python-dev. Dont forget to add the option -I/usr/include/python2.6 when you invoke gcc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.