使用Postgres时C中的链接器错误

使用Postgres时C中的链接器错误,c,postgresql,linker,linker-errors,C,Postgresql,Linker,Linker Errors,我有以下代码: #include <stdio.h> #include <stdlib.h> #include <libpq-fe.h> int main(int argc, char* argv[]) { //Start connection PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621 sslmode=require user=ggales passwo

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>

int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621  sslmode=require user=ggales password=1234");

if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");


return 0;
}
这很奇怪,因为PQconnectdb等都是libpq fe.h中定义的函数,我已经在代码中包含了这些函数

任何帮助都将非常感谢。

#include不链接到库,它只包含库提供的函数和数据类型的信息

您必须告诉链接器在
libpq fe.h
中声明的引用的实际位置

如果使用Makefile编译代码,则应将
-lpq
添加到
LDFLAGS
或链接命令中


发布您正在运行的编译命令,以向我们提供更多信息。

在Mac上,有一个
/usr/lib/libpq.dylib
(其中
.dylib
.so
在Linux和
.dll
在Windows上类似)。库可能像
-lpq
一样简单,因此。@JonathanLeffler正确,它是libpq,只是
-lpq
/tmp/cc73kO0N.o: In function `main':
main.c:(.text+0x15): undefined reference to `PQconnectdb'
main.c:(.text+0x25): undefined reference to `PQstatus'
main.c:(.text+0x40): undefined reference to `PQfinish'
main.c:(.text+0x5d): undefined reference to `PQfinish'
collect2: error: ld returned 1 exit status