Thread: Creating a function from C code

Creating a function from C code

From
José Paumard
Date:
Hello everybody,

I am trying to understand how to create functions written in C available
in postgres. I tried to carefully follow the example provided in the
Programmer's guide, given with the 6.4 distribution.
So :
1) I typed the add_one example :
#include "postgres.h"
int4
add_one(int4 arg)
{
  return(arg + 1) ;
  /* that one is complex enough for me ! */
}

and compiled it with a :
$ gcc add_one.c -c -o add_one.o -I/usr/local/pgsql/include
and linked it with a :
$ ld -Bshareable -o add_one.so add_one.o -lpq -L/usr/local/pgsql/lib
and everything ran fine
2) I had the user postgres put the add_one.so file in
/usr/local/pgsql/lib :
-rwxr-xr-x   1 postgres postgres     2465 Dec  4 16:51 add_one.so
3) I launched postgres with the command :
$ psql template1
and then :
template1=> create function add_one(int4) returns int4 as
'/usr/local/pgsql/lib/add_one.so' language 'c' ;
ERROR:  Can't find function add_one in file
/usr/local/pgsql/lib/essai.so
gr@!#$*.h|?mph

The Programmer's guide says that there are explainations in the Appendix
A about the options needed by the linker to generate code understandable
by Postgres. I did not find any. I tried to look at the makfiles in the
contrib directory of the distribution, and cant find what I am doing
wrong.

Any help would be much appreciated, could you answer me to my personnal
adress, if I solve this problem, I will make a summary in this forum.
Thank you again,

Jose Paumard
Jose.Paumard@cmla.ens-cachan.fr