简体   繁体   English

远期申报C

[英]Forward declaration C

I have 2 header files api.h and impl.h 我有2个头文件api.h和impl.h

api.h is visible to outside files and will be included in other ".c" files. api.h对外部文件可见,并将包含在其他“ .c”文件中。 So api.h includes impl.h 所以api.h包括impl.h

api.h defines 2 structures api.h定义2种结构

typedef struct
    {
    uint32_t att;
    union
    {
        struct
        { 
            void* buffer;
            size_t length;
        } x;  
        struct
        {
            int a, b;
        } v;
    } content;
}dummy;

and impl.h has some other structures and function def which uses this structure. impl.h还有其他一些结构和使用此结构的函数def。 I tried forward declaration but it doesn't help me . 我尝试了前向声明,但无济于事。

Please help . 请帮忙 。

Actually, your dummy is not a structure, but a typedef to an unnamed structure. 实际上,您的dummy对象不是结构,而是对未命名结构的typedef Try naming the structure, you can then forward-declare it: 尝试命名结构,然后可以向前声明它:

typedef struct sdummy dummy; // forward declaration

void foo(dummy *);

struct sdummy { ... }; // definition

Either reorder your code in api.h so the type declaration precedes the #include "impl.h" or give your (currently anonymous) structure itself a name like dummy , dummy_ , dummy_s so you can add a forward declaration 您可以在api.h对代码重新排序,以便类型声明在#include "impl.h"之前,或者为您(当前匿名)结构本身指定一个名称,例如dummydummy_dummy_s以便您可以添加前向声明

typedef struct dummy_ dummy;

to impl.h . impl.h

If you want to hide the details of your struct then you have to define it in some .c file, let's say impl.c , so that it has internal linkage to that compilation unit. 如果要隐藏结构的详细信息,则必须在某个.c文件中对其进行定义,例如impl.c ,以便它具有与该编译单元的内部链接。 To use it you have to expose create, destroy, getter and setter functions. 要使用它,您必须公开create,destroy,getter和setter函数。 So a basic setup would look like this: 因此,基本设置如下所示:

api.h with forward declaration for your struct api.h具有结构的前向声明

// forward declaration
typedef struct dummy* dummy_t;

// create / destroy / setter / getter (omitted)
dummy_t alloc_dummy();
void free_dummy(dummy_t);
void set_number(dummy_t, int);
void set_string(dummy_t, char*);
void print_dummy(dummy_t);

Then comes impl.c 然后是impl.c

#include "api.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct dummy {
    int n;
    char* s;
};

dummy_t alloc_dummy()
{
    return malloc(sizeof(struct dummy));
}

void free_dummy(dummy_t dummy)
{
    if(dummy) {
        free(dummy->s);
        free(dummy);
    }
}

void set_number(dummy_t dummy, int n)
{
    if(dummy) {
        dummy->n = n;
    }
}

void set_string(dummy_t dummy, char* s)
{
    if(dummy && s) {
        dummy->s = strdup(s);
    }
}

void print_dummy(dummy_t dummy)
{
    if(dummy) {
        printf("%d, %s\n", dummy->n, dummy->s);
    }
}

And finally the usage in some other C files, here main.c 最后是其他一些C文件的用法,这里是main.c

#include "api.h"

int main(int argc, char** argv) 
{
    // struct dummy d; // error! type is unknown
    // instead use the create function
    dummy_t d = alloc_dummy();
    // d->n = 1; // error! dereference of unknown type
    // instead use the setter function
    set_number(d, 1);
    set_string(d, "Hello, world!");
    print_dummy(d);
    free_dummy(d);
    return 0;
}

Ouput 输出继电器

1, Hello, world!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM