0

I'm trying to connect a C script running on Windows to my Postgresql database.

For the moment, I got this script on a website :

#include <stdio.h>
#include "libpq-fe.h"
#include <string>
#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;
}

I copied the libpq-fe.h, the postgres_ext.c and the pg_config_ext.c to avoid the errors.

and now I have all this errors :

  • undefined reference to 'PQconnectdb'
  • undefined reference to 'PQresultStatus'
  • ...

So obviously I don't have the file.c which contains all the functions I need but I can't find it.

I saw on another forum I need to create a makefile to tell the compiler to use the libpq.dll (I have this file) but I haven't the knowledge to do that.

How can I make this working ?

EDIT

So now when I try to compile it like that :

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

And I get the error :

C:\Program Files\PostgreSQL\9.6\lib/libpq.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

I think it's near to the final solution.

From the research I made, I found that libpq.dll is 32bit, but my environnement is 64bit. do this change anything ?

EDIT 2

The compilation now works correctly with this line :

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

But 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 with probably a link to Program Files/PG/9.6/lib.

The problem here is that I want to build a standalone .exe which probably embed the Postgresql lib to works correctly even if the computer target haven't Postgresql installed on it

(This is possible, for example in Java when we build a Jar with all the external lib copied in)

Is it possible in C ? I think I need to adapt my compilation line but I don't found which parameter I can add to import all the needed lib in the .exe

M. Ozn
  • 1,018
  • 1
  • 19
  • 42
  • You don't need the C file, You just need to include Postgres library in linker like this: `-lpq`. – Roman Hocke Mar 24 '17 at 08:53
  • Because I'm on Windows, I have to compile it with mingw. When I type "g++ -lpq test.c" I get the following error : "cannot find -lpq" – M. Ozn Mar 24 '17 at 08:55
  • See this question: http://stackoverflow.com/questions/15977181/error-when-connecting-to-postgres-database-in-c-using-libpq-fe-h – Roman Hocke Mar 24 '17 at 08:55
  • Are you also compiling those .c files? If you are using gcc, for instance, `gcc main.c postgres_ext.c pg_config_ext.c -o ouput` – TomasCarvalho Mar 24 '17 at 08:57
  • Sorry I made a mistake, those files are header (.h) – M. Ozn Mar 24 '17 at 09:22
  • The fact that you are calling it a "C Script" indicates that you're in for trouble. ;) – David Hoelzer Mar 24 '17 at 09:48
  • I know it's not a C script, but it looks like the dll can do the work. If not, maybe you can indicates me which file I need to include and where can I find it ? Thanks by advance. – M. Ozn Mar 24 '17 at 09:52
  • If you're on Windows, why not just use MSVC (Visual Studio)? Nothing wrong with gcc, but MSVC is easier for beginners. – Craig Ringer Mar 24 '17 at 10:03

1 Answers1

4

From your edits I see that you have figured out how to use the -I and -L options of gcc by now. Good.

I guess your relaining problem is that you are trying to link with a 64-bit libpq.dll in 32-bit mode or vice versa.

You can set the mode with the gcc options -m32 and -m64.

You can examine the shared library with file libpq.dll, that should tell you if it is a 32-bit or a 64-bit library.

If gcc tells you something like: 64-bit mode not compiled in, you need to install an appropriate version of gcc.

To solve the problem that libpq.dll is not found at run time, either put the directory where it resides into the PATH environment variable, or use the option -Wl,-rpath,<directory with libpq.dll>.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Yes it was the problem ! I installed MinGW-w64 and now the compilation works great. Thanks a lot ! – M. Ozn Mar 24 '17 at 12:50
  • I had another problem during the execution and I think it refer to the compilation too. I edited my first post if you're able to answer it. Thanks ! – M. Ozn Mar 24 '17 at 15:23
  • Hmm, you should have answered another question (or actually run a web search, since this is an everyday problem). I have amended the answer. – Laurenz Albe Mar 26 '17 at 03:33
  • I ran a web search but I didn't find something working as I want. Anyway thanks for your answer, but It looks like it doesn't really solve my problem because I'm looking for a way to build a standalone .exe without any directory next to it. But don't worry I'll write a new question. Thanks ! – M. Ozn Mar 28 '17 at 13:30