Re: Compiling a user C function in 7.2.1 - Mailing list pgsql-novice

From John Gunther
Subject Re: Compiling a user C function in 7.2.1
Date
Msg-id 3D43E557.4030403@bucksvsbytes.com
Whole thread Raw
In response to Add value in an Array  ("Jerome Chochon" <jerome.chochon@ensma.fr>)
List pgsql-novice
P.S. I forgot to supply the commands I used to compile and link my
bvbpglib.so library:
gcc -O2 -march=i386 -mcpu=i686 -Wall -Wmissing-prototypes
-Wmissing-declarations -fpic
-I/usr/src/redhat/BUILD/postgresql-7.2.1/src/interfaces/libpq
-I/usr/src/redhat/BUILD/postgresql-7.2.1/src/include
-I/usr/kerberos/include  -c -o bvbpglib.o bvbpglib.c
gcc -shared -o /usr/include/bvbpglib.so bvbpglib.o

John G

John Gunther wrote:

> Tom Lane wrote:
>
>> (a) you didn't run configure, which creates the files you are missing.
>>
>>
> Thanks, Tom. Your reply caused me to learn a lot about building
> PostgreSQL from source.
>
> Now I'm much further along: I have created a dynamic library
> (bvbpglib.so) containing one PostgreSQL C function (bvbpgsortword).
> This function calls a "regular" (non-PostgreSQL) C function
> (bvbmakesortstring).
>
> My current problem is that my psql statement:
> CREATE FUNCTION bvbpgsortword(TEXT) RETURNS TEXT AS
> '/usr/include/bvbpglib' LANGUAGE C WITH (ISSTRICT);
> fails with "undefined symbol: bvbmakesortstring"
>
> This is probably the result of some fundamental deficit in my
> understanding of C writing/compiling/linking, but what puzzles me is
> that bvbmakesortstring is called successfully from another C program I
> wrote. Only in psql does it come back undefined.
>
> Thanks.
>
> John Gunther
>
> For anyone is interested in enlightening me, relevant source follows:
>
> SOURCE FOR bvbpglib.c (used to create bvbpglib.so, contains PostgreSQL
> function bvbpgsortword):
> #include "postgres.h"
> #include "fmgr.h"
> #include <string.h>
> #include "bvblib.h"
> Datum bvbpgsortword(PG_FUNCTION_ARGS);
> PG_FUNCTION_INFO_V1(bvbpgsortword);
> Datum bvbpgsortword(PG_FUNCTION_ARGS){
> //declarations
>    char  *bvbmakesortstring(const char *input, const int bitmodes,
> const unsigned short int minlength, const char *keeplist, const char
> *outdelim, const char *stoplist);
>    text *instr = PG_GETARG_TEXT_P(0);
>    text *outstr;
>    unsigned int length;
>    char *target;
> //code
>    target=VARDATA(outstr);
>    target = bvbmakesortstring(VARDATA(instr),1,0,"","}","");
>    length=strlen(target-1);
>    target[length]=0;
>    VARATT_SIZEP(outstr) = length + VARHDRSZ;
>    PG_RETURN_TEXT_P(outstr);
> }
>
> SOURCE FOR test.c (this successfully calls bvbmakesortstring):
> #include <stdio.h>
> #include "bvblib.h"
> int /*function*/ main(){
>
> printf("out=%s\n",bvbmakesortstring("test-mctöst'tëst",0,0,",","=","A"));
>    return 0;
> }
>
> PARTIAL SOURCE FOR bvblib.c (this library contains bvbmakesortstring):
> #include <string.h>
> #include "bvblib.h"
> char    bvbstr[65535];
> char *bvbmakesortstring(
>    const char *instr,
>    const int bitmodes,
>    const unsigned short int minlength,
>    const char *keeplist,
>    const char *outdelim,
>    const char *stoplist
> )
> {<code>...
>     return bvbstr;//return the sort-ready string
> }
>
> SOURCE FOR bvblib.h:
> #ifndef BVBLIB_H     /* has this been included */
> #define BVBLIB_H     /* if not say its been done once */
> char bvbstr[65535];
> char *bvbmakesortstring(
>    const char                                *instr, //string to be
> processed
>    const int                                    bitmodes,
>    const unsigned short int    minlength,    //words shorter than this
> are ignored
>    const char                                  *keeplist,    //chars
> to be kept in output words
>    const char                                  *outdelim,    //char
> string to use as word delimiter
>    const char                                  *stoplist
> //space-separated words to be ignored
> );
> #endif                  /* end the #ifndef construct */
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>



pgsql-novice by date:

Previous
From: John Gunther
Date:
Subject: Re: Compiling a user C function in 7.2.1
Next
From: Tom Lane
Date:
Subject: Re: Compiling a user C function in 7.2.1