Обсуждение: Trying to make functions in 'C'
I'm tying to learn how to make function in 'C'.
I'm trying to compile a program I made in 'C'.
The command I gave were:
% gcc -fPIC -c addone.c -I/usr/local/pgsql/include
% ld -G -Bdynamic -o addone.so addone.o
and created the function in postgres like this:
CREATE FUNCTION add_one(int4) RETURNS int4
AS './addone.so' LANGUAGE 'c';
The function I made looks like this:
#include "postgres.h"
int
add_one(int arg)
{
return arg + 1;
}
But when I try to test the function in postgres I recieve this error:
ERROR: stat failed on file ./addone.so
ERROR: stat failed on file ./addone.so
Can anyone tell where did I make a mistake?
I have a linux on a PC.
Thank you.
Fabian.
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
> I'm tying to learn how to make function in 'C'.
> I'm trying to compile a program I made in 'C'.
> The command I gave were:
>
> % gcc -fPIC -c addone.c -I/usr/local/pgsql/include
> % ld -G -Bdynamic -o addone.so addone.o
>
> and created the function in postgres like this:
>
> CREATE FUNCTION add_one(int4) RETURNS int4
> AS './addone.so' LANGUAGE 'c';
^^^^^^^^^^^^^
one must provide an absolute path here
--Gene
>>>>> "fb" == fabian baena <fabbaena@hotmail.com> writes:
fb> I'm tying to learn how to make function in 'C'.
fb> I'm trying to compile a program I made in 'C'.
fb> The command I gave were:
fb> % gcc -fPIC -c addone.c -I/usr/local/pgsql/include
fb> % ld -G -Bdynamic -o addone.so addone.o
fb> and created the function in postgres like this:
fb> CREATE FUNCTION add_one(int4) RETURNS int4
fb> AS './addone.so' LANGUAGE 'c';
fb> The function I made looks like this:
fb> #include "postgres.h"
fb> int
fb> add_one(int arg)
fb> {
fb> return arg + 1;
fb> }
fb> But when I try to test the function in postgres I recieve this error:
fb> ERROR: stat failed on file ./addone.so
fb> ERROR: stat failed on file ./addone.so
fb> Can anyone tell where did I make a mistake?
fb> I have a linux on a PC.
Pehaps not 'int', but 'int4' in argument and return type? Except this
I think your example is right.
--
Anatoly K. Lasareff Email: tolik@icomm.ru
Senior programmer
> >>>>> "fb" == fabian baena <fabbaena@hotmail.com> writes:
>
> fb> I'm tying to learn how to make function in 'C'.
> fb> I'm trying to compile a program I made in 'C'.
> fb> The function I made looks like this:
>
> fb> #include "postgres.h"
>
> fb> int
> fb> add_one(int arg)
> fb> {
> fb> return arg + 1;
> fb> }
>
> Pehaps not 'int', but 'int4' in argument and return type? Except this
> I think your example is right.
As far as return types, make sure you get what you want by
checking how types are typedef'ed in postgres.h
The safest way to add your own code would be what Fabian is trying to
accomplish -- by using the prototypes from tutorial.
--Gene
hello every body i have some problem with LOAD command postgres=> LOAD '/usr/local/pgsql/complex.so'; pqReadData() -- backend closed the channel unexpectedly. This probably means the backend terminated abnormally before or while processing the request. We have lost the connection to the backend, so further processing is impossible. Terminating. $>> I build complex.so by: >> gcc -I../include -I../backend -O2 -m486 -pipe -Wall -Wmissing-prototypes -I../interfaces/libpq -I../../include -c complex.c -o complex.o >> ld -x -r -o complex.o.obj complex.o >> ranlib complex.so.pic >> ld -x -Bshareable -o complex.so complex.so.pic I use FreeBSD-3.1 with PostgreSQL 6.4.2 thanks
This is a known problem with FreeBSD-3.1 and PostgreSQL I had too with
6.5 cvs version. I suppose you use FreeBSD elf ? Take a look to
ports for freebsd-elf specific patches or just change
src/Makefile.shlib
ifeq ($(PORTNAME), freebsd)
ifdef BSD_SHLIB
install-shlib-dep := install-shlib
ifdef ELF_SYSTEM
shlib := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
LDFLAGS_SL := -x -shared -soname $(shlib)
else
shlib := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
LDFLAGS_SL := -x -Bshareable -Bforcearchive
endif
CFLAGS += $(CFLAGS_SL)
endif
endif
and makefiles/Makefile.freebsd
ifdef ELF_SYSTEM
LDFLAGS+= -export-dynamic
endif
%.so: %.o
ifdef ELF_SYSTEM
$(LD) -x -shared -o $@ $<
else
$(LD) -x -r -o $<.obj $<
@echo building shared object $@
@rm -f $@.pic
@${AR} cq $@.pic order $<.obj | tsort
${RANLIB} $@.pic
@rm -f $@
$(LD) -x -Bshareable -Bforcearchive -o $@ $@.pic
endif
This works for me !
Regards,
Oleg
PS.
These patches are already applied to current 6.5 sources.
On Thu, 20 May 1999, abdelkrim wrote:
> Date: Thu, 20 May 1999 18:06:30 +0000
> From: abdelkrim <haj@idianet.net>
> To: pgsql-general@postgresql.org, pgsql-interfaces@postgresql.org
> Subject: [INTERFACES] problem with LOAD
>
> hello every body
>
> i have some problem with LOAD command
>
> postgres=> LOAD '/usr/local/pgsql/complex.so';
> pqReadData() -- backend closed the channel unexpectedly.
> This probably means the backend terminated abnormally before or while
> processing the request.
> We have lost the connection to the backend, so further processing is
> impossible. Terminating.
> $>>
>
> I build complex.so by:
>
> >> gcc -I../include -I../backend -O2 -m486 -pipe -Wall
> -Wmissing-prototypes -I../interfaces/libpq -I../../include -c
> complex.c -o complex.o
> >> ld -x -r -o complex.o.obj complex.o
> >> ranlib complex.so.pic
> >> ld -x -Bshareable -o complex.so complex.so.pic
>
> I use FreeBSD-3.1 with PostgreSQL 6.4.2
>
> thanks
>
>
>
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83
hello
CREATE SEQUENCE seq
INCREMENT 1
MINVALUE 1
START 1
;
CREATE TABLE T (
index int primary key UNIQUE DEFAULT nextval('seq'),
f1 char(5),
f2 char(5)
);
CREATE TABLE T1 (
f3 char(5),
f4 char(5),
f5 char(5)
) INHERITS (T);
CREATE TABLE T2(
cp1 char(5),
cp2 char(5)
) INHERITS (T);
INSERT INTO T1 VALUES ( nextval('seq'), 'joe', 'joe', 'joe', 'joe', 'joe');
INSERT INTO T1 VALUES ( nextval('seq'), 'davis', 'davis', 'davis', 'davis',
'davis');
INSERT INTO T1 VALUES ( nextval('seq'), 'ba', 'ba', 'ba', 'ba', 'ba');
INSERT INTO T2 VALUES ( nextval('seq'), 'aw', 'aw', 'aw', 'aw');
INSERT INTO T2 VALUES ( nextval('seq'), 'es', 'es', 'es', 'es');
SELECT * FROM T*;
this request give me only the fields in table T but I want to give me all
fiels in T1 and T2
how ?
thanks
Jonathan Davis wrote:
> hello
>
> CREATE SEQUENCE seq
>
> INCREMENT 1
> MINVALUE 1
> START 1
> ;
>
> CREATE TABLE T (
> index int primary key UNIQUE DEFAULT nextval('seq'),
> f1 char(5),
> f2 char(5)
>
> );
>
> CREATE TABLE T1 (
> f3 char(5),
> f4 char(5),
> f5 char(5)
>
> ) INHERITS (T);
>
> CREATE TABLE T2(
> cp1 char(5),
> cp2 char(5)
> ) INHERITS (T);
>
> INSERT INTO T1 VALUES ( nextval('seq'), 'joe', 'joe', 'joe', 'joe', 'joe');
> INSERT INTO T1 VALUES ( nextval('seq'), 'davis', 'davis', 'davis', 'davis',
> 'davis');
> INSERT INTO T1 VALUES ( nextval('seq'), 'ba', 'ba', 'ba', 'ba', 'ba');
>
> INSERT INTO T2 VALUES ( nextval('seq'), 'aw', 'aw', 'aw', 'aw');
> INSERT INTO T2 VALUES ( nextval('seq'), 'es', 'es', 'es', 'es');
>
> SELECT * FROM T*;
>
> this request give me only the fields in table T but I want to give me all
> fiels in T1 and T2
> how ?
>
> thanks