trying to connect to postgresql via c++ program

4.7k views Asked by At

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.

1

There are 1 answers

4
Olaf Dietsche On BEST ANSWER

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.