user2856064
user2856064

Reputation: 553

Error while compiling libpqxx code example?

I am trying to compile libpqxx code example:

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

int main(int, char *argv[])
{
  pqxx::connection c("dbname=company user=accounting");
  pqxx::work txn(c);

  pqxx::result r = txn.exec(
    "SELECT id "
    "FROM Employee "
    "WHERE name =" + txn.quote(argv[1]));

  if (r.size() != 1)
  {
    std::cerr
      << "Expected 1 employee with name " << argv[1] << ", "
      << "but found " << r.size() << std::endl;
    return 1;
  }

  int employee_id = r[0][0].as<int>();
  std::cout << "Updating employee #" << employee_id << std::endl;

  txn.exec(
    "UPDATE EMPLOYEE "
    "SET salary = salary + 1 "
    "WHERE id = " + txn.quote(employee_id));

  txn.commit();
}

Using command:

c++ add_employee.cxx -lpqxx -lpq

Unfortunately I have got an error:

test2.cpp:(.text+0x162): undefined reference to `pqxx::transaction_base::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test2.cpp:(.text+0x32e): undefined reference to `pqxx::transaction_base::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cchvWstf.o: In function `pqxx::string_traits<int>::null()':
test2.cpp:(.text._ZN4pqxx13string_traitsIiE4nullEv[_ZN4pqxx13string_traitsIiE4nullEv]+0x47): undefined reference to `pqxx::internal::throw_null_conversion(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cchvWstf.o: In function `pqxx::connect_direct::connect_direct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
test2.cpp:(.text._ZN4pqxx14connect_directC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4pqxx14connect_directC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x1f): undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cchvWstf.o: In function `pqxx::transaction<(pqxx::isolation_level)0, (pqxx::readwrite_policy)1>::transaction(pqxx::connection_base&)':
test2.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE]+0xba): undefined reference to `pqxx::dbtransaction::fullname(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test2.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE]+0x190): undefined reference to `pqxx::basic_transaction::basic_transaction(pqxx::connection_base&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, pqxx::readwrite_policy)'

How can I compile it? I have trying to change order of flags, but it doesn't help. Thank you.

Upvotes: 3

Views: 3266

Answers (3)

Nigar Alam
Nigar Alam

Reputation: 7

Please add name namespace pqxx in your code

Upvotes: -1

ZestyMeta
ZestyMeta

Reputation: 131

I've recently encountered this when attempting to compile code with libpqxx-3.1 on Ubuntu 16.04. The same code compiled successfully in Ubuntu 14.04, but spit out linker errors (like yours) in Ubuntu 16.04. The culprit appears to be a change in libstdc++ between gcc 4.8.4 and gcc 5.4.0. Per the "Runtime Library" documentation at https://gcc.gnu.org/gcc-5/changes.html

A Dual ABI is provided by the library. A new ABI is enabled by default. The old ABI is still supported and can be used by defining the macro _GLIBCXX_USE_CXX11_ABI to 0 before including any C++ standard library headers.

To confirm whether this is the issue you're facing, add the following macro to the top of your cpp file and compile.

#define _GLIBCXX_USE_CXX11_ABI 0

If it links successfully, then you've confirmed that your code was compiling with the new implementation of std::string (as gcc5 will do by default), while the library you're linking to uses the old implementation (as gcc4 would do). With the macro introduced, both your code and libpqxx are using the same implementation.

The long term solution is to use libraries built with the same version of gcc that you're using. I'm not sure why Ubuntu 16.04 ships libraries built by an older gcc, it's certainly causing me a headache. You can either stick with the above solution, compile and install your own build of libpqxx, or upgrade to a later version of your Linux distro (hopefully shipping a newer version of the library).

Upvotes: 3

Daniel Costa
Daniel Costa

Reputation: 27

First, check if your libpxx.so is correctly installed in /usr/lib. After that try to use g++ -L/usr/lib/ add_employee.cpp -o add_employee -lpqxx -lpq. This should work and generate a executable called add_employee (the part -o add_employee).

Upvotes: 0

Related Questions