如何用代码块下的pqxx解决这个编译错误

How to solve this compiling error with pqxx under code blocks

在过去使用 libpq-fe.h 之后,对于一个新项目,我开始使用 pqxx。

因此,在我的代码中包括:

#include <pqxx/pqxx>

然后我编译。一切都很好。
当我声明时:

pqxx::connection p_con;

然后我编译,我有错误:

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()'

google 上的搜索表明这不是库问题。
事实上:一个非常相似的问题,同样的错误,已经在这里解决了:Problem compiling program with pqxx

我在code::blocks中不知道如何解决它。有什么建议吗?

软件版本:

我对使用 code::blocks 比较陌生,所以我可能遗漏了一些东西 :-/

编辑:根据要求 2 路径:

你试过这种方法吗?

尝试声明与唯一指针的连接:

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

然后您可以将其传递给函数,例如:

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

-lpq,像所有 -l<libname> 选项一样,是一个 linker 选项,不是 一个编译器选项。通过将其放入编译器设置 -> 其他选项 你说你已经生成了一个编译命令行:

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

因为这只是一个 compile 命令,不涉及 linking 并且 -lpq 被忽略, 在您的 link 命令行中 - 您没有向我们展示 - 没有 -lpq 选项 会出现。

编译器设置中取出-lpq -> 其他选项并放入-lpqxx-lpq链接器设置 -> 其他选项。然后 -lpqxx -lpq 将传递给 linker,因为他们需要。

如果linker抱怨找不到-lpqxx,那么你需要安装 libpqxx

我遇到了从 vim 切换到 Code::Blocks 时 OP 描述的完全相同的错误。

我通过将 "pqxx" 放在左侧窗格 ("Link libraries") 选项卡 "Linker settings" 的“Settings/Compiler 和调试器设置”下解决了这个问题。

在那之后,我的程序(使用 pqxx 库)编译没有错误。

我在 Linux Mint 13 Maya 上使用 Code::Blocks 10.05。