2010-12-03 67 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