使结构定义“全局”可见?

时间:2013-10-19 00:49:13

标签: c

是否可以在最初定义的文件中展开宏?我的意思是,如果我做了类似的事情:

    #define DEFINE_SAMPLE_CLASS(name) \
    typedef struct{ \
        int example_int; \
    } name; \

我希望它扩展到我定义此宏的原始文件中。这可能吗?我想要这样做的原因是因为我希望用户能够定义一堆类似模板的类,但是一旦他们定义了它们,我希望它们能够从其他文件中使用,而不必包含一堆其他头文件以获取他们想要的类型。如果这不可能或不实用,那么有更好的方法吗?

编辑:澄清

我基本上想要做的是允许用户“添加”到头文件,以便他们可以声明类似上面的示例:

DEFINE_SAMPLE_CLASS(Sample1)

将扩展为:

    typedef struct{
        int example_int;
    } Sample1;

然后其他文件可以使用这些“全局”定义,而不必包含定义它们的所有文件。我希望用户能够定义他们自己的这些类的版本,同时保持它们“全局”,而不必编辑原始标题。

3 个答案:

答案 0 :(得分:1)

  

我希望它在我定义此宏的原始文件中展开。这可能吗?

是。在头文件中,输入

#define DEFINE_SAMPLE_CLASS(name) typedef struct name name
DEFINE_SAMPLE_CLASS(Sample1);

完成...现在,Sample1可用作包含头文件的任何源文件中的类型。

答案 1 :(得分:1)

作为Jim Balter's answer的替代方案,您可以执行以下操作:

/* header file */
#pragma once

#define DEFINE_SAMPLE_CLASS(name) \
typedef struct{ \
    int example_int; \
} name

#include "sample_class_definitions.h"

然后,文件sample_class_definitions.h可以是脚本生成的文件,人们只需将要定义的类的名称添加到某个make脚本中。或者,用户只需将相应的DEFINE_SAMPLE_CLASS(...)代码行添加到该头文件即可。

答案 2 :(得分:0)

在评论中回答您的问题:(有关外部使用的详细讨论,请阅读 HERE 。)

声明这样的结构,然后extern'ing它的副本(或任何变量)允许它包含相同的值,并在使用它的所有文件中看到相同的值。

在.h

typedef struct {
    int a;
    char *b;
}A: 
//A has just been created as a new type, i.e. struct A, so it now conforms to the same rules other variable conform to with regards to the use of extern.  So...  

extern A a, *pA;//create a global copy of A with extern modifier  

然后在file1.c

A a, *pA;  //copy of externed variable 
int main(void)
{
    pA = &a;//initialize pA here, use everywhere in file;
    return 0;
}  

然后在file2.c

A a, *pA;

[编辑]我的原始答案没有动摇,并且确实回答了OP的一个非常具体的问题:澄清我的评论:

执行类似typedef struct {...} A的操作并不容易; extern A a,* pA;在你的头文件中,然后A a,* pA;在每个.c模块中你需要使用它们吗?对于那些以后需要维护代码的人来说,我会更加可读

OP问道:

@ryyker:你能澄清你发布的那个外部例子吗?这让我感到困惑,因为结构在c中没有连接,但所有变量和函数都有。

以上回答了上述要求。 OP接受了答案。此编辑是为了回应@Jim Balter对我的回答有效性的侵略性投诉/主张。新代码已经过清理和测试,以说明使用extern修饰符创建和使用项目可见变量的用法。以下代码示例将演示:

header.h

int somefunc(void);
typedef struct {
    int num;
    char *b;
}A; 
//A has just been created as a new type, i.e. struct A, so it now 
//conforms to the same rules other variable conform to with 
//regards to the use of extern.  So...  

extern A a, *pA;//create a global copy of A with extern modifier    

file1.c

#include <ansi_c.h>
#include "header.h"

A a, *pA;
int main(void)
{
    pA = &a;//initialize pA here, use everywhere in file;
    pA->num = 45;
    pA->b = malloc(strlen("value of pA->b")+1);
    somefunc();
    printf("value of pA->b is: %s\n", pA->b);
    getchar();
    free(pA->b);
    return 0;
}  

<强> file2.c中

#include <ansi_c.h>
#include "header.h"

A a, *pA; //Note: optional presence of A in this file.  
          //It is not Not necessary to place this line, 
          //but for readability (future maintainers of code
          //I always include it in each module in which it is used. 
int somefunc(void)
{
    printf("%d\n", pA->num);//value of pN->num same as in file1.c 
    strcpy(pA->b, "value of pA->b");
    return 0;
}

构建和运行这些文件将演示创建的A结构的项目可见性 header.h,并在file1.c和file2.c中使用(以及需要的.c文件) 它也证实了我在原始答案中提出的主张。

相关问题