0

Background

I'm running linux... and I'm trying to write a basic little c++ program that connects to a postgresql database.

I'm trying to follow this article http://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm

Problem

I've been able to compile the library... and I can see now that I have the following folder on my computer /usr/local/include/pqxx

But when i try to write some basic code and compile it, I get the following error:

devbox2:/var/abus# g++ testdb.cpp -lpqxx -lpq
testdb.cpp:2:22: fatal error: pqxx/pqxx: No such file or directory
 #include <pqxx/pqxx> 
                      ^
compilation terminated.

Source Code

Here's what the code looks like:

  1 #include <iostream>
  2 #include <pqxx/pqxx>
  3 
  4 using namespace std;
  5 using namespace pqxx;
  6 
  7 int main(int argc, char* argv[])
  8 {
  9    try{
 10       connection C("dbname=testdestination user=testuser password=testpassword \
 11       hostaddr=127.0.0.1 port=5432");
 12       if (C.is_open()) {
 13          cout << "Opened database successfully: " << C.dbname() << endl;
 14       } else {
 15          cout << "Can't open database" << endl;
 16          return 1;
 17       }
 18       C.disconnect ();
 19    }catch (const std::exception &e){
 20       cerr << e.what() << std::endl;
 21       return 1;
 22    }
 23 }

What I've tried so far:

I've been poking around the /usr/local/include/pqxx folder and I can see that there is a file called pqxx... but it doesn't have any extension on it.

Here's a snippet from the ls -lah command for that folder:

-rw-r--r--    1 root     root         637 Dec  8 21:42 pipeline
-rw-r--r--    1 root     root        7.5K Dec  8 21:42 pipeline.hxx
-rw-r--r--    1 root     root        1.1K Dec  8 21:42 pqxx
-rw-r--r--    1 root     root         728 Dec  8 21:42 prepared_statement
-rw-r--r--    1 root     root        8.2K Dec  8 21:42 prepared_statement.hxx

I've also made sure that my PATH includes the /usr/local/include/pqxx folder. This is what my PATH looks like:

PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/gcc:/usr/local/include/pqxx:/usr/local/include'

I'm not sure what else I should check. Any suggestions would be appreciated. Thanks.

dot
  • 14,928
  • 41
  • 110
  • 218
  • Changing `PATH` won't make a difference; that's used to find programs, not headers. `cpp -v` should tell you the search path - check that it includes `/usr/local/include` [(it should)](https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html). You can try adding `-I/usr/local/include` to the compile command to be sure it looks there. – Mike Seymour Dec 09 '14 at 16:42

1 Answers1

2

To find the include files, you must add an -I option, e.g.

g++ -I/usr/local/include testdb.cpp -lpqxx -lpq

Adding directories to PATH doesn't help here, PATH is for locating executables from the shell.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Although that should be in the default search path; if this helps, then something very strange has happened. – Mike Seymour Dec 09 '14 at 16:46
  • @MikeSeymour what types of things may have gone wrong? sorry, i'm too green to this type of programming to know... thanks. – dot Dec 09 '14 at 16:48
  • @dot: I've no idea what might have gone wrong. Your compiler's configuration is messed up in some way, so isn't looking in a directory that it [should look in](https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html). But using this hack will be much easier than trying to fix it. – Mike Seymour Dec 09 '14 at 16:53
  • @MikeSeymour, ok no problem. For what it's worth, the manual has comments / suggestions to do the same thing. I must have missed that before: http://pqxx.org/development/libpqxx/. Notice the comments under the "Build your libpqxx program" section. – dot Dec 09 '14 at 18:11