'undefined reference' error with Postgres library

'undefined reference' error with Postgres library

Post by Dennis Dun » Wed, 28 Jul 1999 04:00:00



Hi.

I am trying to program a front end to the Postgres database system but
I am having a problem getting the code to link correctly.  When I
compile to the program with

gcc -o sql -lpq sql.c

I get this error

/usr/lib/libpq.so: undefined reference to 'crypt'
collect2: ld returned 1 exit status

Here is some version info:

Red Hat 6.0 distribution of Linux.
Postgres 6.4.x from the Red Hat distribution CD

Here is sql.c , all I am trying to do at this point is to make a
connection to the database and then close the connection:

#include <stdio.h>
#include <pgsql/libpq-fe.h>

int main(int argc, char *argv[])
{
    PGconn conn;

    conn = PQconnectdb("");
    PQfinish(conn);

Quote:}

I read the crypt(3) manpage and saw that crpyt() was defined in unistd.h
so I added  #include <unistd.h> to the source code but this did not
correct my problem.

I read the PostgreSQL Programmer's Guide but it make no mention of any
other required header files.

I figure that because I'm very new at this, my error probably isn't some
obscure bug but rather it is my own ignorance of how to use the tools.

Thanks.

--dennis

 
 
 

'undefined reference' error with Postgres library

