PostgreSQLのlibpqプログラムを構築する方法

PostgreSQL libpqプログラムの構築方法

PostgreSQL libpqを使用してプログラムを作成およびコンパイルするのはそれほど簡単ではありません。 PostgreSQLドキュメントで「testlibpq.c」のようなサンプルプログラムを作成してテストしました。

コンパイルすると、次のエラーが発生します

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'

ライブラリがありません。 -L/usr/lib/postgresql/8.3/lib/でPostgreSQLライブラリパスを含めます。このすべての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'

libpgライブラリを含めるには、-lpqでライブラリを正確に指定する必要があります。正しいコマンドは次のようになります。

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

完了、エラーなしでコンパイルします。