How to Do a #Define Inside of Another #Define

Is it possible to use #define inside a function?

#define is a preprocessor directive: it is used to generate the eventual C++ code before it is handled to the compiler that will generate an executable. Therefore code like:

for(int i = 0; i < 54; i++) {
#define BUFFER_SIZE 1024
}

is not executed 54 times (at the preprocessor level): the preprocessor simply runs over the for loop (not knowing what a for loop is), sees a define statement, associates 1024 with BUFFER_SIZE and continues. Until it reaches the bottom of the file.

You can write #define everywhere since the preprocessor is not really aware of the program itself.

Accessing declared variables inside a #define from another #define

You can't get at that struct from another piece of code because you have declared it inside a braced block. C scope rules prohibit access to code outside that block. That's true even if you get your macro to generate correct name: __counter_info_F instead of __counter_info_FOO.

This isn't a #define issue, by the way. By the time C sees the code, all macro processing is done and the compiler only sees the tokens produced by the preprocessor.

Find a way to get the job done without macros...and THEN figure out a macro representation to ease the coding.

One way would be to use macros that don't use the do loops to seal off those variables. The problem now is to generate unique names. Look carefully at those macros and you'll see that that's the problem that the one-time-loops were created to solve...duplicate names creating double definitions. If you unwrap the macro generated code, you'll have to solve the duplicate name problem in another way...probably by adding macro arguments.

One thing, for the record: You don't need a compound statement like do or for to contain a plain block. Just enclosing in braces is good enough. You can take off the "do" at the beginning and the "while (0);" at the end, and get the same effect.

Using a previously defined #define in a new #define in C

A #define is handled by the pre-processor. The pre-processor is run prior to compilation and can perform simple mathematical operations and copy/paste of code. For instance, you could do the following with your example:

int myVar = SAMPLERATE;

The pre-processor would simply paste 32 where SAMPLERATE is before being compiled.

This mechanism is powerful in the sense that you have now created a name for an integer value. This adds meaning for both you and future developers. It also allows you to make changes in one place instead of many.

Just be sure to #define SAMPLERATE 32 prior to any other #define statements that may use SAMPLERATE.

#ifdef inside #define

Not possible. Do it the other way around:

#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x)
#endif

Is there a way to define a preprocessor macro that includes preprocessor directives?

You cannot define preprocessing directives the way you show in the question.

Yet you may be able to use the _Pragma operator for your purpose:

#if defined __GNUC__ && __GNUC__ >= 8
#define foo _Pragma("GCC unroll 128") _Pragma("GCC ivdep")
#elif defined __clang__
#define foo _Pragma("clang loop vectorize(enable) interleave(enable)")
#else
#define foo
#endif


Related Topics



Leave a reply



Submit