[英]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.在过去使用 libpq-fe.h 之后,对于一个新项目,我开始使用 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事实上:一个非常相似的问题,同样的错误,已经在这里解决了: Problemcompile program with pqxx
I don't get how to solve it in code::blocks.我不知道如何在 code::blocks 中解决它。 Any suggestion?
有什么建议吗?
Software versions:软件版本:
I am relatively new to using code::blocks, so probably I'm missing something :-/我对使用 code::blocks 比较陌生,所以可能我错过了一些东西:-/
EDIT : As requested the 2 path:编辑:根据要求 2 路径:
-lpq
, like all -l<libname>
options, is a linker option, not a compiler option. -lpq
与所有-l<libname>
选项一样,是链接器选项,而不是编译器选项。 By putting it in Compiler settings -> Other options you say you have generated a compile commandline:通过将它放在Compiler settings -> Other options你说你已经生成了一个编译命令行:
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.由于这只是一个编译命令,不涉及链接并且
-lpq
被忽略,而在您的链接命令行中 - 您没有向我们展示 - 不会出现-lpq
选项。
Take -lpq
out of Compiler settings -> Other options and put -lpqxx
and -lpq
in Linker settings -> Other options .从Compiler settings -> Other options 中取出
-lpq
并将-lpqxx
和-lpq
放在Linker settings -> Other options 中。 Then -lpqxx -lpq
will be passed to to linker, as they need to be.然后
-lpqxx -lpq
将根据需要传递给链接器。
If the linker complains that -lpqxx
is not found, then you need to install libpqxx
如果链接器抱怨
-lpqxx
,那么您需要安装libpqxx
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;
}
I had the exact same error the OP described when switching from vim to Code::Blocks.从 vim 切换到 Code::Blocks 时,我遇到了与 OP 描述的完全相同的错误。
I solved it by putting "pqxx" under 'Settings/Compiler and debugger settings" on the tab "Linker settings" in the left-hand pane ("Link libraries").我通过在左侧窗格(“链接库”)的“链接器设置”选项卡上的“设置/编译器和调试器设置”下放置“pqxx”来解决它。
After that, my program (which uses the pqxx lib) compiled without errors.之后,我的程序(使用 pqxx 库)编译没有错误。
I'm using Code::Blocks 10.05 on Linux Mint 13 Maya.我在 Linux Mint 13 Maya 上使用 Code::Blocks 10.05。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.