Re: "undefined reference" error when compiling

Lists: pgsql-hackers-win32
From: "Mark Miller" <mark(at)maxpreps(dot)com>
To: <pgsql-hackers-win32(at)postgresql(dot)org>
Subject: "undefined reference" error when compiling extension functions
Date: 2005-04-25 20:36:41
Message-ID: 20050425203935.86E1B54A46@svr1.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

I am trying to figure out how to write extension functions and I am getting
"undefined reference" errors when I compile on the lines which call "malloc"
and "pfree". How can I get the compiler to see the functions so I can
compile successfully?

I installed PostgreSQL on XP Pro using the windows installer, then I
installed the includes (which are not part of the windows install) by
running the following commands:

configure --without-zlib --includedir=/c/progra~1/postgresql/8.0/include
--with-includes=/c/progra~1/postgresql/8.0/include/port/win32/*

make -C src/include install

Here's the output from make when I try to compile:

$ make makefile filesize.dll

make: Nothing to be done for `makefile'.

gcc -g -I c:/Progra~1/PostgreSQL/8.0/include -I
c:/Progra~1/PostgreSQL/8.0/include/server -I
c:/Progra~1/PostgreSQL/8.0/include/server/port/win32 -I
c:/Progra~1/PostgreSQL/8.0/lib -fpic -c filesize.c

cc1.exe: warning: -fpic ignored for target (all code is position
independent)

gcc -g -I c:/Progra~1/PostgreSQL/8.0/include -I
c:/Progra~1/PostgreSQL/8.0/include/server -I
c:/Progra~1/PostgreSQL/8.0/include/server/port/win32 -I
c:/Progra~1/PostgreSQL/8.0/lib -shared -o filesize.dll filesize.o

filesize.o(.text+0x1a): In function `filesize':

C:/msys/1.0/home/mark/dev/filesize.c:9: undefined reference to
`pg_detoast_datum'

filesize.o(.text+0x3d):C:/msys/1.0/home/mark/dev/filesize.c:11: undefined
reference to `_imp__CurrentMemoryContext'

filesize.o(.text+0x44):C:/msys/1.0/home/mark/dev/filesize.c:11: undefined
reference to `MemoryContextAlloc'

filesize.o(.text+0x9b):C:/msys/1.0/home/mark/dev/filesize.c:19: undefined
reference to `pfree'

filesize.o(.text+0xb1):C:/msys/1.0/home/mark/dev/filesize.c:25: undefined
reference to `pfree'

make: *** [filesize.dll] Error 1

Here's the source I'm trying to compile (taken from PostgreSQL ch 6 by
Douglas and Douglas):

#include "postgres.h"

#include "fmgr.h"

#include <sys/stat.h>

PG_FUNCTION_INFO_V1(filesize);

Datum filesize(PG_FUNCTION_ARGS)

{

text * fileNameText = PG_GETARG_TEXT_P(0);

size_t fileNameLen = VARSIZE( fileNameText ) - VARHDRSZ;

char * fileName = (char *)palloc( fileNameLen + 1 );

struct stat statBuf;

memcpy( fileName, VARDATA( fileNameText), fileNameLen );

fileName[fileNameLen] = '\0';

if( stat(fileName, &statBuf) == 0 && S_ISREG(statBuf.st_mode) )

{

pfree( fileName );

PG_RETURN_INT32((int32)statBuf.st_size);

}

else

{

pfree( fileName );

PG_RETURN_NULL();

}

}

Here's the makefile, from the same example but modified to include the
needed include directories and to output ".dll" file instead of ".so".

# File name: makefile

SERVER_INCLUDES += -I $(shell pg_config --includedir)

SERVER_INCLUDES += -I $(shell pg_config --includedir-server)

SERVER_INCLUDES += -I $(shell pg_config --includedir-server)/port/win32

SERVER_INCLUDES += -I $(shell pg_config --libdir)

CFLAGS += -g $(SERVER_INCLUDES)

.SUFFIXES: .dll

.c.dll:

$(CC) $(CFLAGS) -fpic -c $<

$(CC) $(CFLAGS) -shared -o $@ $(basename $<).o


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: mark(at)maxpreps(dot)com
Cc: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: "undefined reference" error when compiling
Date: 2005-04-25 21:04:49
Message-ID: 426D5B71.7070303@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32


You might find using the pgxs setup, new in 8.0, useful. See
http://www.postgresql.org/docs/current/static/xfunc-c.html#XFUNC-C-PGXS

cheers

andrew

Mark Miller wrote:

> I am trying to figure out how to write extension functions and I am
> getting “undefined reference” errors when I compile on the lines which
> call “malloc” and “pfree”. How can I get the compiler to see the
> functions so I can compile successfully?
>
> I installed PostgreSQL on XP Pro using the windows installer, then I
> installed the includes (which are not part of the windows install) by
> running the following commands:
>
> configure --without-zlib
> --includedir=/c/progra~1/postgresql/8.0/include
> --with-includes=/c/progra~1/postgresql/8.0/include/port/win32/*
>
> make -C src/include install
>
> Here’s the output from make when I try to compile:
>
> $ make makefile filesize.dll
>
> make: Nothing to be done for `makefile'.
>
> gcc -g -I c:/Progra~1/PostgreSQL/8.0/include -I
> c:/Progra~1/PostgreSQL/8.0/include/server -I
> c:/Progra~1/PostgreSQL/8.0/include/server/port/win32 -I
> c:/Progra~1/PostgreSQL/8.0/lib -fpic -c filesize.c
>
> cc1.exe: warning: -fpic ignored for target (all code is position
> independent)
>
> gcc -g -I c:/Progra~1/PostgreSQL/8.0/include -I
> c:/Progra~1/PostgreSQL/8.0/include/server -I
> c:/Progra~1/PostgreSQL/8.0/include/server/port/win32 -I
> c:/Progra~1/PostgreSQL/8.0/lib -shared -o filesize.dll filesize.o
>
> filesize.o(.text+0x1a): In function `filesize':
>
> C:/msys/1.0/home/mark/dev/filesize.c:9: undefined reference to
> `pg_detoast_datum'
>
> filesize.o(.text+0x3d):C:/msys/1.0/home/mark/dev/filesize.c:11:
> undefined reference to `_imp__CurrentMemoryContext'
>
> filesize.o(.text+0x44):C:/msys/1.0/home/mark/dev/filesize.c:11:
> undefined reference to `MemoryContextAlloc'
>
> filesize.o(.text+0x9b):C:/msys/1.0/home/mark/dev/filesize.c:19:
> undefined reference to `pfree'
>
> filesize.o(.text+0xb1):C:/msys/1.0/home/mark/dev/filesize.c:25:
> undefined reference to `pfree'
>
> make: *** [filesize.dll] Error 1
>
> Here’s the source I’m trying to compile (taken from PostgreSQL ch 6 by
> Douglas and Douglas):
>
> #include "postgres.h"
>
> #include "fmgr.h"
>
> #include <sys/stat.h>
>
> PG_FUNCTION_INFO_V1(filesize);
>
> Datum filesize(PG_FUNCTION_ARGS)
>
> {
>
> text * fileNameText = PG_GETARG_TEXT_P(0);
>
> size_t fileNameLen = VARSIZE( fileNameText ) - VARHDRSZ;
>
> char * fileName = (char *)palloc( fileNameLen + 1 );
>
> struct stat statBuf;
>
> memcpy( fileName, VARDATA( fileNameText), fileNameLen );
>
> fileName[fileNameLen] = '\0';
>
> if( stat(fileName, &statBuf) == 0 && S_ISREG(statBuf.st_mode) )
>
> {
>
> pfree( fileName );
>
> PG_RETURN_INT32((int32)statBuf.st_size);
>
> }
>
> else
>
> {
>
> pfree( fileName );
>
> PG_RETURN_NULL();
>
> }
>
> }
>
> Here’s the makefile, from the same example but modified to include the
> needed include directories and to output “.dll” file instead of “.so”.
>
> # File name: makefile
>
> SERVER_INCLUDES += -I $(shell pg_config --includedir)
>
> SERVER_INCLUDES += -I $(shell pg_config --includedir-server)
>
> SERVER_INCLUDES += -I $(shell pg_config --includedir-server)/port/win32
>
> SERVER_INCLUDES += -I $(shell pg_config --libdir)
>
> CFLAGS += -g $(SERVER_INCLUDES)
>
> .SUFFIXES: .dll
>
> .c.dll:
>
> $(CC) $(CFLAGS) -fpic -c $<
>
> $(CC) $(CFLAGS) -shared -o $@ $(basename $<).o
>