2010-12-03 98 views
2

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

#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++一段时间了,所以我没有像以前那样弄清楚事情出了什么地方。

回答

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

你会注意到Netbeans没有像你那样向链接器命令添加-L和-l选项。这会导致'未定义的引用'错误 - 它不会将您的程序链接到pqxx库。

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

+0

呃 - 非常感谢。我知道这会是一件愚蠢的事情。 – thetaiko 2010-12-03 19:17:37

0

我也是Netbeans的新手,这张图片也可以帮助其他人。

enter image description here