【问题标题】:C macro issue: redefinition of functions / structureC宏问题:重新定义函数/结构
【发布时间】:2010-06-08 21:28:00
【问题描述】:

给定以下代码(它是一个宏,根据包含的类型为列表数据结构生成代码)。

list.h

#ifndef _LIST_H
#define _LIST_H

#ifdef __cplusplus
extern "C" {
#endif

#define LIST_TEMPLATE_INIT(type) \
    typedef struct __list_s_##type { \
        struct __list_s_##type *next; \
        type value; \
    } __list_##type; \
\
    __list_##type * __list_##type##_malloc(type value){ \
        __list_##type * list = NULL; \
        list = malloc(sizeof(*list)); \
        list->value = value; \
        return list; \
    }\
\
    void __list_##type##_free(__list_##type *list){\
        __list_##type * back = list;\
        while(list=list->next){\
            free(back);\
            back = list;\
        }\
    }
#define LIST_TYPE(type) __list_##type
#define LIST_MALLOC(type,value) __list_##type##_malloc(value)
#define LIST_FREE(type,list) __list_##type##_free(list)
#define LIST_DATA(list) (list->value)

#ifdef __cplusplus
}
#endif

#endif /* _LIST_H */

上面的代码是这样工作的:

#include <stdio.h>
#include <stdlib.h>
#include "list.h"

/*
 * 
 */
LIST_TEMPLATE_INIT(int)
int main(int argc, char** argv)
{
 LIST_TYPE(int)* list = NULL;
 list = LIST_MALLOC(int, 5);
 printf("%d",LIST_DATA(list));
 LIST_FREE(int,list);
 return (0);
}

我的问题,是否有可能以分散的方式以某种方式调用:LIST_TEMPLATE_INIT(int),我想多少次都可以?

目前的问题是在另一个文件中调用LIST_TEMPLATE_INIT(int) 会引发编译错误(因为函数重定义):

错误示例:

error: redefinition of ‘struct __list_s_int’
 error: redefinition of ‘struct __list_s_int’
error: conflicting types for ‘__list_int’
note: previous declaration of ‘__list_int’ was here
error: conflicting types for ‘__list_int_malloc’
note: previous definition of ‘__list_int_malloc’ was here
error: conflicting types for ‘__list_int_free’
note: previous definition of ‘__list_int_free’ was here

【问题讨论】:

  • extern "C" 是对宏定义的多余包装
  • 由 IDE 生成。感谢您的评论,我会调查的!

标签: c macros c-preprocessor


【解决方案1】:

我建议创建不同的宏来声明和定义列表结构,然后为每个使用单独的头文件和源文件:

list.h

#ifndef _LIST_H    
#define _LIST_H    

#define LIST_TEMPLATE_DECLARE(type)                   \
    struct __list_##type;                             \
    typedef struct __list_##type __list_##type;       \
    struct __list_##type {                            \
        struct __list_##type * next;                  \
        type value;                                   \
    };                                                \
                                                      \
__list_##type * __list_##type##_malloc(type value);   \
void __list_##type##_free(__list_##type * list);

#define LIST_TEMPLATE_DEFINE(type)                    \
__list_##type * __list_##type##_malloc(type value) {  \
    __list_##type * list = NULL;                      \
    list = malloc(sizeof(*list));                     \
    list->value = value;                              \
    return list;                                      \
}                                                     \
void __list_##type##_free(__list_##type * list) {     \
    __list_##type * back = list;                      \
    while(list=list->next){                           \
        free(back);                                   \
        back = list;                                  \
    }                                                 \
}

#define LIST_TYPE(type) __list_##type    
#define LIST_MALLOC(type,value) __list_##type##_malloc(value)    
#define LIST_FREE(type,list) __list_##type##_free(list)    
#define LIST_DATA(list) (list->value)    

#endif /* _LIST_H */    

int_list.h

#ifndef INT_LIST_H_
#define INT_LIST_H_

#include "list.h"
LIST_TEMPLATE_DECLARE(int)

#endif /* INT_LIST_H_ */

int_list.c

#include "int_list.h"

LIST_TEMPLATE_DEFINE(int)

other.c

#include "int_list.h"

int some_function(int argc, char** argv)
{
    LIST_TYPE(int)* list = NULL;
    list = LIST_MALLOC(int, 5);
    printf("%d",LIST_DATA(list));
    LIST_FREE(int,list);
    return (0);
}

【讨论】:

  • extern C 是对宏定义的冗余包装 - 应移至 init_list.h 或适当添加到宏本身
【解决方案2】:

使底层结构类型匿名。这将允许您根据自己的意愿定义任意数量:

#define LIST_TEMPLATE_INIT(type) \
    typedef struct { \
        struct __list_s_##type *next; \
        type value; \
    } __list_##type; \

【讨论】:

  • 你能说得更具体些吗?你确定要上班吗?问题不仅出在结构上,还出在功能上。
  • 这不起作用,因为next 指针必须与列表结构的类型相同。
猜你喜欢
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
相关资源
最近更新 更多