我在clang插件中编写了PostgreSQL连接代码,如下所示。
class PrintFunctionNamesAction : public PluginASTAction {
protected:
ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
conn = PQconnectdb("dbname=a host=localhost user=user password=user123");
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
//break;
}
res = PQexec(conn,
"update people set phonenumber=\'8983194618\' where id=3");
res = PQexec(conn,
"select lastname,firstname,phonenumber from people order by id");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
puts("We did not get any data!");
//break;
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);
puts("==========================");
for (row=0; row<rec_count; row++) {
for (col=0; col<3; col++) {
printf("%s\t", PQgetvalue(res, row, col));
}
puts("");
}
puts("==========================");
PQclear(res);
PQfinish(conn);
return new PrintFunctionsConsumer();
}
它没有给出任何编译时错误,但我得到以下错误是运行时。
clang++ -cc1 -load ../../../../Release+Asserts/lib/libPrintFunctionNames.so
-plugin print-fns -plugin-arg-print-fns -lpq test.cpp PrintFunctionNames
arg = -lpq clang++: symbol lookup error: ../../../../Release+Asserts
/lib/libPrintFunctionNames.so: undefined symbol: PQconnectdb