简体   繁体   English

如何在ANSI C中链接模板头?

[英]how to link template header in ANSI C?

I made a template list with help of macroses. 我借助宏创建了一个模板列表。 And I have an error, when I use it in more then one time in the code. 当我在代码中多次使用它时,出现了一个错误。 There is a link error LNC2005 in MS VS. MS VS中存在链接错误LNC2005。 I think, it happens, because bodies of functions are in the header, is there another way to keep them? 我认为发生这种情况是因为函数主体在标头中,还有另一种方法可以保留它们吗?

#define  GENERIC_LIST_POSTFIX  i
#define  GENERIC_LIST_TYPE     int
#define  GENERIC_LIST_NAME     list

#include "generic_list.h"

#undef  GENERIC_LIST_POSTFIX
#undef  GENERIC_LIST_TYPE   
#undef  GENERIC_LIST_NAME  

If I can't change a language, what can You advice to me? 如果我无法更改语言,您能给我什么建议? Thanks. 谢谢。

There is my code 有我的密码

#ifndef _GENERIC_LIST_H
#define _GENERIC_LIST_H

#define _CAT(x,y)    x##y
#define  CAT(x,y)  _CAT(x,y)

#if !defined GENERIC_LIST_POSTFIX 
#   error("GENERIC_LIST_POSTFIX")
#endif

#if !defined GENERIC_LIST_TYPE
#   error("GENERIC_LIST_TYPE")
#endif
#if !defined GENERIC_LIST_NAME 
#   error("GENERIC_LIST_NAME")
#endif
//-------------------------------------------------------------------------------
typedef struct CAT(CAT(_list_,GENERIC_LIST_POSTFIX),_node) CAT(GENERIC_LIST_NAME,_node);
struct CAT(CAT(_list_,GENERIC_LIST_POSTFIX),_node)
{ GENERIC_LIST_TYPE value;
  struct CAT(CAT(_list_,GENERIC_LIST_POSTFIX),_node) *prev;
  struct CAT(CAT(_list_,GENERIC_LIST_POSTFIX),_node) *next;
};

//typedef struct CAT(_list_,GENERIC_LIST_POSTFIX) GENERIC_LIST_NAME;
struct CAT(_list_,GENERIC_LIST_POSTFIX)
{ unsigned int len; // number of elements
  struct CAT(CAT(_list_,GENERIC_LIST_POSTFIX),_node) *first;
  struct CAT(CAT(_list_,GENERIC_LIST_POSTFIX),_node) *last;
};
//-------------------------------------------------------------------------------
void   CAT(CAT(list_,GENERIC_LIST_POSTFIX),_create )
(struct CAT(_list_,GENERIC_LIST_POSTFIX) *List);
{ List->len   = 0; List->first = NULL; List->last  = NULL; }

void   CAT(CAT(list_,GENERIC_LIST_POSTFIX),_copy   )
(struct CAT(_list_,GENERIC_LIST_POSTFIX) *scr, struct CAT(_list_,GENERIC_LIST_POSTFIX) *dest);
{ // ... }
// ... there are more code
#endif

All works, but there is another problem. 一切正常,但是还有另一个问题。 I can use this .h file only one time in a one .c file. 我只能在一个.c文件中使用此.h文件一次。 If I define GENERIC_LIST_TYPE firstly as int, than as int*, for example. 例如,如果我首先将GENERIC_LIST_TYPE定义为int,则将其定义为int *。

#define  GENERIC_LIST_POSTFIX  i
#define  GENERIC_LIST_TYPE     int
#define  GENERIC_LIST_NAME     list_i

#include "generic_list.h"

#undef   GENERIC_LIST_POSTFIX
#undef   GENERIC_LIST_TYPE   
#undef   GENERIC_LIST_NAME  


#define  GENERIC_LIST_POSTFIX  pi
#define  GENERIC_LIST_TYPE     int*
#define  GENERIC_LIST_NAME     list_pi

#include "generic_list.h"

#undef   GENERIC_LIST_POSTFIX
#undef   GENERIC_LIST_TYPE   
#undef   GENERIC_LIST_NAME 

I don't get 2 lists with list_i and list_pi names. 我没有2个具有list_i和list_pi名称的列表。 The second "list_pi" is "undeclared identifier". 第二个“ list_pi”是“未声明的标识符”。 Is there a solution for this? 有解决方案吗? Thank you twice. 谢谢你两次

If I read your question right, you have a header like this: 如果我没看错你的问题,那么你会有这样的标题:

void doSomething()
{
    printf("doing something");
}

Which causes link errors when you include the file multiple times. 当您多次包含文件时,这会导致链接错误。 However, if you make the function static: 但是,如果将函数设为静态:

static void doSomething()
{
    printf("doing something");
}

The method will not be put to the linker, so you won't have to worry about linker errors. 该方法将不会放在链接器中,因此您不必担心链接器错误。

Your other option is to put a method declaration in your header, and an implementation in a .c file elsewhere in your project. 您的另一种选择是将方法声明放在标头中,并将实现放在项目中其他位置的.c文件中。

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

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