What are some tricks I can use with macros? [closed]

In C, it’s common to define macros that do some stuff getting the verbatim argument, and at the same time define functions to be able to get the address of it transparently. // could evaluate at compile time if __builtin_sin gets // special treatment by the compiler #define sin(x) __builtin_sin(x) // parentheses avoid substitution by … Read more

Scope of macros in C?

C Preprocessor works top to bottom irrespective of function calls. It is effective from that point (line) in whatever the file that macro is defined, until corresponding undef or till end of the translation unit. So, your code would become, # define i 20 // from now on, all token i should become 20 void … Read more

Why __func__, __FUNCTION__ and __PRETTY_FUNCTION__ aren’t preprocessor macros?

Expanding __func__ at preprocessing time requires the preprocessor to know which function it’s processing. The preprocessor generally doesn’t know that, because parsing happens after the preprocessor is already done. Some implementations combine the preprocessing and the parsing, and in those implementations, it would have been possible for __func__ to work the way you’d like it … Read more

Define a preprocessor macro through CMake

For a long time, CMake had the add_definitions command for this purpose. However, recently the command has been superseded by a more fine grained approach (separate commands for compile definitions, include directories, and compiler options). An example using the new add_compile_definitions: add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION}) add_compile_definitions(WITH_OPENCV2) Or: add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2) The good part about this is that it circumvents … Read more

What do the numbers mean in the preprocessed .i files when compiling C with gcc?

The numbers following the filename are flags: 1: This indicates the start of a new file. 2: This indicates returning to a file (after having included another file). 3: This indicates that the following text comes from a system header file, so certain warnings should be suppressed. 4: This indicates that the following text should … Read more

Detecting Endianness

As stated earlier, the only “real” way to detect Big Endian is to use runtime tests. However, sometimes, a macro might be preferred. Unfortunately, I’ve not found a single “test” to detect this situation, rather a collection of them. For example, GCC recommends : __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ . However, this only works with latest versions, … Read more

tech