How to compile the following linux program with postgres libraries

#include <iostream> //using namespace std; #include <stdio.h> #include <postgresql/libpq-fe.h> #include <string> #include <cstdio> #include <stdlib.h> #include <libc.h> int main() { PGconn *conn; PGresult *res; int rec_count; int row; int col; FILE *stream; conn = PQconnectdb("hostaddr=192.168.143.92 port=5432 connect_timeout=5 dbname=NexentaSearch user=DKOI password=21111991"); if (PQstatus(conn) == CONNECTION_BAD) { puts("Не удается подключиться к базе данных"); exit(0); } res = PQexec(conn, "select path from paths order by id"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { printf("Мы не получили данные"); exit(0); } rec_count = PQntuples(res); printf("Мы получили %d записей.\n", rec_count); puts("=========================="); stream=fopen("usr/local/ paths.txt ", "w"); for (row=0; row<rec_count; row++) { for (col=0; col<1; col++) { fprintf(stream, "%s\n", PQgetvalue(res, row, col)); } puts(""); } puts("=========================="); PQclear(res); PQfinish(conn); return 0; } 

Attempting to compile with gcc -c readwritepaths.cpp resulted in an error

gcc: error trying to exec 'cc1plus': execvp: No such file or directory

  • @ ivan31, To format a code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky
  • g ++ deliver - avp
  • and how to compile with g ++? - ivan89
  • @ ivan31, when gcc "sees" the .cpp file extension, it decides that this is a c ++ program and calls the appropriate compiler modules. Compile g ++ - similar to gcc g ++ readwritepaths.cpp By the way, you probably need to specify (via the -l keys) which Postgres libraries to install and maybe in which directory to search for them (via -L) - avp

2 answers 2

 g++ -I/usr/include my_program.cpp -o my_program_bin -L/usr/lib/ -lpostres 

Something like this, only you need to specify the postgreSQL libraries and libraries.

  • -lpq , no? - drdaeman
  • @ Re1aps, / usr / include and / usr / lib can be omitted (these are the default directories). - avp pm
  • Is it really? I hinted at - "only need to specify the libraries and libraries." - Re1aps
  • @ Re1aps what does my_program_bin mean? - ivan89
  • g ++ - compiler -I / usr / include / postres - path to postgres inches. That you yourself need to see where they lie, presumably I wrote. my_program.cpp - file with the program. -o - create a binary (executable file) my_program_bin - the name of the binary tobit the file that you will run. -L / usr / lib / postgres - the path to the postgres libs, again you need to clarify this with yourself -lpostgres - the name of the libraries used. When all this is done in the terminal, go to the folder with the binary and do the following ./my_program_bin - Re1aps

Have you ever see what you are doing. You are trying a C ++ program (since iostream, etc., is included) and having the .cpp extension to compile using gcc - C compiler. What to do: either delete all the plush inclusions (at a cursory examination, the text is otherwise completely plain) and change the file extension to .c, or compile g ++.