linking with shared library, undefined reference to Py_BuildValue

JW jkpangtang at yahoo.com
Tue Mar 4 19:21:10 EST 2003


Hi all,

I'm fairly new to unix and python, so have patience with my questioning....

I have 2 files:
1) spam.c
int gcd(int x, int y)
{
	int g;
	g = y;
	while(x > 0)
	{
		g = x;
		x = y % x;
		y = g;
	}
	
	return g;
}

2) spamwrapper.c
#include "Python.h"

extern int gcd(int, int);
extern void print_data(char *, char *, char *);

PyObject* spam_gcd(PyObject *self, PyObject *args)
{
	int x, y, g;

	if(!PyArg_ParseTuple(args, "ii", &x, &y))
	{
		return NULL;
	}

	g = gcd(x, y);
	return Py_BuildValue("i", g);
}


---
Then, i make a shared libary file:

PYTHON = python2.2
INCLUDE = -I/usr/include/$(PYTHON) 
gcc -c -fpic $(INCLUDE) spam.c spamwrapper.c
gcc -shared spam.o spamwrapper.c -o spammodule.so


After this, i have no problem from within a python script :
import spam
a=input("1st number:")
b=input("2nd:")
x=spam.gcd(a,b)
 

--
My problem occurs when I try to link the spammodule.so 
to another .c file so that i could use the gcd() function

Example:
//file main.c
int main()
{
  int x,a,b;
  a=5;
  b=10;
  x=gcd(a,b);
  return 0;
}


When i try compiling and linking main.c with spammodule.so and libpython2.2.a,
i get the following error messages:
-->./spammodule.so - undefined reference 'Py_BuildValue'
--> ./spammodule.so - undefined reference 'Py_InitModule4'

Any suggestions?
Thanks




More information about the Python-list mailing list