Обсуждение: "undefined reference" error when compiling extension functions

Поиск
Список
Период
Сортировка

"undefined reference" error when compiling extension functions

От
"Mark Miller"
Дата:

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

 

Re: "undefined reference" error when compiling

От
Andrew Dunstan
Дата:
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
>

Re: "undefined reference" error when compiling extension functions

От
"Mark Miller"
Дата:
You're right, I could just install it there. That's the easiest way around
it. But I WOULD like to know why it's mapping to the wrong directory and how
to work around it. It would be better if I could install it where ever I
like. I'm doing this all as a learning experince. Since I have no experience
with any of these technologies (C, postgresql, gcc, makefiles), I just want
to figure out how to do it right.

Thanks,
Mark

-----Original Message-----
From: Andrew Dunstan [mailto:andrew@dunslane.net]
Sent: Monday, April 25, 2005 3:15 PM
To: mark@maxpreps.com
Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
compiling extension functions

Install instead to

C:\MSYS\1.0\local\pgsql - under MSys that will map to /usr/local/pgsql

Personally, if I were building extensions I would have built/installed pg
from scratch, rather than using the installer.

cheers

andrew


Mark Miller wrote:

>Thanks, that simplifies things.
>
>But I ran into the following problem:
>
>$ make
>dlltool --export-all --output-def filesize.def filesize.o
>dllwrap -o filesize.dll --def filesize.def filesize.o
>c:/Progra~1/PostgreSQL/8.0/lib/pgxs/src/makefiles/../../src/utils/dllinit.o
>-L/usr/local/pgsql/bin -lpostgres
>c:\MinGW\bin\..\lib\gcc-lib\mingw32\3.2.3\..\..\..\..\mingw32\bin\ld.exe:
>cannot find -lpostgres
>c:\MinGW\bin\dllwrap.exe: c:\MinGW\bin\gcc exited with status 1
>make: *** [filesize.dll] Error 1
>
>
>Here's the makefile (located in /home/usr/dev (windows path is:
>C:\MSYS\1.0\home\mark\dev)  ***:
>
>
>MODULES = filesize
>
>PGXS := $(shell pg_config --pgxs)
>include $(PGXS)
>
>*** ( postgresql is installed in C:\Progra~1\postgresql\8.0\)
>
>It looks like "-L/usr/local/pgsql/bin -lpostgres" is looking in the wrong
>directory. I reinstalled making sure I used the --prefix parameter for
>configure, but it's still looking in the wrong place. How do I point it to
>the right "bin" directory?.
>
>Again, I did a windows install followed by using MinGW and MSYS to install
>the headers needed for development. (The reason for doing separate installs
>was so I could install PostgreSQL as a service and make sure the proper
>accounts were set up).
>
>Thanks again for your help,
>Mark
>
>
>-----Original Message-----
>From: Andrew Dunstan [mailto:andrew@dunslane.net]
>Sent: Monday, April 25, 2005 2:05 PM
>To: mark@maxpreps.com
>Cc: pgsql-hackers-win32@postgresql.org
>Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
>compiling extension functions
>
>
>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
>>
>>
>>
>
>
>


Re: "undefined reference" error when compiling

От
Andrew Dunstan
Дата:
Well, you picked a difficult target to learn on :-)

If you look in pgxs.mk you will see some clues.  You might need to set
something to point to the installed lib directory (Maybe SHLIB_LINK ?).
some value like -L/c/progra~1/postgresql/8.0/lib

And if you are coming from Windows build experience and think all this
has rough edges, you're right, it does.

cheers

andrew

Mark Miller wrote:

