Using libpq in VisualStudio (Windows)

This post does not pretend to anything, but was written by me in order to help the same non-programmers like me. Perhaps, if I had read the documentation for this API completely, then this article would not have been needed. However, in my defense, I will say that I did not find a clear instruction for dummies.





I encountered a number of problems when trying to connect libpq in VS 2017: starting with the inconsistency of the documentation for this API with the instructions for connecting, ending with the default of some key points.





In general, in order.





To use this API, you need the following libraries and files:





  • libpq.dll and libpq.lib - the API itself The last mention in the documentation about the possibility of building it for Windows is in the 9th version. At 10+ this is no longer there. Thus, the easiest way is to do this: install postgresql of the required version and take the PostgreSQL \ {version number} \ lib folder from there. Copy where you want and in VS specify in the project properties Configuration Properties> VC ++ Directories> Library Directories . Next, we specify libpq.lib in Configuration Properties> Linker> Input> Additional Dependencies .





  • libpq-fe.h - API header file. Along with all other header files, it is located in the postgresql sources. Download the required version of postgresql from the off site ( https://ftp.postgresql.org/pub/source/v12.0/postgresql-12.0.tar.gz ). Headers are found in postgresql- {version number} \ src \ interfaces \ libpq . We connect this folder in Configuration Properties> VC ++ Directories> Included Directories. Also, I have included these directories here: "\ src \ include" "\ include \ libpq" "\ src \ interfaces" (so to say, just for everyone).





  • libintl-8.dll, libiconv-2.dll - are located in the folder with postgres installed ("PostgreSQL \ {version number} \ bin"). You need to copy it to Windows \ system32 or to the folder with the assembled exe of the program.





  • libcrypto-1_1-x64.dll, libssl-1_1-x64.dll - OpenSSL libraries. Either we put it (choosing during installation, copying the libraries to the system32 folder), or we take these libraries from the already installed openssl (C: \ Program Files \ OpenSSL-Win64 \ bin) and copy them to the program folder or system32 ourselves.





    Then there is one more point. In my case, the project platform had to be x64.





    That's all. Add #include <libpq-fe.h>



    to the program and work












All Articles