【问题标题】:error: dereferencing pointer to incomplete type when compil C program错误:在编译 C 程序时取消引用指向不完整类型的指针
【发布时间】:2014-12-19 15:03:04
【问题描述】:

我在这里放了一个简短的源代码,因为源代码太长了。你可以在这个 git 存储库中找到完整的源代码:https://github.com/strophe/libstrophe

事实上,我在我的主 C 程序中使用 strophe 作为一个库,它是一个 openWrt 包。

common.h (libstrophe) 的源代码

/** run-time context **/

typedef enum {
    XMPP_LOOP_NOTSTARTED,
    XMPP_LOOP_RUNNING,
    XMPP_LOOP_QUIT
} xmpp_loop_status_t;

typedef struct _xmpp_connlist_t {
    xmpp_conn_t *conn;
    struct _xmpp_connlist_t *next;
} xmpp_connlist_t;

struct _xmpp_ctx_t {
    const xmpp_mem_t *mem;
    const xmpp_log_t *log;

    xmpp_loop_status_t loop_status;
    xmpp_connlist_t *connlist;
};

这是头文件strophe.h源代码(libstrophe):

/
    * user-replaceable memory allocator */
    typedef struct _xmpp_mem_t xmpp_mem_t;

    /* user-replaceable log object */
    typedef struct _xmpp_log_t xmpp_log_t;

    /* opaque run time context containing the above hooks */
    typedef struct _xmpp_ctx_t xmpp_ctx_t;

    xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem, 
                     const xmpp_log_t * const log);
    void xmpp_ctx_free(xmpp_ctx_t * const ctx);
    struct _xmpp_log_t {
        xmpp_log_handler handler;
        void *userdata;
        /* mutex_t lock; */
    };

ctx.c 简要源代码(libstrophe):

xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem, 
             const xmpp_log_t * const log)
{
    xmpp_ctx_t *ctx = NULL;

    if (mem == NULL)
    ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t), NULL);
    else
    ctx = mem->alloc(sizeof(xmpp_ctx_t), mem->userdata);

    if (ctx != NULL) {
    if (mem != NULL) 
        ctx->mem = mem;
    else 
        ctx->mem = &xmpp_default_mem;

    if (log == NULL)
        ctx->log = &xmpp_default_log;
    else
        ctx->log = log;

    ctx->connlist = NULL;
    ctx->loop_status = 0;//XMPP_LOOP_NOTSTARTED;
    }

    return ctx;
}

主 C 程序

#include <strophe.h>

void main()
{
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
char *jid, *pass;


create a context */
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
ctx = xmpp_ctx_new(NULL, log);
/* create a connection */
conn = xmpp_conn_new(ctx);
/* setup authentication information */
xmpp_conn_set_jid(conn, "jid");
xmpp_conn_set_pass(conn, "pass");
/* initiate connection */
xmpp_connect_client(conn, "alt3.xmpp.l.google.com", 5222, conn_handler, ctx);
 ctx->loop_status = 1; // error error: dereferencing pointer to incomplete type
/* enter the event loop -
our connect handler will trigger an exit */
xmpp_run(ctx);
/* release our connection and context */
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
/* final shutdown of the library */
xmpp_shutdown();
 }

使用此源代码编译时出现此错误:

错误:取消引用指向不完整类型的指针

【问题讨论】:

  • main.c #include common.h 是直接还是间接?我敢打赌它不会。
  • @Fred Larson : common.h 文件包含#include "strophe.h"
  • 错误信息引用了哪一行代码?
  • 在 C 主程序中:ctx->loop_status = 1; // 错误错误:取消引用指向不完整类型的指针
  • 第一条评论中的问题仍然存在(即尚未回答)。

标签: c


【解决方案1】:

是循环依赖头问题。

文件 strophe.h 必须在 common.h 之前包含

因为 strophe.h 中的类型必须由 common.h 知道

#include "strophe.h"
#include "common.h"

void main()
{
    xmpp_ctx_t *ctx;

    xmpp_initialize();

    ctx = xmpp_ctx_new(NULL, log);
    /* create a connection */
    conn = xmpp_conn_new(ctx);
    xmpp_connect_client(conn, "alt3.xmpp.l.google.com", 5222, conn_handler,
            ctx);
    ctx->loop_status = 1; // error error: dereferencing pointer to incomplete type
    xmpp_run(ctx);
}

【讨论】:

  • 我只包括 stophe.h。这是一个为openwrt包编译的库
  • 查看 git repo 中的 stophe.h
  • 编译器告诉你他不知道xmpp_mem_t和xmpp_log_t是什么
  • 编译器不知道 xmpp_loop_status_t 结构
  • 编译器抱怨指向不完整类型的指针;在注释指示的行中,这将是ctx,其类型为xmpp_ctx_t *。编译器不知道xmpp_ctx_t 是什么。
猜你喜欢
  • 2018-10-23
  • 2011-02-04
  • 1970-01-01
  • 2018-05-31
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
相关资源
最近更新 更多