Search code examples
c++netbeansg++libpqxx

Can't compile libpqxx tests using Netbeans


I'm trying to compile a very simple program that makes a connection to a database and prints the database name. I'm able to compile on the command line but am unable to do so in Netbeans. I'm using Netbeans IDE 6.9.1 on Ubuntu 10.04 and am using g++ 4.4.3 and libpqxx 2.6.9 installed using apt-get. Code follows:

#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;
}

I can successfully compile using the following:

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

Netbeans fails with this:

"/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)

Can anyone offer any insight into why this is failing? I've been away from C++ for a while so am not as sharp at working out where things have gone wrong as I used to be.


Solution

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

    You'll notice that Netbeans hasn't added the -L and -l options to the linker command like you did. This is causing the 'undefined reference' errors -- it's not linking your program to the pqxx library.

    You'll need to edit your Netbeans project to set up the proper library path and dependencies.