使用 libpq 获取 sizeof(PGconn)

发布于 2024-10-09 19:20:49 字数 2159 浏览 5 评论 0原文

我正在尝试为程序包装 libpq,并且我正在使用的 FFI 工具的一部分尝试获取 sizeof() 任何正在使用的结构。在这种情况下,问题是尝试获取 sizeof(PGconn) 会导致 GCC 出现一堆错误,因为它是不完整的类型。有没有办法获得相同的数据,或者我是否需要训练这个 FFI 工具来忽略不透明的类型?供参考的是生成的 C 代码和编译器错误:

/* Define on Darwin to activate all library features */
#define _DARWIN_C_SOURCE 1
/* This must be set to 64 on some systems to enable large file support. */
#define _FILE_OFFSET_BITS 64
/* Define on Linux to activate all library features */
#define _GNU_SOURCE 1
/* This must be defined on some systems to enable large file support. */
#define _LARGEFILE_SOURCE 1
/* Define on NetBSD to activate all library features */
#define _NETBSD_SOURCE 1
/* Define to activate features from IEEE Stds 1003.1-2001 */
#define _POSIX_C_SOURCE 200112L
/* Define on FreeBSD to activate all library features */
#define __BSD_VISIBLE 1
#define __XSI_VISIBLE 700
/* Windows: winsock/winsock2 mess */
#define WIN32_LEAN_AND_MEAN

#include <libpq-fe.h>

#include <stdio.h>
#include <stddef.h>   /* for offsetof() */

void dump(char* key, int value) {
    printf("%s: %d\n", key, value);
}


void dump_section_PGconn(void) {
    typedef PGconn platcheck_t;
    typedef struct {
        char c;
        platcheck_t s;
    } platcheck2_t;

    platcheck_t s;
    dump("align", offsetof(platcheck2_t, s));
    dump("size",  sizeof(platcheck_t));
}

int main(int argc, char *argv[]) {
    printf("-+- PGconn\n");
    dump_section_PGconn();
    printf("---\n");
    return 0;
}

以及错误:

[platform:execute] gcc -c -O3 -pthread -fomit-frame-pointer -Wall -Wno-unused -I/usr/include/postgresql/ /tmp/usession-default-52/platcheck_10.c -o /tmp/usession-default-52/platcheck_10.o
[platform:Error] /tmp/usession-default-52/platcheck_10.c: In function ‘dump_section_PGconn’:
[platform:Error] /tmp/usession-default-52/platcheck_10.c:34: error: field ‘s’ has incomplete type
[platform:Error] /tmp/usession-default-52/platcheck_10.c:37: error: storage size of ‘s’ isn’t known
[platform:Error] /tmp/usession-default-52/platcheck_10.c:39: error: invalid application of ‘sizeof’ to incomplete type ‘platcheck_t’

I'm attempting to wrap libpq for a program, and part of the FFI tool I'm using attempts to get sizeof() any struct in use. In this case the issue is that trying to get sizeof(PGconn) results in a bunch of errors from GCC because it is an incomplete type. Is there a way to get the same data, or do I need to train to this FFI tool to ignore types that are intended to be opaque? For reference here is the generated C code, and the compiler errors:

/* Define on Darwin to activate all library features */
#define _DARWIN_C_SOURCE 1
/* This must be set to 64 on some systems to enable large file support. */
#define _FILE_OFFSET_BITS 64
/* Define on Linux to activate all library features */
#define _GNU_SOURCE 1
/* This must be defined on some systems to enable large file support. */
#define _LARGEFILE_SOURCE 1
/* Define on NetBSD to activate all library features */
#define _NETBSD_SOURCE 1
/* Define to activate features from IEEE Stds 1003.1-2001 */
#define _POSIX_C_SOURCE 200112L
/* Define on FreeBSD to activate all library features */
#define __BSD_VISIBLE 1
#define __XSI_VISIBLE 700
/* Windows: winsock/winsock2 mess */
#define WIN32_LEAN_AND_MEAN

#include <libpq-fe.h>

#include <stdio.h>
#include <stddef.h>   /* for offsetof() */

void dump(char* key, int value) {
    printf("%s: %d\n", key, value);
}


void dump_section_PGconn(void) {
    typedef PGconn platcheck_t;
    typedef struct {
        char c;
        platcheck_t s;
    } platcheck2_t;

    platcheck_t s;
    dump("align", offsetof(platcheck2_t, s));
    dump("size",  sizeof(platcheck_t));
}

int main(int argc, char *argv[]) {
    printf("-+- PGconn\n");
    dump_section_PGconn();
    printf("---\n");
    return 0;
}

And the errors:

[platform:execute] gcc -c -O3 -pthread -fomit-frame-pointer -Wall -Wno-unused -I/usr/include/postgresql/ /tmp/usession-default-52/platcheck_10.c -o /tmp/usession-default-52/platcheck_10.o
[platform:Error] /tmp/usession-default-52/platcheck_10.c: In function ‘dump_section_PGconn’:
[platform:Error] /tmp/usession-default-52/platcheck_10.c:34: error: field ‘s’ has incomplete type
[platform:Error] /tmp/usession-default-52/platcheck_10.c:37: error: storage size of ‘s’ isn’t known
[platform:Error] /tmp/usession-default-52/platcheck_10.c:39: error: invalid application of ‘sizeof’ to incomplete type ‘platcheck_t’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-10-16 19:20:49

PGConn 在设计上是不透明的。如果您需要查看它,请包含 libpq-int.h (表示“内部”)。但请考虑重新考虑您的要求。

PGConn is opaque by design. If you need to look into it, include libpq-int.h (for "internal"). But consider reconsidering your requirements instead.

少女的英雄梦 2024-10-16 19:20:49

当我在 Google 上搜索 PGConn 时,我看到的所有内容都是在处理 PGConn* 而不是 PGConn。我的猜测是,您应该通过指针将它们作为不透明类型进行处理。

但我确实找到了引用的 this 来源。也许这对你有帮助。

When I search for PGConn on Google, everything I see has you dealing with PGConn* instead of PGConn. My guess is that you're meant to deal with these as an opaque type through a pointer.

But I did find this that references the source. Perhaps that is helpful to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文