1

I'm trying a very basic C++ program using Code::Blocks. I'm on Ubuntu 12.04 and installed pqxx from the software manager. 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:

/usr/include/pqxx/connection.hxx|87|undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)'

The above message is from the file connection.hxx and the line highlighted is this:

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

The connection.hxx file is not mine - I think it's part of pqxx.

I'm pretty new to this platform so I'm avoiding the terminal to compile code. Any help would be greatly appreciated.

3
  • FWIW I've been seeing reports that suggest that libpqxx isn't particularly maintained. Personally I'd use regular libpq and libpqtypes. Commented Jun 30, 2013 at 12:41
  • @CraigRinger Thanks for the tip. I'm new to this platform and I got the idea that pqxx is the default thing to use. Furthermore, I thought the libpq was more towards old C-style code. I'm no guru here but I'm just sharing my thoughts. Eventually, I'm looking at building wxwidgets apps using Code::Clocks as the IDE. So I'm thinking if using libpq will be convenient to work with when it comes to things like grids on a form. Please advice. Thanks! Commented Jun 30, 2013 at 14:20
  • libpq is indeed a pure C library. That means a bit more work is required for resource management and error handling since there are no exceptions, there's no RAII, and there are no automatic dtors. OTOH, you get to use libpqtypes which can make a lot of things easier. Unsure if it works with libpqxx. Commented Jun 30, 2013 at 23:35

2 Answers 2

4

You need to add the reference to the libpqxx library to the project.

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.

If you were using the libpq C library instead, the procedure would be identical except the name would be pq.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry if this sounds rather stupid. But why don't we specify the path to libpqxx? Simply putting libpqxx works. How is that?
@itsols: because it resides in a standard location (/usr/lib) where the linker looks by default
0

You need to link against the according library, just #including the header files isn't enough. If available, you could use pkg-config to determine the according libraries. Further, what IDE are you using? Without that, the "on hitting F9" reference is useless. Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening.

3 Comments

Thanks for your time. I'm using Code::Blocks as the IDE. Please elaborate on what you mean by linking against the according libraries... I was just following the docs on the pqxx site. I thought Code::Blocks takes care of most linking...
As Daniel explained above, C::B doesn't take care of the linking automatically. On Debian, libpqxx3-dev comes with pkg-config integration, so the intended settings for compiling and linking can be retrieve via pkg-config libpqxx --cflags and pkg-config libpqxx --libs. I would use these instead of hardcoding things, because this will keep working even if you install a different version locally, where you then need /usr/local/.. paths. If it works for you now, good, but keep this in mind for the future. Good luck!
Thanks for that tip. I might even consider moving towards debian :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.