PostgreSQLのlibpqでプログラムを作成しコンパイルするのは簡単ではありません。私はPostgreSQLのドキュメントで “testlibpq.c”のようなサンプルプログラムを作成してテストしました。

私はそれをコンパイルすると、私は次のエラーを打つ

mkyong@mkyong-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’への未定義参照

mkyong@mkyong-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 ‘への未定義の参照**

mkyong @ mkyong-desktop:〜/Desktop/index $ gcc -I/usr/include/postgresql/-L/usr/lib/postgresql/8.3/lib/-o test.o test.c/tmp/ccgWnRJg.o:関数 `exit__nicely 'で:

test.c :(。テキスト0xd): `PQfinish 'への未定義参照/tmp/ccgWnRJg.o:` main'関数では:

test.c :(。テキスト0x57): `PQconnectdb 'への未定義の参照
test.c :(。テキスト0x65): `PQstatus 'への未定義の参照
test.c :(。テキスト0x74): `PQerrorMessage 'への未定義の参照
test.c :(。テキスト0xac): `PQexec 'への未定義の参照
test.c :(。テキスト0xba): `PQresultStatus 'への未定義の参照
test.c :(。テキスト0xca): `PQerrorMessage 'への未定義の参照
test.c :(。テキスト0xef): `PQclear 'への未定義の参照
test.c :(。テキスト0x105): `PQclear 'への未定義参照。
test.c :(。テキスト0x118): `PQexec 'への未定義の参照
test.c :(。テキスト0x126): `PQresultStatus 'への未定義の参照
test.c :(。テキスト0x136): `PQerrorMessage 'への未定義の参照
test.c :(。テキスト0x15b): `PQclear 'への未定義の参照
test.c :(。テキスト0x171): `PQclear 'への未定義の参照

libpgをインクルードするには、

-lpq

を指定してライブラリを指定する必要があります
ライブラリ、正しいコマンドは次のようなものです

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

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