My task is to connect with the PostgreSQL database through a C ++ program. Found the official library for this purpose (libpq). Now I tried to use it in VC ++ 13 (Windows 7), but it does not work! Error inserted below.

Help or solve this problem, or find another lib for working with this database.

Now to the point:

Added additional directories for the project parameters (for PostgreSQL \ 9.3 \ include) and for libs (PostgreSQL \ 9.3 \ lib). Additional dependencies in linking options added libpq.lib

main.cpp:

 /* * testlibpq.c * * Test the C version of libpq, the PostgreSQL frontend library. */ #include <stdio.h> #include <stdlib.h> #include <libpq-fe.h> static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main(int argc, char **argv) { const char *conninfo; PGconn *conn; PGresult *res; int nFields; int i, j; /* * If the user supplies a parameter on the command line, use it as the * conninfo string; otherwise default to setting dbname=postgres and using * environment variables or defaults for all other connection parameters. */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = postgres"; /* Make a connection to the database */ conn = PQconnectdb(conninfo); /* Check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /* * Our test case here involves using a cursor, for which we must be inside * a transaction block. We could do the whole thing with a single * PQexec() of "select * from pg_database", but that's too trivial to make * a good example. */ /* Start a transaction block */ res = PQexec(conn, "BEGIN"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } /* * Should PQclear PGresult whenever it is no longer needed to avoid memory * leaks */ PQclear(res); /* * Fetch rows from pg_database, the system catalog of databases */ res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "FETCH ALL in myportal"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } /* first, print out the attribute names */ nFields = PQnfields(res); for (i = 0; i < nFields; i++) printf("%-15s", PQfname(res, i)); printf("\n\n"); /* next, print out the rows */ for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) printf("%-15s", PQgetvalue(res, i, j)); printf("\n"); } PQclear(res); /* close the portal ... we don't bother to check for errors ... */ res = PQexec(conn, "CLOSE myportal"); PQclear(res); /* end the transaction */ res = PQexec(conn, "END"); PQclear(res); /* close the connection to the database and cleanup */ PQfinish(conn); return 0; } 

Error:

 1>------ Build started: Project: PostgreSQL, Configuration: Debug Win32 ------ 1>main.obj : error LNK2019: unresolved external symbol _PQconnectdb referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQfinish referenced in function "void __cdecl exit_nicely(struct pg_conn *)" (?exit_nicely@@YAXPAUpg_conn@@@Z) 1>main.obj : error LNK2019: unresolved external symbol _PQstatus referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQerrorMessage referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQexec referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQresultStatus referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQntuples referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQnfields referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQfname referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQgetvalue referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol _PQclear referenced in function _main 1>D:\Work\C++\PostgreSQL\Debug\PostgreSQL.exe : fatal error LNK1120: 11 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
  • one
    Google LNK2019: unresolved external symbol _PQconnectdb referenced gives meleon recipes, watched? Two duplicates in English stack - strangeqargo
  • Eh, if I hadn’t looked, I wouldn’t have written. There, people everywhere just forget to add a name to the linker dependencies (libpq.lib in this case). After fixing this, everything works for them. I also read that someone used version 8 of the database, and then again 9, and only then everything worked for him, and there was a problem such as mine. At the office. I haven’t found version 8 of the site to try this trick ... And this is, after all, “dance with a tambourine”. I want the right decision. - Ligvest O
  • one
    finally there is no (about the name of the stack) stackoverflow.com/a/20915117/5006740 here and stackoverflow.com/a/15810129/5006740 here are very different solutions and they are mostly about x32 / x64 build problems - strangeqargo
  • one
    Thanks for the help. Answered your question. Indeed, it was a bit case. I tried to change this parameter, but did not know where to find it. Finally, I understood, thanks to links, how to form a query correctly in order to find what I need. - Ligvest O

1 answer 1

Here is the solution. Change the output project bit depth from 32 to 64. The instruction is drawn to: https://msdn.microsoft.com/en-us/library/9yb4317s.aspx