错误:在编译C程序时解除引用指向不完整类型的指针

时间:2014-12-19 15:03:05

标签: c

我在这里提供了一个简短的源代码,因为源太长了。你可以在这个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();
 }

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

  

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

1 个答案:

答案 0 :(得分: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);
}