1

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

8
  • You don't need the C file, You just need to include Postgres library in linker like this: -lpq. Commented Mar 24, 2017 at 8: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" Commented Mar 24, 2017 at 8:55
  • See this question: stackoverflow.com/questions/15977181/… Commented Mar 24, 2017 at 8: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 Commented Mar 24, 2017 at 8:57
  • Sorry I made a mistake, those files are header (.h) Commented Mar 24, 2017 at 9:22

1 Answer 1

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>.

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

4 Comments

Yes it was the problem ! I installed MinGW-w64 and now the compilation works great. Thanks a lot !
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 !
Hmm, you should have answered another question (or actually run a web search, since this is an everyday problem). I have amended the answer.
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 !

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.