Comment compiler les programmes PostgreSQL libpq

Comment construire des programmes libpq PostgreSQL

Créer et compiler un programme avec PostgreSQL libpq n'est pas si simple. J'ai créé un exemple de programme comme «testlibpq.c» dans la documentation PostgreSQL pour le tester.

Quand je le compile, je frappe l'erreur suivante

libpq-fe.h: No such file or directory
PGconn ’non déclaré (première utilisation dans cette fonction)

example@example-desktop:~/Desktop/index$ gcc -o test.o test.c
test.c:8:22: error: libpq-fe.h: No such file or directory
test.c:11: error: expected ‘)’ before ‘*’ token
test.c: In function ‘main’:
test.c:21: error: ‘PGconn’ undeclared (first use in this function)
test.c:21: error: (Each undeclared identifier is reported only once
test.c:21: error: for each function it appears in.)
test.c:21: error: ‘conn’ undeclared (first use in this function)
test.c:22: error: ‘PGresult’ undeclared (first use in this function)
test.c:22: error: ‘res’ undeclared (first use in this function)
test.c:41: error: ‘CONNECTION_OK’ undeclared (first use in this function)
test.c:57: error: ‘PGRES_COMMAND_OK’ undeclared (first use in this function)
test.c:83: error: ‘PGRES_TUPLES_OK’ undeclared (first use in this function)

Il doit inclure-I/usr/include/postgresql/ afin de le compiler, veuillez vérifier votre base de données ou votre administrateur Linux, ou tapez simplement «pg_config –includedir» pour savoir où se trouve le fichier d'inclusion de PostgreSQL.

Cependant, je frappe toujours une erreur comme follwing

référence indéfinie à «PQfinish»
référence indéfinie à «PQconnectdb»

example@example-desktop:~/Desktop/index$ gcc -I/usr/include/postgresql/  -o test.o test.c
/tmp/ccoOJzAT.o: In function `exit_nicely':
test.c:(.text+0xd): undefined reference to `PQfinish'
/tmp/ccoOJzAT.o: In function `main':
test.c:(.text+0x57): undefined reference to `PQconnectdb'
test.c:(.text+0x65): undefined reference to `PQstatus'
test.c:(.text+0x74): undefined reference to `PQerrorMessage'
test.c:(.text+0xac): undefined reference to `PQexec'
test.c:(.text+0xba): undefined reference to `PQresultStatus'
test.c:(.text+0xca): undefined reference to `PQerrorMessage'
test.c:(.text+0xef): undefined reference to `PQclear'
test.c:(.text+0x105): undefined reference to `PQclear'
test.c:(.text+0x118): undefined reference to `PQexec'

La bibliothèque est manquante. Incluez le chemin de la bibliothèque PostgreSQL avec-L/usr/lib/postgresql/8.3/lib/, toutes ces informations de configuration PostgreSQL peuvent l'obtenir à partir de «pg_config».

Mais je frappe encore la même erreur

référence indéfinie à «PQfinish»
référence non définie à «PQconnectdb»

example@example-desktop:~/Desktop/index$ gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -o test.o test.c
/tmp/ccgWnRJg.o: In function `exit_nicely':
test.c:(.text+0xd): undefined reference to `PQfinish'
/tmp/ccgWnRJg.o: In function `main':
test.c:(.text+0x57): undefined reference to `PQconnectdb'
test.c:(.text+0x65): undefined reference to `PQstatus'
test.c:(.text+0x74): undefined reference to `PQerrorMessage'
test.c:(.text+0xac): undefined reference to `PQexec'
test.c:(.text+0xba): undefined reference to `PQresultStatus'
test.c:(.text+0xca): undefined reference to `PQerrorMessage'
test.c:(.text+0xef): undefined reference to `PQclear'
test.c:(.text+0x105): undefined reference to `PQclear'
test.c:(.text+0x118): undefined reference to `PQexec'
test.c:(.text+0x126): undefined reference to `PQresultStatus'
test.c:(.text+0x136): undefined reference to `PQerrorMessage'
test.c:(.text+0x15b): undefined reference to `PQclear'
test.c:(.text+0x171): undefined reference to `PQclear'

Il faut spécifier exactement la bibliothèque avec-lpq pour inclure la bibliothèque libpg, la commande correcte est comme suit

gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -lpq -o test.o test.c

Terminé, compilez sans erreur.