Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2007
    Posts
    1

    Unanswered: Building C user-defined functions for Postgres in Windows

    I am new to writing user-defined functions for Postgres in C under Windows. I am trying to build one using a gcc Windows compiler (DevC++). When I try to build it (I use SPI inside the function) it passes the compilation, but then it quits on me in the linker, saying that I have an undefined reference to 'libpostgres_a_iname' and to '_nm__SPI_tuptable': (just as a comment, the same type of misunderstanding was generated when I tried using palloc and pfree: it could not find reference to 'CurrentMemoryContext'.

    I am attaching below the compiler output, followed by my C-code followed by the makefile generated by DevC++ (the gcc compiler).

    Compiler: Default compiler
    Building Makefile: "C:\Program Files\PostgreSQL\8.1\myPGExtension\Makefile.win"
    Executing make clean
    rm -f dllmain.o myPGExtension.dll

    gcc.exe -c dllmain.c -o dllmain.o -I"C:/Dev-Cpp/include" -I"C:/Program Files/PostgreSQL/8.1/include" -I"C:/Program Files/PostgreSQL/8.1/include/server" -I"C:/Program Files/PostgreSQL/8.1/include/server/access" -I"C:/Program Files/PostgreSQL/8.1/include/server/executor" -I"C:/Program Files/PostgreSQL/8.1/include/server/utils" -I"C:/Program Files/PostgreSQL/8.1/include/server/port" -I"C:/Program Files/PostgreSQL/8.1/include/server/port/win32" -I"C:/Program Files/PostgreSQL/8.1/include/server/nodes" -DBUILDING_DLL=1

    dllwrap.exe --output-def libmyPGExtension.def --implib libmyPGExtension.a dllmain.o -L"C:/Dev-Cpp/lib" -L"C:/Program Files/PostgreSQL/8.1/lib" -L"C:/Program Files/PostgreSQL/8.1/lib/" ../lib/libpq.a ../lib/libecpg.a ../lib/libecpg_compat.a ../lib/libpgport.a ../lib/libpgtypes.a ../lib/libpostgres.a ../lib/pgxs/src/utils/dllinit.o ../lib/ms/libecpg.lib ../lib/ms/libpq.lib -o myPGExtension.dll

    Info: resolving _SPI_tuptable by linking to __imp__SPI_tuptable (auto-import)
    fu000001.o(.idata$3+0xc): undefined reference to `libpostgres_a_iname'
    nmth000000.o(.idata$4+0x0): undefined reference to `_nm__SPI_tuptable'
    collect2: ld returned 1 exit status

    dllwrap.exe: no export definition file provided.
    Creating one, but that may not be what you want
    dllwrap.exe: gcc exited with status 1

    make.exe: *** [myPGExtension.dll] Error 1

    Execution terminated

    My code is nothing special; just trying to run SPI_exec on a query being passed into the function. Here it is:

    //#include "dll.h"
    #include <stdio.h>
    #include <stdlib.h>

    #include "postgres.h"
    #include "miscadmin.h"
    #include "spi.h"
    #include "memnodes.h"
    #include "memutils.h"
    #include "htup.h"
    #include "fmgr.h"

    extern SPITupleTable* SPI_Tuptable;


    DLLIMPORT int32 AddTwoNumbers (int32 a, int32 b);
    DLLIMPORT int32 AddTwoNumbers (int32 a, int32 b)
    {
    int32 c = a + b;
    return (c);
    }


    /**
    * This function will add two columns
    * First argument is the first query
    * Second argument is the second query
    * Third argument is the factor by which to multiply the second column
    **/
    PG_FUNCTION_INFO_V1 (AddTwoColumns);
    DLLIMPORT Datum AddTwoColumns (PG_FUNCTION_ARGS)
    {
    int32* arA;
    int32* arB;
    int32 factor ;
    int32 numRows;

    char* query1;

    query1 = (char*)PG_GETARG_TEXT_P (0);
    query2 = (char*)PG_GETARG_TEXT_P (1);
    /*Get the data returned by the first query*/
    int spiRet = SPI_exec(query1, 1);

    if (spiRet != SPI_OK_SELECT)
    return (Datum)0;

    return (Datum)SPI_tuptable->vals[0];
    }


    This is my first attempt to create a UDF for Postgres. Please help!


    Just in case, I am attaching a copy of the makefile that DevC++ generated:


    # Project: myPGExtension
    # Makefile created by Dev-C++ 4.9.9.2

    CPP = g++.exe
    CC = gcc.exe
    WINDRES = windres.exe
    RES =
    OBJ = dllmain.o $(RES)
    LINKOBJ = dllmain.o $(RES)
    LIBS = -L"C:/Dev-Cpp/lib" -L"C:/Program Files/PostgreSQL/8.1/lib" -L"C:/Program Files/PostgreSQL/8.1/lib/" ../lib/libpq.a ../lib/libecpg.a ../lib/libecpg_compat.a ../lib/libpgport.a ../lib/libpgtypes.a ../lib/libpostgres.a ../lib/pgxs/src/utils/dllinit.o ../lib/ms/libecpg.lib ../lib/ms/libpq.lib
    INCS = -I"C:/Dev-Cpp/include" -I"C:/Program Files/PostgreSQL/8.1/include" -I"C:/Program Files/PostgreSQL/8.1/include/server" -I"C:/Program Files/PostgreSQL/8.1/include/server/access" -I"C:/Program Files/PostgreSQL/8.1/include/server/executor" -I"C:/Program Files/PostgreSQL/8.1/include/server/utils" -I"C:/Program Files/PostgreSQL/8.1/include/server/port" -I"C:/Program Files/PostgreSQL/8.1/include/server/port/win32" -I"C:/Program Files/PostgreSQL/8.1/include/server/nodes"
    CXXINCS = -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -I"C:/Program Files/PostgreSQL/8.1/include" -I"C:/Program Files/PostgreSQL/8.1/include/server" -I"C:/Program Files/PostgreSQL/8.1/include/server/access" -I"C:/Program Files/PostgreSQL/8.1/include/server/executor" -I"C:/Program Files/PostgreSQL/8.1/include/server/utils" -I"C:/Program Files/PostgreSQL/8.1/include/server/port" -I"C:/Program Files/PostgreSQL/8.1/include/server/port/win32" -I"C:/Program Files/PostgreSQL/8.1/include/server/nodes"
    BIN = myPGExtension.dll
    CXXFLAGS = $(CXXINCS) -DBUILDING_DLL=1
    CFLAGS = $(INCS) -DBUILDING_DLL=1
    RM = rm -f

    .PHONY: all all-before all-after clean clean-custom

    all: all-before myPGExtension.dll all-after


    clean: clean-custom
    ${RM} $(OBJ) $(BIN)

    DLLWRAP=dllwrap.exe
    DEFFILE=libmyPGExtension.def
    STATICLIB=libmyPGExtension.a

    $(BIN): $(LINKOBJ)
    $(DLLWRAP) --output-def $(DEFFILE) --implib $(STATICLIB) $(LINKOBJ) $(LIBS) -o $(BIN)

    dllmain.o: dllmain.c
    $(CC) -c dllmain.c -o dllmain.o $(CFLAGS)




    Thank you very much, and sorry about such a long post. I need to get it working ASAP!



    Alex
    Last edited by alexutiug; 01-26-07 at 19:18.

  2. #2
    Join Date
    Aug 2007
    Posts
    1

    I have the same problem!!!

    I have the same problem .... Could anybody help me?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •