?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
要制作(也就是說編譯和鏈接)你的libpq程序,你需要做下面的一些事情: things:
包含libpq-fe.h頭文件∶
#include <libpq-fe.h>
如果你沒干這件事,那么你通常會看到類似下面這樣的來自編譯器的信息∶
foo.c: In function `main': foo.c:34: `PGconn' undeclared (first use in this function) foo.c:35: `PGresult' undeclared (first use in this function) foo.c:54: `CONNECTION_BAD' undeclared (first use in this function) foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function) foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)
告訴你的編譯器PostgreSQL頭文件的安裝位置,方法是給你的編譯器 提供-Idirectory選項(xiàng)。(有些時候編譯器會查找缺省的目錄, 因此你可以忽略這些選項(xiàng)。) 比如,你的命令行看起來象∶
cc -c -I/usr/local/pgsql/include testprog.c
如果你在使用制作文件(makefile),那么向CPPFLAGS變量中增加下面的選項(xiàng)∶
CPPFLAGS += -I/usr/local/pgsql/include
如果你的程序可能會被別人編譯,那么你應(yīng)該避免象上面那樣把目錄路徑 寫成硬編碼。 取而代之的是你可以運(yùn)行pg_config工具找出頭文件在系統(tǒng)的什么地方∶
$pg_config --includedir /usr/local/include
如果沒能給編譯器提供正確的選項(xiàng)將產(chǎn)生類似下面這樣的錯誤信息
testlibpq.c:8:22: libpq-fe.h: No such file or directory
在鏈接最后的程序的時候,聲明選項(xiàng)-lpq,這樣就可以把libpq庫鏈接進(jìn)來, 還要聲明-Ldirectory以告訴編譯器libpq所處的目錄。 (同樣,編譯器也會搜索一些缺省的目錄。) 為了盡可能提高可移植性, 你應(yīng)該把-L選項(xiàng)放在-lpq選項(xiàng)前面。比如∶
cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq
你也可以用pg_config找出庫目錄∶
$pg_config --libdir /usr/local/pgsql/lib
指向這類問題的錯誤信息會是類似下面這個樣子。
testlibpq.o: In function `main': testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin' testlibpq.o(.text+0x71): undefined reference to `PQstatus' testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'
This means you forgot-lpq.
/usr/bin/ld: cannot find -lpq
這意味著你忘記-L了或沒有指定正確的目錄。