2

After using libpq-fe.h in the past, for a new project I'm starting use pqxx.

So, in the code I include:

#include <pqxx/pqxx>

And I compile. Everything fine.
When I declare:

pqxx::connection p_con;

And I compile, I have errors:

obj/Debug/src/dbfunc.o: In function `pqxx::connect_direct::connect_direct(std::string const&)':
/usr/include/pqxx/connection.hxx:87: undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::string const&)'
/usr/include/pqxx/connection.hxx:87: undefined reference to `vtable for pqxx::connect_direct'
obj/Debug/src/dbfunc.o: In function `pqxx::connect_direct::~connect_direct()':
/usr/include/pqxx/connection.hxx:84: undefined reference to `vtable for pqxx::connect_direct'
/usr/include/pqxx/connection.hxx:84: undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'
obj/Debug/src/dbfunc.o: In function `pqxx::basic_connection<pqxx::connect_direct>::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()'
obj/Debug/src/dbfunc.o: In function `pqxx::basic_connection<pqxx::connect_direct>::~basic_connection()':
/usr/include/pqxx/basic_connection.hxx:78: undefined reference to `pqxx::connection_base::close()'

A search on google indicates that this is not a library problem.
Infact: a very similar problem, same error, was already solved here: Problem compiling program with pqxx

I don't get how to solve it in code::blocks. Any suggestion?

Software versions:

  • Code::Blocks 13.12
  • Os: Debian 8.2
  • Libpqxx: libpqxx-4.0
  • Compiler used: gcc
  • gcc --version: gcc (Debian 4.9.2-10) 4.9.2

I am relatively new to using code::blocks, so probably I'm missing something :-/

EDIT: As requested the 2 path:

  • /usr/lib/x86_64-linux-gnu/libpq.so
  • /usr/lib/x86_64-linux-gnu/libpq.a
Community
  • 1
  • 1
user_0
  • 3,173
  • 20
  • 33
  • you're compiling with `-lpq` right? – Azad Nov 02 '15 at 16:10
  • No, i didn't. I added it in project-> build options -> Compiler settings -> other options But nothing changes. Well, the command change: "g++ -std=c++11 -Wall -fexceptions -g -lpq -Iinclude -c /myfile.cpp -o /myfile.o" – user_0 Nov 02 '15 at 16:20
  • first try Mike Kinghan suggestion if unsuccessful search for a file named `libpq.so` or `libpq.a` on your system and post its path – Azad Nov 02 '15 at 18:28
  • Ok, I have both. Paths inserted in question. – user_0 Nov 02 '15 at 19:28
  • now add `-L/usr/lib/x86_64-linux-gnu/ -lpq` in linker settings – Azad Nov 02 '15 at 19:33
  • In meantime Mike found what I made wrong. Thank you anyway. – user_0 Nov 02 '15 at 20:16

3 Answers3

3

-lpq, like all -l<libname> options, is a linker option, not a compiler option. By putting it in Compiler settings -> Other options you say you have generated a compile commandline:

g++ -std=c++11 -Wall -fexceptions -g -lpq -Iinclude -c /myfile.cpp -o /myfile.o

Since this is just a compile command no linking is involved and -lpq is ignored, while in your link commandline - which you haven't shown us - no -lpq option will appear.

Take -lpq out of Compiler settings -> Other options and put -lpqxx and -lpq in Linker settings -> Other options. Then -lpqxx -lpq will be passed to to linker, as they need to be.

If the linker complains that -lpqxx is not found, then you need to install libpqxx

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
0

Have you tried this approach?

try declaring the connection to a unique pointer:

std::unique_ptr<pqxx::connection> connection;

then you can pass it in to a function like:

bool do_something(std::unique_ptr<pqxx::connection> & connection) {

    connection.reset(new pqxx::connection("myconnectionstring"));

    std::string sqlstr ="select * from some table;";

    pqxx::result r;
    try {
        pqxx::work query(*connection, "");
        r = query.exec(sqlstr, "cache batch types");

    } catch (const std::exception & e) {
        return false;
    }

    // handle results...

    return true;
}
Hector Villarreal
  • 822
  • 10
  • 20
  • Still get: /usr/include/pqxx/connection.hxx|84|undefined reference to `vtable for pqxx::connect_direct'| AND /usr/include/pqxx/connection.hxx|84|undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'| – user_0 Nov 02 '15 at 16:23
0

I had the exact same error the OP described when switching from vim to Code::Blocks.

I solved it by putting "pqxx" under 'Settings/Compiler and debugger settings" on the tab "Linker settings" in the left-hand pane ("Link libraries").

After that, my program (which uses the pqxx lib) compiled without errors.

I'm using Code::Blocks 10.05 on Linux Mint 13 Maya.

Mark
  • 39
  • 4