Post by J.H.M. Dassen (R » Wed, 28 Jul 1999 04:00:00



>gcc -o sql -lpq sql.c
>I read the crypt(3) manpage and saw that crpyt() was defined in unistd.h
>so I added  #include <unistd.h> to the source code but this did not
>correct my problem.

It only tells the compiler about crypt()'s prototype; it does not cause the
linker to link in the relevant library automagically.

crypt() is in libcrypt, so you need to tell the linker to link against that:
gcc -o sql sql.c -lpq -lcrypt

HTH,
Ray
--
*space, a final frontier. These are the voyages of my messages,
on a lightspeed mission to explore strange new systems and to boldly go
where no data has gone before.

 
 
 

'undefined reference' error with Postgres library

Post by Dennis Dun » Thu, 29 Jul 1999 04:00:00


Quote:> It only tells the compiler about crypt()'s prototype; it does not cause the
> linker to link in the relevant library automagically.

> crypt() is in libcrypt, so you need to tell the linker to link against that:
> gcc -o sql sql.c -lpq -lcrypt

Thanks!

How could I have found out about the crypt library?  Was that in the man pages
somewhere or in unistd.h?  Is this just one of the standard libraries I need to
know about?

One question answered, one  million to go....

Thanks again.

--dennis

 
 
 

'undefined reference' error with Postgres library

Post by David Konerdi » Thu, 29 Jul 1999 04:00:00



>> It only tells the compiler about crypt()'s prototype; it does not cause the
>> linker to link in the relevant library automagically.

>> crypt() is in libcrypt, so you need to tell the linker to link against that:
>> gcc -o sql sql.c -lpq -lcrypt

>Thanks!

>How could I have found out about the crypt library?  Was that in the man pages
>somewhere or in unistd.h?  Is this just one of the standard libraries I need to
>know about?

Actually, after you gain a bit of experience you can usually guess which library
a symbol is defined in.  However, in the short term, you can use the following trick:

% nm -A -l /usr/lib/*.a |grep "[TWtw] crypt"

nm is a beautiful little tool which prints out the symbols defined in an executable
(or object file or library).  See the info pages for binutils (should be installed
if you installed binutils on your box) to understand what the various symbols "T" and "W"
mean.  But basically this searches all the staticlibraries in /usr/lib for the symbol
crypt, and prints out which files contain it.  

I do this *all the time*...

dave

 
 
 

'undefined reference' error with Postgres library

Post by J.H.M. Dassen (R » Thu, 29 Jul 1999 04:00:00



>How could I have found out about the crypt library?

In the FAQ that ships with the GNU C library.

HTH,
Ray
--
*space, a final frontier. These are the voyages of my messages,
on a lightspeed mission to explore strange new systems and to boldly go
where no data has gone before.

 
 
 

'undefined reference' error with Postgres library

Post by J.H.M. Dassen (R » Thu, 29 Jul 1999 04:00:00



>% nm -A -l /usr/lib/*.a |grep "[TWtw] crypt"

I'd go for
        nm --dynamic --defined-only --demangle --print-file-name \
                /usr/X11R6/lib/lib*so* /usr/lib/lib*so* /lib/lib*so* | \
                grep 'crypt'
myself; at least with my distribution, static libraries can be in separate
"debug" packages rather than part of the regular development packages.

Ray
--
*space, a final frontier. These are the voyages of my messages,
on a lightspeed mission to explore strange new systems and to boldly go
where no data has gone before.

 
 
 

'undefined reference' error with Postgres library

Post by Dennis Dun » Fri, 30 Jul 1999 04:00:00


Quote:> % nm -A -l /usr/lib/*.a |grep "[TWtw] crypt"

Thanks, look like this might become an alias on my box...

--dennis

 
 
 

'undefined reference' error with Postgres library

Post by ghislain.. » Fri, 30 Jul 1999 04:00:00


Hi all,

Would anyone know how to enable multi threading on gcc.  I can compile
my libraries fine without the -mthreads option and create an executable
but the program crashes when attempting to create a 2nd thread.  My
library compilation string looks like this with the -mthreads included
(note the error at the end which causes the compilation to stop).

g++ -fPIC -mthreads -DRW_COMPILE_INSTANTIATE -DUNIX -DMMS3_1
-DGNUCOMPILATION -D__need_timeval -DPTHREADS -DRW_NO_STL  -I.
-I../include -I/home/garsenau/mms/code/Linux.2.2.9-19mdk.unknown/include
-I/devl/mms/mms3.1/code/Linux.2.2.9-19mdk.unknown/include
-I/usr/include/g++
-I/devl/mms/mms3.1/3rd-party/Linux.2.2.9-19mdk/include/ACE-BETA
-I/devl/mms/mms3.1/3rd-party/Linux.2.2.9-19mdk/rogueV7    -c -o
obj/Linux.2.2.9-19mdk.unknown/gen.o gen.cpp
cc1plus: Invalid option `threads'?
make[2]: *** [obj/Linux.2.2.9-19mdk.unknown/gen.o] Error 1
make[2]: Leaving directory `/home/garsenau/mms/code/gen/src'
make[1]: *** [install_pre_dir] Error 2
make[1]: Leaving directory `/home/garsenau/mms/code/gen'
make: *** [install_pre_dir] Error 2

Anyhow, if anybody has any ideas I'd appreciate them.  Thanks a lot.

ghislain

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 
 
 

'undefined reference' error with Postgres library

Post by Graffit » Fri, 30 Jul 1999 04:00:00



>Hi all,

>Would anyone know how to enable multi threading on gcc.  I can compile
>my libraries fine without the -mthreads option and create an executable
>but the program crashes when attempting to create a 2nd thread.  My
>library compilation string looks like this with the -mthreads included
>(note the error at the end which causes the compilation to stop).

`-mthreads'
     Support "AIX Threads".  Link an application written to use
     "pthreads" with special libraries and startup code to enable the
     application to run.

In the case of Linux, just try -lpthread.

-- DN

 
 
 

'undefined reference' error with Postgres library

Post by Pierre Ficheu » Sat, 07 Aug 1999 04:00:00



> In the case of Linux, just try -lpthread.

        Additionnaly, you should compile with -D_REENTRANT . Check-out
linuxthreads FAQ on

                http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html

        regards,

Quote:

> -- DN

--

                                         http://www.alienor.fr/~pierre
Mieux vaut jeuner avec les aigles que picorer avec les poules