2

I am trying to run PostgreSQL on my mac. PostgreQL itself works fine and I can create database and table and stuff but when I try to connect to PostgreSQL using C++ with something like:

#include <stdio.h>
#include </Library/PostgreSQL/8.4/include/libpq-fe.h>
#include <string>

int     main() {
PGconn          *conn;
PGresult        *res;
int             rec_count;

conn = PQconnectdb("dbname=ljdata host=localhost user=dataman);

if (PQstatus(conn) == CONNECTION_BAD) {
 puts("We were unable to connect to the database");
exit(0);
} 

res = PQexec(conn, "update people set phonenumber=\'5055559999\' where id=3");

and compile with something like:

g++ -lpq db.cpp -o db

I get the error ld: library not found for -lpq

and if I compile without lpq, I get

Undefined symbols:
  "_PQclear", referenced from:
      _main in ccpjNCAU.o
      _main in ccpjNCAU.o"

I have already included the libpq-fe.h, shouldn't it work? Does anybody know what went wrong?

dandan78
  • 13,328
  • 13
  • 64
  • 78
Confused
  • 23
  • 1
  • 3

3 Answers3

4

g++ can't find the pq library. You have to specify where to look for it, with a capital -L:

g++ -L/path/to/pq/lib -lpq db.cpp -o db

where pq is /path/to/pq/lib/libpq.a (or whatever the extension is)

Here's what you probably want to do:

  1. change the include line to not have the path.

    #include "libpq-fe.h"
    
  2. Add the include path to the commandline

    g++ -I/Library/PostgreSQL/8.4/include db.cpp
    
  3. Build intermediary object files

    g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -o db.o
    
  4. Link it together as a separate step

    g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq
    
  5. Build with debug info using -g

Put it all together, for two separate build steps: compile and link:

g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -g -o db.o
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq -o db
Tim
  • 8,912
  • 3
  • 39
  • 57
  • So if my file is in home, I should just do g++ -L/Library/PostgreSQL/8.4/include/libpq-fe.h? That doesn't work either. – Confused Mar 14 '11 at 22:34
  • -L/path/to/lib/dir not /path/to/header/file. Where is libpq located? – Tim Mar 14 '11 at 22:39
  • libpq-fe.h and libpq-events.h are in Library/PostgreSQL/8.4/include/ but there is one more, "libpq-fs.h" in Library/PostgreSQL/8.4/include/libpq – Confused Mar 14 '11 at 22:46
  • those are header files - they end in `.h`. Where is the actual library file? – Tim Mar 14 '11 at 22:47
  • try looking in `Library/PostgreSQL/8.4/lib` is it there? If so, try compiling with `-L~/Library/PostgreSQL/8.4/lib` – Tim Mar 14 '11 at 22:49
  • Ah now I understand you!! Yeah, they are in Library/PostgreSQL/8.4/lib but still g++ -L~/Library/PostgreSQL/8.4/lib db.cpp -o db doesn't work. Furthermore, it says ld: warning: directory '~/Library/PostgreSQL/8.4/lib/' following -L not found. So, I tried g++ -L/Library/PostgreSQL/8.4/lib db.cpp -o db and it still doesn't work!! – Confused Mar 14 '11 at 22:58
  • You seem to be using `/Library` which is from the root, instead of `~/Library` which would be under your home directory. I've updated my answer with detailed explanations for building your code. See if it works. – Tim Mar 14 '11 at 23:00
  • Thank you so much for the expkanation! However, it still doesn't compile. It now gives ld: warning: in /Library/PostgreSQL/8.4/lib/libpq.dylib, missing required architecture x86_64 in file Undefined symbols: "_PQclear", referenced from: _main in test.o _main in test.o – Confused Mar 14 '11 at 23:29
  • I think it is a version problem. But the one click installer of postgresql for mac os X installs only a 32 bit version of the database, how do I install the 64 bit version? – Confused Mar 14 '11 at 23:34
  • @Confused, here's a resource page about building with pq: http://developer.postgresql.org/pgdocs/postgres/libpq-build.html – Tim Mar 14 '11 at 23:35
  • @Confused, what do you get if you run `pg_config --includedir` and `pg_config --libdir`? – Tim Mar 14 '11 at 23:39
  • I just get /Library/PostgreSQL/8.4/include and /Library/PostgreSQL/8.4/lib – Confused Mar 14 '11 at 23:47
  • @Confused, a little googling confirms that this is a 32vs64 bit problem. Yes, the one-click installer only includes 32-bit. Apparently, PostGres's response is "download the source and build a 64-bit version yourself". Which is almost as rude as "go f*** yourself". You can get [the source here](http://www.postgresql.org/ftp/source/v8.4.6/), and supposedly it's "easy to build". But I haven't tested that. – Tim Mar 14 '11 at 23:51
  • Hmm... everything seems to be the same as what is mentioned in the link you provided and I didn't forget to add -lpq either but I get ld: warning: in /Library/PostgreSQL/8.4/lib/libpq.dylib, missing required architecture x86_64 in file Undefined symbols: "_PQclear", referenced from: _main in test.o _main in test.o – Confused Mar 14 '11 at 23:51
0

Had the same problem, You need to add the path of the library to /etc/ld.so.conf, do it and you'll see. Good luck

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
0

libpq-fe.h is a user library, not a system library, and therefore you should use "..." instead of <...>, like this:

#include "/Library/PostgreSQL/8.4/include/libpq-fe.h"

Take a look at this link. And make sure libpq-fe.h can actually be found by your compiler.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • I think my compiler can find libpq-fe.h. Once I change #include "/Library/PostgreSQL/8.4/include/libpq-fe.h" to #include "Library/PostgreSQL/8.4/include/libpq-fe.h", I get error: ‘PGconn’ was not declared in this scope error: ‘conn’ was not declared in this scope. As PGconn has been defined, doesn't that mean the libpq-fe.h can be found by the compiler? – Confused Mar 14 '11 at 22:31