如何克服SWIG生成的libpg_query源代码中的gcc错误

7xzttuei  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(170)

如何克服SWIG为libpg_query库生成的C源文件中的gcc编译器错误?
libpg_query是一个“用于访问服务器外的PostgreSQL解析器的C库”,由pganalyze的优秀人员维护,但重要的是,不是由我维护的。它在Ruby、Go和Python中有绑定,但在Java中没有,所以我想创建它们。我正在尝试使用SWIG来做这件事。SWIG文档的介绍中说,“SWIG通常不需要修改现有的代码,通常可以在几分钟内构建一个可用的接口。”不幸的是,我对这一点的希望到目前为止已经破灭了。我遵循了他们的Java指南,但是在生成的C源文件中遇到了同样的gcc编译器错误。下面是详细的步骤。

wget https://raw.githubusercontent.com/pganalyze/libpg_query/13-latest/pg_query.h
swig -java -module test pg_query.h
gcc -fPIC -c pg_query_wrap.c -I /usr/lib/jvm/jdk-11.0.7/include/ -I /usr/lib/jvm/jdk-11.0.7/include/linux/ &> errors.txt
cat errors.txt | grep "error:" | cut -d' ' -f3- | sort | uniq

在编译步骤中,我得到了大量的错误,基本上可以归结为:

expected expression before ‘)’ token
‘PgQueryDeparseResult’ undeclared (first use in this function)
‘PgQueryError’ undeclared (first use in this function)
‘PgQueryFingerprintResult’ undeclared (first use in this function)
‘PgQueryNormalizeResult’ undeclared (first use in this function)
‘PgQueryParseResult’ undeclared (first use in this function)
‘PgQueryPlpgsqlParseResult’ undeclared (first use in this function)
‘PgQueryProtobufParseResult’ undeclared (first use in this function)
‘PgQueryProtobuf’ undeclared (first use in this function)
‘PgQueryScanResult’ undeclared (first use in this function)
‘PgQuerySplitResult’ undeclared (first use in this function)
‘PgQuerySplitStmt’ undeclared (first use in this function)
request for member ‘context’ in something not a structure or union
request for member ‘cursorpos’ in something not a structure or union
request for member ‘data’ in something not a structure or union
request for member ‘error’ in something not a structure or union
request for member ‘filename’ in something not a structure or union
request for member ‘fingerprint’ in something not a structure or union
request for member ‘fingerprint_str’ in something not a structure or union
request for member ‘funcname’ in something not a structure or union
request for member ‘len’ in something not a structure or union
request for member ‘lineno’ in something not a structure or union
request for member ‘message’ in something not a structure or union
request for member ‘normalized_query’ in something not a structure or union
request for member ‘n_stmts’ in something not a structure or union
request for member ‘parse_tree’ in something not a structure or union
request for member ‘pbuf’ in something not a structure or union
request for member ‘plpgsql_funcs’ in something not a structure or union
request for member ‘query’ in something not a structure or union
request for member ‘stderr_buffer’ in something not a structure or union
request for member ‘stmt_len’ in something not a structure or union
request for member ‘stmt_location’ in something not a structure or union
request for member ‘stmts’ in something not a structure or union
‘uint64_t’ undeclared (first use in this function); did you mean ‘u_int64_t’?
unknown type name ‘PgQueryDeparseResult’
unknown type name ‘PgQueryError’
unknown type name ‘PgQueryFingerprintResult’
unknown type name ‘PgQueryNormalizeResult’
unknown type name ‘PgQueryParseResult’
unknown type name ‘PgQueryPlpgsqlParseResult’
unknown type name ‘PgQueryProtobuf’
unknown type name ‘PgQueryProtobufParseResult’
unknown type name ‘PgQueryScanResult’
unknown type name ‘PgQuerySplitResult’
unknown type name ‘PgQuerySplitStmt’
unknown type name ‘uint64_t’; did you mean ‘u_int64_t’?

正如您所看到的,这里有关于未声明类型、关于未使用结构或联合以及关于未知类型的错误。我发现了其他关于SWIG的类似StackOverflow问题,答案是修复正在 Package 的C头文件,但我不能这样做,因为我没有编写pg_query.h。为了使它工作,我可以通过调用SWIG和/或gcc编译器来做些什么吗?谢谢你!

编辑1

不管出于什么原因,使用所有可能的接口文件中最简单的一个就可以了,即使SWIG文档中说接口文件不是绝对必要的:

%module test
%{
#include "pg_query.h"
%}
%include "pg_query.h"

也许回答这个问题的另一种方式是说为什么需要这样做。

c90pui9n

c90pui9n1#

通常,需要以下接口文件:

// test.i
%module test

// This code is directly injected into the wrapper, and probably
// what was missing running SWIG directly on the header itself.
%{
#include "pg_query.h"
%}

// Process top-level code in this header file and wrap functions and globals found.
// It does not recurse into other includes like stdint.h.
%include "pg_query.h"

然后,swig -java test.i
即使它编译并链接了,你也会想要测试每个函数。SWIG在它的 Package 器中是 * 非常 * 宽容的,它把任何它不理解的东西都当作不透明的指针。

相关问题