Как создавать программы PostgreSQL для libpq

Как создавать программы PostgreSQL для libpq

Создать и скомпилировать программу с PostgreSQL libpq не так просто. Я создал образец программы, такой как «testlibpq.c» в документации PostgreSQL, чтобы протестировать ее.

Когда я его компилирую, я нажимаю следующую ошибку

libpq-fe.h: No such file or directory
PGconn ’не объявлен (первое использование в этой функции)

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)

Он действительно должен включать-I/usr/include/postgresql/ для его компиляции, проверьте свою базу данных или администратора Linux, или просто введите «pg_config –includedir», чтобы узнать, где находится включаемый файл PostgreSQL.

Однако я все еще сталкиваюсь с ошибкой, как следующие

неопределенная ссылка на `PQfinish '
неопределенная ссылка на `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'

Библиотека отсутствует. Включите путь к библиотеке PostgreSQL с-L/usr/lib/postgresql/8.3/lib/, вся эта информация о конфигурации PostgreSQL может быть получена из «pg_config».

Но я все еще сталкиваюсь с той же ошибкой снова

неопределенная ссылка на `PQfinish '
неопределенная ссылка на `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'

Необходимо указать именно библиотеку с-lpq, чтобы включить библиотеку libpg, правильная команда выглядит следующим образом

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

Готово, скомпилировать без ошибок.