1

The Issue

I'm trying to use lipqxx to interact with a PostgreSQL database. However, despite #include-ing pqxx, its namespace doesn't seem to be recognized and as such no pqxx functions are recognized. (simplified code with annotations of issue below:)

#include <iostream>
#include <pqxx/pqxx> // no errors indicated in Visual Studio (VS) here. 

void Classname::operator() (){ // functor method implementation
    using namespace std;
    using namespace pqxx; // **red underline in Visual Studio indicating "name must be a namespace"**
    try{
        connection C("String text"); // **red underline in VS: "identifier 'connection' is undefined"**
    }catch(const std::exception &e) {
        cerr << e.what() << std::endl;
    }
}

What I've done

  1. Downloaded CMake (for Windows) and libpqxx.
  2. Built lipqxx using CMake following instructions in (this video: https://www.youtube.com/watch?v=LxHV-KNEG3k). This produced a folder called "libpqxx" which has "bin", "include", "lib" and "share" sub-folders.
  3. Followed instructions (on this thread: How to add additional libraries to Visual Studio project?) to add the built libraries to my VS project

Conclusion

I'd like the pqxx namespace to be properly recognized and am not sure where to go from here. I've been stuck on this for quite a while and would really appreciate some clear guiding steps on how to fix this.

Many thanks

2 Answers2

0

I've finally found a solution to this by adding the folder called "include" (located within the "libpqxx" directory that was produced by the CMake process) to the Visual Studio Project using

Project -> Properties -> C/C++ -> General -> Additional Include Directories -> edit (drop-down option) -> New Line (icon) -> browse to and select the "include" folder -> OK

The #include error disappeared but I then received the following errors (snippet below) after trying to compile my program:

1>D:\Software\libpqxx\libpqxx\include\pqxx\internal\encoding_group.hxx(14,1): error C2429: language feature 'nested-namespace-definition' requires compiler flag '/std:c++17'

I solved these by setting the Visual Studio C++ Language property of the project to C++ 17 via:

Project -> Properties -> C/C++ -> Language -> C++ Language Standard -> (select drop-down option) ISO C++ 17 Standard (/std:c++17)


EDIT: (Cleanest Solution by Far)

The cleanest solution is to use Microsoft's vcpkg package installer because it very simply adds libpqxx to Visual Studio itself, and not to individual projects on a per-project basis.

As explained via the instructions on the link:

  1. Install vcpkg by running the below commands in cmd from a folder called C:\src (to avoid issues. Create folder if necessary):

git clone https://github.com/microsoft/vcpkg

.\vcpkg\bootstrap-vcpkg.bat

  1. Install libpqxx by entering in cmd from the same folder:

.\vcpkg\vcpkg install libpqxx:x64-windows

.\vcpkg\vcpkg integrate install

.\vcpkg\vcpkg install libpqxx

  1. All done!
0

I had the same problem after running:

vcpkg install libqpxx:x64-windows

Running (as administrator)

vcpkg integrate install

solved the issue for me

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • 1
    Could you correct your answer so I can select it as the best one. I had tried vcpkg to no avail but they appeared to update the bugs! now it works seamlessly but your instructions are incorrect. They should be: "vcpkg install libpqxx:x64-windows" and "vcpkg integrate install" – Andikan Otung Jan 16 '21 at 16:45