When should I quote CMake variable references?

Two principles of CMake you have to keep in mind: CMake is a script language and arguments are evaluated after the variables are expanded CMake differentiates between normal strings and list variables (strings with semicolon delimiters) Examples set(_my_text “A B C”) with message(“${_my_text}”) would give A B C set(_my_list A B C) with message(“${_my_list}”) would … Read more

How to view C preprocessor output?

gcc -E file.c or g++ -E file.cpp will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output. Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output. … Read more

Why should I avoid macros in C++? [closed]

Macros don’t respect scoping rules and operate at the textual level, as opposed to the syntax level. From this arise a number of pitfalls that can lead to strange, difficult to isolate bugs. Consider the following well-known example: #define max(a, b) ((a) < (b) ? (b) : (a)) ⋮ int i = max(i++, j++); The … Read more

Macro expansion and stringification: How to get the macro name (not its value) stringified using another macro?

(The standard disclaimer about not abusing the C preprocessor without a really good reason applies here.) It’s certainly possible to do what you want to do. You need a STRINGIFY macro and a bit of macro indirection. Typically, STRINGIFY is defined with one level of indirection, to allow the C preprocessor to expand its arguments … Read more

Macro and function with same name

Use () to stop the preprocessor from expanding the function definition: #include <stdio.h> #define myfunc(a, b) myfunc(do_a(a), do_b(b)) /* if you have a preprocessor that may be non-standard * and enter a loop for the previous definition, define * myfunc with an extra set of parenthesis: #define myfunc(a, b) (myfunc)(do_a(a), do_b(b)) ******** */ int (myfunc)(int … Read more

Is there a way to use C++ preprocessor stringification on variadic macro arguments?

You can use various recursive macro techniques to do things with variadic macros. For example, you can define a NUM_ARGS macro that counts the number of arguments to a variadic macro: #define _NUM_ARGS(X100, X99, X98, X97, X96, X95, X94, X93, X92, X91, X90, X89, X88, X87, X86, X85, X84, X83, X82, X81, X80, X79, X78, … Read more

tech