0

Refering to Connect Postgresql in C

I asked some help to compile my script of Postgres database connection. This is now compiling well but now I have a problem when I execute it.

There is the code :

#include <stdio.h>
#include "libpq-fe.h"
#include <stdlib.h>

int     main() {
PGconn          *conn;
PGresult        *res;
int             rec_count;
int             row;
int             col;



        conn = PQconnectdb("dbname=ljdata host=localhost user=dataman password=supersecret");

        if (PQstatus(conn) == CONNECTION_BAD) {
                puts("We were unable to connect to the database");
                exit(0);
        }

        res = PQexec(conn,
                "update people set phonenumber=\'5055559999\' where id=3");

        res = PQexec(conn,
                "select lastname,firstname,phonenumber from people order by id");

        if (PQresultStatus(res) != PGRES_TUPLES_OK) {
                puts("We did not get any data!");
                exit(0);
        }

        rec_count = PQntuples(res);

        printf("We received %d records.\n", rec_count);
        puts("==========================");

        for (row=0; row<rec_count; row++) {
                for (col=0; col<3; col++) {
                        printf("%s\t", PQgetvalue(res, row, col));
                }
                puts("");
        }

        puts("==========================");

        PQclear(res);

        PQfinish(conn);

        return 0;
}

And this is the compilation line it's actually working :

gcc -m64 -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq -o test.exe

Now when I double-click on the .exe I have the following error :

"Unable to start the program because LIBPQ.dll is missing"

So it clearly means that I have to link the libpq.dll

But what I'm trying to do is compiling all the methods from the .c/.dll etc in the binary .exe which I plan to distribute and the postgresql libs will not be installed on the target machines.

After some web search, I found the "-static" parameter but I'm not sure that it's what I'm looking for and I'm not able to have it compiling with this parameter.

Then I tried to add "-static-libgcc" which results as :

gcc -m64 -static-libgcc -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq -o test.exe

But I still have the "System error" when I try to run it :

"Unable to run the script because LIBPQ.dll is missing on your computer" (Not sure if my french translate is exact)

Do you know the parameter or the way I can compile my C program into a binary .exe which would be totally standalone and without any library requirement ?

EDIT

Screenshot :

error

Community
  • 1
  • 1
M. Ozn
  • 1,018
  • 1
  • 19
  • 42

1 Answers1

1

Stick with dynamic linking.

Your problem is that the system cannot find libpq.dll at runtime.

There are two solutions:

  1. Add C:\Program Files\PostgreSQL\9.6\lib to the PATH environment variable.

  2. Put a copy of libpq.dll in the same path as your executable.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Because I want to deploy my exe, I choose the second option. The compilation worked well with this line : [ gcc -m64 -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" -Wl,-rpath,"C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq -o test.exe ]. But when I double click on the executable, a cmd prompt appears then another windows come aswell with the usual error. If you are able to understand french or translate the error, I edited my first post to add the screenshot – M. Ozn Mar 29 '17 at 21:45
  • Sorry, I made a mistake (I'm no Windows specialist). Windows doesn't have a concept of runpath. I have fixed the answer with an alternative suggestion. Now I know why there usually are so many copies of shared libraries on that benighted operating system. – Laurenz Albe Mar 30 '17 at 18:59
  • Thanks. The second solution still better to deploy my app. I suppose I can't just provide a standalone .exe without nothing next to it (for example the .dll). Thanks anyway ! – M. Ozn Mar 30 '17 at 19:32
  • Sounds like a nice idea, but there is no static libpq. You'd have to build it yourself, and I don't know if that even works, particularly on Windows. The documentation offers nothing, And it is no better that shipping a copy of `libpq.dll`... – Laurenz Albe Mar 31 '17 at 12:29