1

SOLVED, I found a solution to declare variable through macro and macro syntax,

the declare.c is generate by other tools, so I can't modify it.

[I_VAR.c]

#define _DECLARE_VAR_I TYPE NAME;
#define _DECLARE_VAR_U
#define DECLARE_VAR(TYPE, NAME, CLASS) \
_DECLARE_VAR_##CLASS(TYPE, NAME)
#include "declare.h"
#undef _DECLARE_VAR_I
#undef _DECLARE_VAR_U

[U_VAR.c]

#define _DECLARE_VAR_I
#define _DECLARE_VAR_U TYPE NAME;
#define DECLARE_VAR(TYPE, NAME, CLASS) \
_DECLARE_VAR_##CLASS(TYPE, NAME)
#include "declare.h"
#undef _DECLARE_VAR_I
#undef _DECLARE_VAR_U

I have a macro to declare global variable, and the macro has a syntax call "class" for separate those declaration to different C file, but the preprocessor show me an error, because I use "ifdef" in macro.

does any idea? thanks!

(1)macro

#define DECLARE_VAR(TYPE, NAME, CLASS) \
#ifdef CLASS \
TYPE NAME; \
#endif

(2) [declare.h] generate by tools

DECLARE_VAR(int, i0, I)
DECLARE_VAR(int, i1, I)
DECLARE_VAR(int, i2, I)
DECLARE_VAR(int, u0, U)
DECLARE_VAR(int, u1, U)
DECLARE_VAR(int, u2, U)

(3) [I_Var.c]

#define I
#include "declare.h"

[U_Var.c]

#define U
#include "declare.h"

it's possble to separate variable to different file through macro parameter?

Owen
  • 41
  • 7
  • I don't see the point of your macro. What is your goal? – schorsch312 Oct 31 '19 at 07:39
  • for declare specified variable in different file through macro parameter CLASS – Owen Oct 31 '19 at 07:46
  • Perhaps you should describe what you want to achieve. There might be a simple and straightforward answer to your _actual_ problem. I have the feeling that the inclusion of `declare.h` and the macros obscure your real intension. – M Oehm Oct 31 '19 at 08:28

2 Answers2

1

You want to conditionally define stuff. Don't make the parameter that has to be defined an argument of the macro. Just use straightforward #ifdefs, which makes it immediately clear¹ which block is defined.

#ifdef I
    int i0;
    int i1;
    int i2;
#end

#ifdef U
    unsigned int u0;
    unsigned int u1;
    unsigned int u2;
#endif

Of course, now you have to find a way to make the rest of the code distiguish between i0 and u0. Perhaps you want to define variables of the same type, but not of the same names. Pehaps you don't want #ifdef here, but a defined type:

#define M_TYPE
#include "declare.h"

and in declare.h:

#ifndef M_TYPE
#error "Forgot to define M_TYPE"
#end

M_TYPE i0;
M_TYPE i1;
M_TYPE i2;

Here, you cannot include declare.h twice from the same *.c file with different types.

________
¹ in this short example, anyway.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • I need, if define the class like "#define U" in the file top, then the include "declare.h" "U" part is effect and "I" part is not effect. thanks! – Owen Oct 31 '19 at 07:32
  • Doesn't the code above do that, only with `CLASS` instead of `U`? – M Oehm Oct 31 '19 at 07:38
  • in above case, the macro parameter CLASS mean "I" or "U". but the macro ifdef is illegal – Owen Oct 31 '19 at 07:48
  • Okay, now I see. I've rewritten my answer. (I guess your example is just made up, otherwise I'm not sure why you want to declare three variables of the same type with another name.) – M Oehm Oct 31 '19 at 07:55
  • the post is updated, thanks for the answer – Owen Nov 01 '19 at 03:31
1

There's no need to use a macro(), just do:

#ifdef I
    int i0;
    ...
#end

#ifdef U
    int u2;
    ...
#endif

Be aware that use of macro's and conditional compilation obscures things; so only use this when really necessary.

I have my doubts about putting both sets of definitions in the same declare.h. Please consider having a declareU.h and a declareI.h and simple #include either one in your .c files.

Then, your declare.h is misnamed because you're not declaring variables but instead are defining them. It's bad practice to define variables in a header file because you'll end up with multiple instances of them in your program if you include the header in multiple source files.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142