0

背景

我正在运行 linux ......我正在尝试编写一个连接到 postgresql 数据库的基本小 C++ 程序。

我正在尝试关注这篇文章 http://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm

问题

我已经能够编译库......现在我可以看到我的计算机上有以下文件夹 /usr/local/include/pqxx

但是当我尝试编写一些基本代码并编译它时,我收到以下错误:

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.

源代码

代码如下所示:

  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 }

到目前为止我已经尝试过:

我一直在寻找 /usr/local/include/pqxx 文件夹,我可以看到有一个名为 pqxx 的文件......但它没有任何扩展名。

这是该文件夹的 ls -lah 命令的片段:

-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.

4

1 回答 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.

于 2014-12-09T16:44:11.990 回答