Thread: Problem calling C function in PostgreSQL

Problem calling C function in PostgreSQL

From
Date:
Hi,

I am new to PostgreSQL  and could  anyone please suggest me any windows based C compiler which will successfully create
adll on windows and which can be callable by PostgreSQL. 


I am facing a problem with the below code compiled and created dll in MSVC 2005.

_______________________________________________________________________

Here is my code:

#define _USE_32BIT_TIME_T
#include "postgres.h"
#include "fmgr.h"

//#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
//#endif


PG_FUNCTION_INFO_V1(add_one);
Datum add_one(PG_FUNCTION_ARGS)
{
    int32   arg = PG_GETARG_INT32(0);

    PG_RETURN_INT32(arg + 1);
}

______________________________________________________________

But when I am calling this function from POstgreSQL the below error is thrown.
____________________________________________________________________________
CREATE OR REPLACE FUNCTION add_test(iparam int) RETURNS int AS '$libdir/add', 'add_one' LANGUAGE C;

ERROR:  could not find function "add_one" in file "C:/Program Files/PostgreSQL/8.3/lib/add.dll"
____________________________________________________________________________

It would be kind if any help!


Thanks and regards
AB


Re: Problem calling C function in PostgreSQL

From
Magnus Hagander
Date:
2009/10/22 A.Bhattacharya <A.Bhattacharya@sungard.com>:
> Hi,
>
> I am new to PostgreSQL  and could  anyone please suggest me any windows based C compiler which will successfully
createa dll on windows and which can be callable by PostgreSQL. 
>
>
> I am facing a problem with the below code compiled and created dll in MSVC 2005.
>
> _______________________________________________________________________
>
> Here is my code:
>
> #define _USE_32BIT_TIME_T
> #include "postgres.h"
> #include "fmgr.h"
>
> //#ifdef PG_MODULE_MAGIC
> PG_MODULE_MAGIC;
> //#endif
>
>
> PG_FUNCTION_INFO_V1(add_one);
> Datum add_one(PG_FUNCTION_ARGS)
> {
>    int32   arg = PG_GETARG_INT32(0);
>
>    PG_RETURN_INT32(arg + 1);
> }
>
> ______________________________________________________________
>
> But when I am calling this function from POstgreSQL the below error is thrown.
> ____________________________________________________________________________
> CREATE OR REPLACE FUNCTION add_test(iparam int) RETURNS int AS '$libdir/add', 'add_one' LANGUAGE C;
>
> ERROR:  could not find function "add_one" in file "C:/Program Files/PostgreSQL/8.3/lib/add.dll"
> ____________________________________________________________________________
>
> It would be kind if any help!

Try adding __declspec(dllexport) to the function, that might be what
you're missing unless you have a DEF file exporting it. Also, make
sure your function is C and not C++.

--
 Magnus Hagander
 Me: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/