[C++-sig] Python and C integration

Larina D'Souza dsouza at purdue.edu
Wed Apr 2 00:46:53 CEST 2003


I wrote this code that I got somewhere from a book 

#include "/usr/local/include/python2.1/Python.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int fac(int n)
{
 if(n<2)return(1);
 return((n)*fac(n-1));
}

char *reverse(char *s)
{
 register char t,
  *p=s,
  *q=(s+(strlen(s)-1));
 while(s && (p<q))
 {
  t = *p;
  *p++ = *q;
  *q-- = t;
 } return s;
}

void main()
{
 char s[BUFSIZ];
 printf("4!==%d\n",fac(4));
 printf("8!==%d\n",fac(8));
 printf("12!==%d\n",fac(12));
 strcpy(s,"abcdef");
 printf("reversing 'abcdef', we get '%s'\n",reverse(s));
 strcpy(s,"madam");
 printf("reversing 'madam', we get '%s'\n",reverse(s));
}
static PyObject*
testProj_fac(PyObject *self, PyObject *args){
 int res;
 int num;
 PyObject* retval;

 res=PyArg_ParseTuple(args,"i",&num);
 if(!res){
  return NULL;
 }
 res=fac(num);
 retval=(PyObject*)Py_BuildValue("i",res);
 return retval;
}
********************************************************
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??

Please help...
Thanks
Larina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030401/78ce96b6/attachment.htm>


More information about the Cplusplus-sig mailing list