>You're right, I could just install it there. That's the easiest way around
>it. But I WOULD like to know why it's mapping to the wrong directory and how
>to work around it. It would be better if I could install it where ever I
>like. I'm doing this all as a learning experince. Since I have no experience
>with any of these technologies (C, postgresql, gcc, makefiles), I just want
>to figure out how to do it right.
>
>Thanks,
>Mark
>
>-----Original Message-----
>From: Andrew Dunstan [mailto:andrew@dunslane.net]
>Sent: Monday, April 25, 2005 3:15 PM
>To: mark@maxpreps.com
>Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
>compiling extension functions
>
>Install instead to
>
>C:\MSYS\1.0\local\pgsql - under MSys that will map to /usr/local/pgsql
>
>Personally, if I were building extensions I would have built/installed pg
>from scratch, rather than using the installer.
>
>cheers
>
>andrew
>
>
>Mark Miller wrote:
>
>
>
>>Thanks, that simplifies things.
>>
>>But I ran into the following problem:
>>
>>$ make
>>dlltool --export-all --output-def filesize.def filesize.o
>>dllwrap -o filesize.dll --def filesize.def filesize.o
>>c:/Progra~1/PostgreSQL/8.0/lib/pgxs/src/makefiles/../../src/utils/dllinit.o
>>-L/usr/local/pgsql/bin -lpostgres
>>c:\MinGW\bin\..\lib\gcc-lib\mingw32\3.2.3\..\..\..\..\mingw32\bin\ld.exe:
>>cannot find -lpostgres
>>c:\MinGW\bin\dllwrap.exe: c:\MinGW\bin\gcc exited with status 1
>>make: *** [filesize.dll] Error 1
>>
>>
>>Here's the makefile (located in /home/usr/dev (windows path is:
>>C:\MSYS\1.0\home\mark\dev)  ***:
>>
>>
>>MODULES = filesize
>>
>>PGXS := $(shell pg_config --pgxs)
>>include $(PGXS)
>>
>>*** ( postgresql is installed in C:\Progra~1\postgresql\8.0\)
>>
>>It looks like "-L/usr/local/pgsql/bin -lpostgres" is looking in the wrong
>>directory. I reinstalled making sure I used the --prefix parameter for
>>configure, but it's still looking in the wrong place. How do I point it to
>>the right "bin" directory?.
>>
>>Again, I did a windows install followed by using MinGW and MSYS to install
>>the headers needed for development. (The reason for doing separate installs
>>was so I could install PostgreSQL as a service and make sure the proper
>>accounts were set up).
>>
>>Thanks again for your help,
>>Mark
>>
>>
>>-----Original Message-----
>>From: Andrew Dunstan [mailto:andrew@dunslane.net]
>>Sent: Monday, April 25, 2005 2:05 PM
>>To: mark@maxpreps.com
>>Cc: pgsql-hackers-win32@postgresql.org
>>Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
>>compiling extension functions
>>
>>
>>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
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 9: the planner will ignore your desire to choose an index scan if your
>      joining column's datatypes do not match
>
>
>

Re: "undefined reference" error when compiling extension functions

От
"Mark Miller"
Дата:
Thanks,

I have to say that it is worlds different than what I'm used to. But it's
never stopped me before ;). I did find why it was pointing to the wrong dir,
the pgxs/src/Makefile.global had the following line in it:

prefix := /usr/local/pgsql

Which likely has something to do with the build, since I did a windows
install the default might not get set. I did set prefix when I ran configure
and make install, but since I only installed includes it make sense that the
file didn't get updated. But sadly, even though I found this and the output
has changed (see below), it did not fix the problem. It still fails with the
following error:

cannot find -lpostgres

Which brings us back to your suggestion of a clean build from scratch. It
seems the only solution unless I want to walk thru every possible place
where I'll run into these problems.

What is "-lpostgres" anyway?

--------------------- make output --------------------------------------

$ make
dlltool --export-all --output-def filesize.def filesize.o
dllwrap -o filesize.dll --def filesize.def filesize.o
c:/Progra~1/PostgreSQL/8.0/lib/pgxs/src/makefiles/../../src/utils/dllinit.o
-L/c/progra~1/postgresql/8.0/bin -lpostgres
c:\MinGW\bin\..\lib\gcc-lib\mingw32\3.2.3\..\..\..\..\mingw32\bin\ld.exe:
cannot find -lpostgres
c:\MinGW\bin\dllwrap.exe: c:\MinGW\bin\gcc exited with status 1
make: *** [filesize.dll] Error 1

------------------------------------------------------------------------


