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.

1
  • 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). You can try adding -I/usr/local/include to the compile command to be sure it looks there. Commented Dec 9, 2014 at 16:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Although that should be in the default search path; if this helps, then something very strange has happened.
@MikeSeymour what types of things may have gone wrong? sorry, i'm too green to this type of programming to know... thanks.
@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. But using this hack will be much easier than trying to fix it.
@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: pqxx.org/development/libpqxx. Notice the comments under the "Build your libpqxx program" section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.