C代码宏问题:功能/结构的重新定义 [英] C macro issue: redefinition of functions / structure

查看:231
本文介绍了C代码宏问题:功能/结构的重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code(它是产生code为列表数据结构的宏观基础上,包含的类型)。

list.h

 的#ifndef _LIST_H
#定义_LIST_H#IFDEF __cplusplus
为externC{
#万一#定义LIST_TEMPLATE_INIT(类型)\\
    typedef结构__list_s _ ##型{\\
        结构__list_s _ ##型*接下来的; \\
        类型值; \\
    } __list _ ##型; \\
\\
    __list _ ##型* __list _ ##型## _的malloc(类型值){\\
        __list _ ##型*表= NULL; \\
        表=的malloc(sizeof的(*列表)); \\
        列表 - >值=价值; \\
        返回列表; \\
    } \\
\\
    虚空__list _ ##型## _免费(__ _列表式## *名单){\\
        __list _ ##型*回来=清单; \\
        而(名单=列表 - >接下来){\\
            免费(回); \\
            回到=清单; \\
        } \\
    }
#定义LIST_TYPE(类型)__list _ ##型
#定义LIST_MALLOC(类型,值)__list _ ##型## _的malloc(值)
#定义LIST_FREE(类型,列表)__list _ ##型## _免费(名单)
#定义LIST_DATA(名单)(列表 - >值)#IFDEF __cplusplus
}
#万一#ENDIF / * * _LIST_H /

这是怎么了超过code工作:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括list.h/ *
 *
 * /
LIST_TEMPLATE_INIT(INT)
INT主(INT ARGC,字符** argv的)
{
 LIST_TYPE(INT)*名单= NULL;
 清单= LIST_MALLOC(INT,5);
 的printf(%d个,LIST_DATA(列表));
 LIST_FREE(INT,清单);
 返回(0);
}

我的问题,是有可能以某种方式能够调用: LIST_TEMPLATE_INIT(INT),很多次我想,在一个分散的方式?

现在有这个权利目前的问题是,调用 LIST_TEMPLATE_INIT(INT)(因为重新定义函数)的另一个文件加薪编译错误:

错误示例:

 错误:'结构__list_s_int的重新定义
 错误:'结构__list_s_int的重新定义
错误:冲突的类型'__list_int
注:__list_int'previous声明在这里
错误:冲突的类型'__list_int_malloc
注:__list_int_malloc'previous定义在这里
错误:冲突的类型'__list_int_free
注:__list_int_free'previous定义在这里


解决方案

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


  

list.h


 的#ifndef _LIST_H
#定义_LIST_H#定义LIST_TEMPLATE_DECLARE(类型)\\
    结构__list _ ##型; \\
    typedef结构__list _ ##型__list _ ##型; \\
    结构__list _ ##型{\\
        结构__list _ ##型*接下来的; \\
        类型值; \\
    }; \\
                                                      \\
__list _ ##型* __list _ ##型## _的malloc(类型值); \\
虚空__list _ ##型## _免费(__ _列表式## *清单);#定义LIST_TEMPLATE_DEFINE(类型)\\
__list _ ##型* __list _ ##型## _的malloc(类型值){\\
    __list _ ##型*表= NULL; \\
    表=的malloc(sizeof的(*列表)); \\
    列表 - >值=价值; \\
    返回列表; \\
} \\
虚空__list _ ##型## _免费(__ _列表式## *名单){\\
    __list _ ##型*回来=清单; \\
    而(名单=列表 - >接下来){\\
        免费(回); \\
        回到=清单; \\
    } \\
}#定义LIST_TYPE(类型)__list _ ##型
#定义LIST_MALLOC(类型,值)__list _ ##型## _的malloc(值)
#定义LIST_FREE(类型,列表)__list _ ##型## _免费(名单)
#定义LIST_DATA(名单)(列表 - >值)#ENDIF / * * _LIST_H /


  

int_list.h


 的#ifndef INT_LIST_H_
#定义INT_LIST_H_#包括list.h
LIST_TEMPLATE_DECLARE(INT)#ENDIF / * * INT_LIST_H_ /


  

int_list.c


 的#includeint_list.hLIST_TEMPLATE_DEFINE(INT)


  

other.c


 的#includeint_list.hINT some_function(INT ARGC,字符** argv的)
{
    LIST_TYPE(INT)*名单= NULL;
    清单= LIST_MALLOC(INT,5);
    的printf(%d个,LIST_DATA(列表));
    LIST_FREE(INT,清单);
    返回(0);
}

Given the following code (it's a macro that generates code for a list data structure, based on the contained type).

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 */

And here is how the above code works:

#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);
}

My question, is it possible to somehow be able to call : LIST_TEMPLATE_INIT(int), as many times as I want, in a decentralized fashion ?

The current issue with this right now is that calling LIST_TEMPLATE_INIT(int) in another file raise compilation errors (because of function redefinition):

Example of error:

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

解决方案

I would suggest creating different macros to declare and define the list structure, then using separate header and source files for each:

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);
}

这篇关于C代码宏问题:功能/结构的重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