c++ - 无法使用 Netbeans 编译 libpqxx 测试

标签 c++ netbeans g++ libpqxx

我正在尝试编译一个非常简单的程序,它可以连接到数据库并打印数据库名称。我可以在命令行上进行编译,但无法在 Netbeans 中进行编译。我在 Ubuntu 10.04 上使用 Netbeans IDE 6.9.1,并使用使用 apt-get 安装的 g++ 4.4.3 和 libpqxx 2.6.9。代码如下:

#include <iostream>
#include <pqxx/connection>

using namespace std;
using namespace pqxx;

int main(int argc, char *argv[])
{
  try
  {
    // Set up a connection to the backend
    connection C(argv[1]);
    cout << C.dbname() << endl;
  }
  catch (const exception &e)
  {
    // All exceptions thrown by libpqxx are derived from std::exception
    cerr << "Exception: " << e.what() << endl;
    return 2;
  }
  return 0;
}

我可以使用以下命令成功编译:

g++ -g -o example -L/usr/lib/ -lpqxx -lpq dbtest001.cpp

Netbeans 失败:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/dev/work/cpptest'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cpptest
make[2]: Entering directory `/home/dev/work/cpptest'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/cpptest build/Debug/GNU-Linux-x86/main.o  
make[2]: Leaving directory `/home/dev/work/cpptest'
make[1]: Leaving directory `/home/dev/work/cpptest'
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/dev/work/cpptest/main.cpp:13: undefined reference to `pqxx::connection_base::dbname()'
build/Debug/GNU-Linux-x86/main.o: In function `connect_direct':
/usr/include/pqxx/connection.hxx:84: undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/include/pqxx/connection.hxx:84: undefined reference to `vtable for pqxx::connect_direct'
build/Debug/GNU-Linux-x86/main.o: In function `~connect_direct':
/usr/include/pqxx/connection.hxx:82: undefined reference to `vtable for pqxx::connect_direct'
/usr/include/pqxx/connection.hxx:82: undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'
build/Debug/GNU-Linux-x86/main.o: In function `basic_connection':
/usr/include/pqxx/basic_connection.hxx:61: undefined reference to `pqxx::connection_base::connection_base(pqxx::connectionpolicy&)'
/usr/include/pqxx/basic_connection.hxx:62: undefined reference to `pqxx::connection_base::init()'
build/Debug/GNU-Linux-x86/main.o: In function `~basic_connection':
/usr/include/pqxx/basic_connection.hxx:70: undefined reference to `pqxx::connection_base::close()'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cpptest] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 622ms)

任何人都可以深入了解为什么会失败吗?我已经离开 C++有一段时间了,所以我不再像以前那样敏锐地找出哪里出了问题。

最佳答案

g++     -o dist/Debug/GNU-Linux-x86/cpptest build/Debug/GNU-Linux-x86/main.o

您会注意到 Netbeans 没有像您一样将 -L 和 -l 选项添加到链接器命令中。这导致了“ undefined reference ”错误——它没有将您的程序链接到 pqxx 库。

您需要编辑 Netbeans 项目以设置正确的库路径和依赖项。

关于c++ - 无法使用 Netbeans 编译 libpqxx 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4348926/

相关文章:

java - 我想使用 WordNet 查找单词相似度

java - Netbeans 错误 : Could not find or load main class (again)

c++ - 对静态类成员的 undefined reference

c++ - 通过 Visual Studio 运行 GCC

java - 通过 SSH 远程调试 |网 bean

c++ - 复制构造函数调用无限循环,尽管通过引用调用

linux - 遗留系统上新 libstdc++ 中的段错误

c++ - 赛格温 : Installing quantlib in cygwin

c++ - 从 VS2008 迁移到 VS2010 时要记住的事情

c++ - 在自定义类构造函数中设置后,不保留 openCV Mat 中的值