简体   繁体   English

使用pqxx的简单C ++程序(postgres)

[英]Simple C++ program using pqxx (postgres)

I'm trying a very basic C++ program using Code::Blocks. 我正在使用Code :: Blocks尝试一个非常基本的C ++程序。 I'm on Ubuntu 12.04 and installed pqxx from the software manager. 我在Ubuntu 12.04上并从软件管理器安装了pqxx。 Here's the code. 这是代码。

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

using namespace std;
int main()
{

    pqxx::connection MyConn ("dbname=dbESM user=postgres");


    cout << "Hello world!" << endl;

    return 0;
}

But I get the following error on hitting F9 to compile and run: 但是在按F9编译并运行时出现以下错误:

/usr/include/pqxx/connection.hxx|87|undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)' /usr/include/pqxx/connection.hxx|87|未定义对`pqxx :: connectionpolicy :: connectionpolicy(std :: basic_string,std :: allocator> const&)'的引用

The above message is from the file connection.hxx and the line highlighted is this: 以上消息来自文件connection.hxx,突出显示的行是:

  explicit connect_direct(const PGSTD::string &opts) : connectionpolicy(opts) {}

The connection.hxx file is not mine - I think it's part of pqxx. connection.hxx文件不是我的 - 我认为它是pqxx的一部分。

I'm pretty new to this platform so I'm avoiding the terminal to compile code. 我对这个平台很陌生,所以我要避免终端编译代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

You need to add the reference to the libpqxx library to the project. 您需要将libpqxx库的引用添加到项目中。

Inside Code::blocks, when the project is open, locate Project in the menus, then follow Build options , then open the tab called Linker settings , then hit Add , then enter pqxx . 在Code :: blocks中,当项目打开时,在菜单中找到Project ,然后按Build options ,然后打开名为Linker settings的选项卡,然后点击Add ,然后输入pqxx

If you were using the libpq C library instead, the procedure would be identical except the name would be pq . 如果您使用的是libpq C库,则该过程将完全相同,但名称将为pq

You need to link against the according library, just #including the header files isn't enough. 你需要链接相应的库,只是#include头文件是不够的。 If available, you could use pkg-config to determine the according libraries. 如果可用,您可以使用pkg-config来确定相应的库。 Further, what IDE are you using? 此外,您使用的是什么IDE? Without that, the "on hitting F9" reference is useless. 没有它,“打击F9”参考是没用的。 Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening. 此外,在命令行上编译它可能更容易,因为它更清楚到底发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM