🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.linux
Article #332

Re: help: Undefined reference to '...'

#332
From: Jim Cochrane
Date: Sun, 27 May 2001 19:54
221 lines
8668 bytes
I think you need to read the gcc man page, or info gcc.  On your command
line:

>gcc -O3 -o testlibpq testlibpq.c -I/usr/include/pgsql -Llibpq 2>testlibpq.txt

the -L option takes a directory and (path).  If you want to link with
libpq.a, the option is -lpq.  That file will need to be in a standard
directory such as /usr/lib; if it's not, read up on the LD_LIBRARY_PATH
env. variable.  Here's the relevant section on -l in the gcc man page:

       -llibrary
	      Use the library named library when linking.

	      The  linker searches a standard list of directories
	      for the library, which is	 actually  a  file  named
	      `liblibrary.a'.	The linker then uses this file as
	      if it had been specified precisely by name.

	      The directories searched include	several	 standard
	      system  directories  plus any that you specify with
	      `-L'.

	      Normally the  files  found  this	way  are  library
	      files--archive   files  whose  members  are  object
	      files.  The linker handles an archive file by scan
	      ning  through  it	 for members which define symbols
	      that have so far been referenced but  not	 defined.
	      However,	if  the	 linker	 finds an ordinary object
	      file rather than a  library,  the	 object	 file  is
	      linked  in  the usual fashion.  The only difference
	      between using an `-l' option and specifying a  file
	      name  is that `-l' surrounds library with `lib' and
	      `.a' and searches several directories.


In article <3b10ae18.58847@news.adelaide.on.net>,
David. E. Goble <goble@gtech> wrote:
>On Thu, 24 May 2001 07:52:32 GMT, goble@gtech (David. E. Goble) wrote:
>>
>Hi All;
>
>I have redHat 6.2 (server install)
>I am trying to do some programming.
>
>How that I know how to save the error messages, here is what is
>happening;
>
>/*######## Here is the c file I am trying to compile and run #######*/
>/*
> * testlibpq.c Test the C version of Libpq, the Postgres frontend
> * library.
> *
> *
> */
>#include<stdio.h>
>#include "libpq-fe.h"
>
>void exit_nicely(PGconn *conn)
>{
>    PQfinish(conn);
>    exit(1);
>}
>
>main()
>{
>    char       *pghost,
>               *pgport,
>               *pgoptions,
>               *pgtty;
>    char       *dbName;
>    int         nFields;
>    int         i,
>                j;
>
>    /* FILE *debug; */
>
>    PGconn     *conn;
>    PGresult   *res;
>
>    /*
>     * begin, by setting the parameters for a backend connection if
>the
>     * parameters are null, then the system will try to use reasonable
>     * defaults by looking up environment variables or, failing that,
>     * using hardwired constants
>     */
>    pghost = NULL;              /* host name of the backend server */
>    pgport = NULL;              /* port of the backend server */
>    pgoptions = NULL;           /* special options to start up the
>backend
>                                 * server */
>    pgtty = NULL;               /* debugging tty for the backend
>server */
>    dbName = "template1";
>
>    /* make a connection to the database */
>    conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
>
>    /*
>     * check to see that the backend connection was successfully made
>     */
>    if (PQstatus(conn) == CONNECTION_BAD)
>    	{
>        fprintf(stderr, "Connection to database '%s' failed.\n",
>dbName);
>        fprintf(stderr, "%s", PQerrorMessage(conn));
>        exit_nicely(conn);
>    	}
>
>    /* debug = fopen("/tmp/trace.out","w"); */
>    /* PQtrace(conn, debug);  */
>
>    /* start a transaction block */
>    res = PQexec(conn, "BEGIN");
>    if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
>    	{
>        fprintf(stderr, "BEGIN command failed\n");
>        PQclear(res);
>        exit_nicely(conn);
>    	}
>
>    /*
>     * should PQclear PGresult whenever it is no longer needed to
>avoid
>     * memory leaks
>     */
>    PQclear(res);
>
>    /*
>     * fetch instances from the pg_database, the system catalog of
>     * databases
>     */
>    res = PQexec(conn, "DECLARE mycursor CURSOR FOR select * from
>pg_database");
>    if(!res || PQresultStatus(res) != PGRES_COMMAND_OK)
>    	{
>        fprintf(stderr, "DECLARE CURSOR command failed\n");
>        PQclear(res);
>        exit_nicely(conn);
>    	}
>    PQclear(res);
>    res = PQexec(conn, "FETCH ALL in mycursor");
>    if(!res || PQresultStatus(res) != PGRES_TUPLES_OK)
>    	{
>        fprintf(stderr, "FETCH ALL command didn't return tuples
>properly\n");
>        PQclear(res);
>        exit_nicely(conn);
>    	}
>
>    /* first, print out the attribute names */
>    nFields = PQnfields(res);
>    for (i = 0; i<nFields; i++)
>        printf("%-15s", PQfname(res, i));
>    printf("\n\n");
>
>    /* next, print out the instances */
>    for (i = 0; i<PQntuples(res); i++)
>    	{
>        for (j = 0; j<nFields; j++)
>            printf("%-15s", PQgetvalue(res, i, j));
>        printf("\n");
>    	}
>    PQclear(res);
>
>    /* close the cursor */
>    res = PQexec(conn, "CLOSE mycursor");
>    PQclear(res);
>
>    /* commit the transaction */
>    res = PQexec(conn, "COMMIT");
>    PQclear(res);
>
>    /* close the connection to the database and cleanup */
>    PQfinish(conn);
>
>    /* fclose(debug); */
>}
>
>/*
>This is how I am trying to do it;
>
>gcc -O3 -o testlibpq testlibpq.c -I/usr/include/pgsql -Llibpq
>2>testlibpq.txt
>
>And here are the results
>
>/tmp/cc2HM1Be.o: In function `exit_nicely':
>/tmp/cc2HM1Be.o(.text+0x8): undefined reference to `PQfinish'
>/tmp/cc2HM1Be.o: In function `main':
>/tmp/cc2HM1Be.o(.text+0x66): undefined reference to `PQsetdbLogin'
>/tmp/cc2HM1Be.o(.text+0x77): undefined reference to `PQstatus'
>/tmp/cc2HM1Be.o(.text+0xa1): undefined reference to `PQerrorMessage'
>/tmp/cc2HM1Be.o(.text+0xd4): undefined reference to `PQexec'
>/tmp/cc2HM1Be.o(.text+0xeb): undefined reference to `PQresultStatus'
>/tmp/cc2HM1Be.o(.text+0x118): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x130): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x141): undefined reference to `PQexec'
>/tmp/cc2HM1Be.o(.text+0x158): undefined reference to `PQresultStatus'
>/tmp/cc2HM1Be.o(.text+0x180): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x198): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x1a9): undefined reference to `PQexec'
>/tmp/cc2HM1Be.o(.text+0x1c0): undefined reference to `PQresultStatus'
>/tmp/cc2HM1Be.o(.text+0x1e8): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x200): undefined reference to `PQnfields'
>/tmp/cc2HM1Be.o(.text+0x229): undefined reference to `PQfname'
>/tmp/cc2HM1Be.o(.text+0x265): undefined reference to `PQntuples'
>/tmp/cc2HM1Be.o(.text+0x29d): undefined reference to `PQgetvalue'
>/tmp/cc2HM1Be.o(.text+0x2d7): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x2e8): undefined reference to `PQexec'
>/tmp/cc2HM1Be.o(.text+0x2f9): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x30a): undefined reference to `PQexec'
>/tmp/cc2HM1Be.o(.text+0x31b): undefined reference to `PQclear'
>/tmp/cc2HM1Be.o(.text+0x327): undefined reference to `PQfinish'
>collect2: ld returned 1 exit status
>*/


-- 
Jim Cochrane
jtc@dimensional.com
Technical Details
Message-ID: <9erm11$t07@flatland.dimensional.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!upp1.onvoy!onvoy.com!news-out.visi.com!hermes.visi.com!wormhole.dimensional.com!flatland.dimensional.com!not-for-mail
References: <3b0bacc3.134395@news.adelaide.on.net> <3b10ae18.58847@news.adelaide.on.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10c696,e7efa684ec24a406
X-Google-Attributes: gid10c696,public
X-Google-Thread: 100c5f,e7efa684ec24a406
X-Google-Attributes: gid100c5f,public
X-Google-Thread: f9b51,e7efa684ec24a406
X-Google-Attributes: gidf9b51,public
X-Google-Thread: 102983,e7efa684ec24a406
X-Google-Attributes: gid102983,public
X-Google-ArrivalTime: 2001-05-27 12:54:18 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!upp1.onvoy!onvoy.com!news-out.visi.com!hermes.visi.com!wormhole.dimensional.com!flatland.dimensional.com!not-for-mail
From: jtc@dimensional.com (Jim Cochrane)
Newsgroups: alt.linux,aus.computers.linux,comp.os.linux.development.apps,comp.os.linux.setup
Subject: Re: help: Undefined reference to '...'
Organization: Dimensional Communications
Lines: 221
Message-ID: <9erm11$t07@flatland.dimensional.com>
References: <3b0bacc3.134395@news.adelaide.on.net> <3b10ae18.58847@news.adelaide.on.net>
Date: 27 May 2001 13:54:09 -0600
NNTP-Posting-Host: 206.124.0.24
X-Trace: wormhole.dimensional.com 990993256 206.124.0.24 (Sun, 27 May 2001 13:54:16 MDT)
NNTP-Posting-Date: Sun, 27 May 2001 13:54:16 MDT