extending python

Started by
6 comments, last by rmtew 14 years, 9 months ago
i want to write some extension modules, but every time i try something it fails. i know one usually extends with C, but i'm using C++. the code i'm trying is:

#include <Python.h>
int a, b, sum;
static PyObject *
Add(PyObject *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, "ii", &a, &b))
       return NULL;
    sum = a + b;
    return Py_BuildValue("i", sum);
}

PyMethodDef adder_methods[] = {
   {"add", Add, METH_VARARGS, "Add two integers"},
   {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initadder()
{
     (void) Py_InitModule("adder", adder_methods);
}
but it says: one unresolved external. can somebody please help me? one note: i don't want to use any library (boost, ect.) assainator
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

Advertisement
I have no idea what's wrong with what you are doing. What I normally do, is just take a working example extension module, replace the example code with my own and rename it. Then go from there.

Developing primarily on Windows, I use this as a base, and this documentation section as a refresher.
thanks, but for some reason it can't compile, downloaded all the files, use "python setup.py build" but says this:
Quote:c:\python26\include\pyconfig.h(233) : fatal error C1083: Cannot open include file: 'basetsd.h': No such file or directory


assainator
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

Quote:Original post by assainator
thanks, but for some reason it can't compile, downloaded all the files, use "python setup.py build" but says this:
Quote:c:\python26\include\pyconfig.h(233) : fatal error C1083: Cannot open include file: 'basetsd.h': No such file or directory


assainator


In order for others to have enough detail to help you, you need to provide a reasonable level of detail. In your first post, you did not mention what the unresolved symbol was, what platform you were using or what compiler (and version of it) you were using.

Without that information, they need to guess the missing information to provide help that may or may not be relevant. I don't know about other people, but I don't have the time to spend speculating solutions to problems which do not have the context to be fully understood.

However, I will say that when I am compiling something and I get an error I have not seen before, the first thing I do is google it. Nine times out of ten, I can find someone else with the same problem and replies describing what is missing. For instance, in the link given for the first match I see has this solution. Here's another solution also in the first page of links, which is in an FAQ into which it looks a lot of effort has gone into.
sorry, at the time i tought it was enough informations.
i'll try to give more explanation next time.

But i if i try to compile this:
#include "Python.h"static PyObject *ex_foo(PyObject *self, PyObject *args){	printf("Hello, world\n");	Py_INCREF(Py_None);	return Py_None;}static PyMethodDef example_methods[] = {	{"foo", ex_foo, METH_VARARGS, "foo() doc string"},	{NULL, NULL}};PyMODINIT_FUNCinitexample(void){	Py_InitModule("example", example_methods);}


i get this output from my setup.py:
Quote:running install
running build
running build_ext
building 'example' extension
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
3 /GS- /DNDEBUG -IC:\Python26\include -IC:\Python26\PC /Tpexample3.cpp /Fobuildtemp.win32-2.6\Release\example3.obj
example3.cpp
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo /INCRE
MENTAL:NO /LIBPATH:C:\Python26\libs /LIBPATH:C:\Python26\PCbuild /EXPORT:initexa
mple build\temp.win32-2.6\Release\example3.obj /OUT:build\lib.win32-2.6\example.
pyd /IMPLIB:build\temp.win32-2.6\Release\example.lib /MANIFESTFILE:build\temp.wi
n32-2.6\Release\example.pyd.manifest
LINK : error LNK2001: unresolved external symbol initexample
build\temp.win32-2.6\Release\example.lib : fatal error LNK1120: 1 unresolved ext
ernals
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"'
failed with exit status 1120


how do i fix this?

assainator
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

Quote:Original post by assainator
LINK : error LNK2001: unresolved external symbol initexample
build\temp.win32-2.6\Release\example.lib : fatal error LNK1120: 1 unresolved ext
ernals
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"'
failed with exit status 1120

how do i fix this?

assainator


google the error.

Seems like there are some good matches there which show people commenting on the same error.
yes and i've searched 5 pages before posting here and NO solution........

assainator
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

Quote:Original post by assainator
yes and i've searched 5 pages before posting here and NO solution........

assainator


This is confusing me. Are you reading the linked pages in the results of the linked search I gave? The one I picked to look at first had someone saying something like "I had an error 'unresolved external symbol'" and then proceeding to say something like "I did this and it then compiled." Is that what you consider to be a possible solution? I consider it to be one.

This was a simple matter of googling with the exact error message, choosing a result and finding the part of the page which quoted that exact error message.

This topic is closed to new replies.

Advertisement