Reputation: 1374
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. 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. If I define GENERIC_LIST_TYPE firstly as int, than as int*, for example.
#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. The second "list_pi" is "undeclared identifier". Is there a solution for this? Thank you twice.
Upvotes: 0
Views: 179
Reputation: 55543
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.
Upvotes: 1