-----Original Message-----
From: Andrew Dunstan [mailto:andrew@dunslane.net]
Sent: Monday, April 25, 2005 3:47 PM
To: mark@maxpreps.com
Cc: pgsql-hackers-win32@postgresql.org
Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
compiling extension functions


Well, you picked a difficult target to learn on :-)

If you look in pgxs.mk you will see some clues.  You might need to set
something to point to the installed lib directory (Maybe SHLIB_LINK ?).
some value like -L/c/progra~1/postgresql/8.0/lib

And if you are coming from Windows build experience and think all this
has rough edges, you're right, it does.

cheers

andrew

Mark Miller wrote:

>You're right, I could just install it there. That's the easiest way around
>it. But I WOULD like to know why it's mapping to the wrong directory and
how
>to work around it. It would be better if I could install it where ever I
>like. I'm doing this all as a learning experince. Since I have no
experience
>with any of these technologies (C, postgresql, gcc, makefiles), I just want
>to figure out how to do it right.
>
>Thanks,
>Mark
>
>-----Original Message-----
>From: Andrew Dunstan [mailto:andrew@dunslane.net]
>Sent: Monday, April 25, 2005 3:15 PM
>To: mark@maxpreps.com
>Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
>compiling extension functions
>
>Install instead to
>
>C:\MSYS\1.0\local\pgsql - under MSys that will map to /usr/local/pgsql
>
>Personally, if I were building extensions I would have built/installed pg
>from scratch, rather than using the installer.
>
>cheers
>
>andrew
>
>
>Mark Miller wrote:
>
>
>
>>Thanks, that simplifies things.
>>
>>But I ran into the following problem:
>>
>>$ make
>>dlltool --export-all --output-def filesize.def filesize.o
>>dllwrap -o filesize.dll --def filesize.def filesize.o
>>c:/Progra~1/PostgreSQL/8.0/lib/pgxs/src/makefiles/../../src/utils/dllinit.
o
>>-L/usr/local/pgsql/bin -lpostgres
>>c:\MinGW\bin\..\lib\gcc-lib\mingw32\3.2.3\..\..\..\..\mingw32\bin\ld.exe:
>>cannot find -lpostgres
>>c:\MinGW\bin\dllwrap.exe: c:\MinGW\bin\gcc exited with status 1
>>make: *** [filesize.dll] Error 1
>>
>>
>>Here's the makefile (located in /home/usr/dev (windows path is:
>>C:\MSYS\1.0\home\mark\dev)  ***:
>>
>>
>>MODULES = filesize
>>
>>PGXS := $(shell pg_config --pgxs)
>>include $(PGXS)
>>
>>*** ( postgresql is installed in C:\Progra~1\postgresql\8.0\)
>>
>>It looks like "-L/usr/local/pgsql/bin -lpostgres" is looking in the wrong
>>directory. I reinstalled making sure I used the --prefix parameter for
>>configure, but it's still looking in the wrong place. How do I point it to
>>the right "bin" directory?.
>>
>>Again, I did a windows install followed by using MinGW and MSYS to install
>>the headers needed for development. (The reason for doing separate
installs
>>was so I could install PostgreSQL as a service and make sure the proper
>>accounts were set up).
>>
>>Thanks again for your help,
>>Mark
>>
>>
>>-----Original Message-----
>>From: Andrew Dunstan [mailto:andrew@dunslane.net]
>>Sent: Monday, April 25, 2005 2:05 PM
>>To: mark@maxpreps.com
>>Cc: pgsql-hackers-win32@postgresql.org
>>Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
>>compiling extension functions
>>
>>
>>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
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 9: the planner will ignore your desire to choose an index scan if your
>      joining column's datatypes do not match
>
>
>


Re: "undefined reference" error when compiling

От
Andrew Dunstan
Дата:
It seems to be looking for libpostgres.a, which is created as part of a
standard build ... which takes me back to my earlier suggestion of doing
a full configure/make to give yourself the right enviroment to build
extensions.

cheers

andrew


Mark Miller wrote:

