user_0
user_0

Reputation: 3363

How to solve this compiling error with pqxx under code blocks

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:

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

EDIT: As requested the 2 path:

Upvotes: 2

Views: 2917

Answers (3)

Mark
Mark

Reputation: 39

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.

Upvotes: 0

Mike Kinghan
Mike Kinghan

Reputation: 61462

-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

Upvotes: 3

Hector Villarreal
Hector Villarreal

Reputation: 822

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

Upvotes: 0

Related Questions