>Thanks,
>
>I have to say that it is worlds different than what I'm used to. But it's
>never stopped me before ;). I did find why it was pointing to the wrong dir,
>the pgxs/src/Makefile.global had the following line in it:
>
>prefix := /usr/local/pgsql
>
>Which likely has something to do with the build, since I did a windows
>install the default might not get set. I did set prefix when I ran configure
>and make install, but since I only installed includes it make sense that the
>file didn't get updated. But sadly, even though I found this and the output
>has changed (see below), it did not fix the problem. It still fails with the
>following error:
>
>cannot find -lpostgres
>
>Which brings us back to your suggestion of a clean build from scratch. It
>seems the only solution unless I want to walk thru every possible place
>where I'll run into these problems.
>
>What is "-lpostgres" anyway?
>
>--------------------- make output --------------------------------------
>
>$ make
>dlltool --export-all --output-def filesize.def filesize.o
>dllwrap -o filesize.dll --def filesize.def filesize.o
>c:/Progra~1/PostgreSQL/8.0/lib/pgxs/src/makefiles/../../src/utils/dllinit.o
>-L/c/progra~1/postgresql/8.0/bin -lpostgres
>c:\MinGW\bin\..\lib\gcc-lib\mingw32\3.2.3\..\..\..\..\mingw32\bin\ld.exe:
>cannot find -lpostgres
>c:\MinGW\bin\dllwrap.exe: c:\MinGW\bin\gcc exited with status 1
>make: *** [filesize.dll] Error 1
>
>
>
>
>

Re: "undefined reference" error when compiling extension functions

От
"Mark Miller"
Дата:
I just finished doing a clean build with the same result. I have installed
everything to the defaults

(uninstall previously installed windows installation)
$ Configure --without-zlib
--with-includes=~/postgresql-8.0.2/include/port/win32/*
$ make install

When I try to complile the source for the extension after this I get the
same message as before. I'm going to hang up my hat for the day and give it
a run again tomorrow. Then I'll try and find out where libpostgres.a is and
see if I can add it to the search path with LDFLAGS and -I specified. Let me
know if you think of anything else.

I appreciate all the help. I'm not used to so many people being so willing
to help. Usually when I post a problem I'm having on "other" lists I get
either useless answers or none at all.

Thanks again,

Mark

-----Original Message-----
From: Andrew Dunstan [mailto:andrew@dunslane.net]
Sent: Monday, April 25, 2005 5:20 PM
To: mark@maxpreps.com
Cc: pgsql-hackers-win32@postgresql.org
Subject: Re: [pgsql-hackers-win32] "undefined reference" error when
compiling extension functions


It seems to be looking for libpostgres.a, which is created as part of a
standard build ... which takes me back to my earlier suggestion of doing
a full configure/make to give yourself the right enviroment to build
extensions.

cheers

andrew


Mark Miller wrote:

>Thanks,
>
>I have to say that it is worlds different than what I'm used to. But it's
>never stopped me before ;). I did find why it was pointing to the wrong
dir,
>the pgxs/src/Makefile.global had the following line in it:
>
>prefix := /usr/local/pgsql
>
>Which likely has something to do with the build, since I did a windows
>install the default might not get set. I did set prefix when I ran
configure
>and make install, but since I only installed includes it make sense that
the
>file didn't get updated. But sadly, even though I found this and the output
>has changed (see below), it did not fix the problem. It still fails with
the
>following error:
>
>cannot find -lpostgres
>
>Which brings us back to your suggestion of a clean build from scratch. It
>seems the only solution unless I want to walk thru every possible place
>where I'll run into these problems.
>
>What is "-lpostgres" anyway?
>
>--------------------- make output --------------------------------------
>
>$ make
>dlltool --export-all --output-def filesize.def filesize.o
>dllwrap -o filesize.dll --def filesize.def filesize.o
>c:/Progra~1/PostgreSQL/8.0/lib/pgxs/src/makefiles/../../src/utils/dllinit.o
>-L/c/progra~1/postgresql/8.0/bin -lpostgres
>c:\MinGW\bin\..\lib\gcc-lib\mingw32\3.2.3\..\..\..\..\mingw32\bin\ld.exe:
>cannot find -lpostgres
>c:\MinGW\bin\dllwrap.exe: c:\MinGW\bin\gcc exited with status 1
>make: *** [filesize.dll] Error 1
>
>
>
>
>