c-preprocessor

Nested macro iteration with C preprocessor

我是研究僧i 提交于 2019-12-10 13:14:29
问题 With the C preprocessor you can have some kind of high-order macros. Something like this: #define ABC(f) f(a) f(b) f(c) #define XY(f) f(x) f(y) #define CODE(x) foo_ ## x ABC(CODE) #undef CODE #define CODE(x) bar_ ## x XY(CODE) #undef CODE The output is: foo_a foo_b foo_c bar_x bar_y Is there some trick to nest such iterations, to do something like this? #define CODE(x) foo_ ## x NEST(ABC, XY, CODE) #undef CODE So the output would be: foo_ax foo_ay foo_bx foo_by foo_cx foo_cy In particular, I

Separate specific #ifdef branches

亡梦爱人 提交于 2019-12-10 13:12:27
问题 In short: I want to generate two different source trees from the current one, based only on one preprocessor macro being defined and another being undefined, with no other changes to the source. If you are interested, here is my story... In the beginning, my code was clean. Then we made a new product, and yea, it was better. But the code saw only the same peripheral devices, so we could keep the same code. Well, almost. There was one little condition that needed to be changed, so I added: #if

When to use include guards?

感情迁移 提交于 2019-12-10 11:08:41
问题 I know that the use of include guards in header files is to prevent something from being defined twice. Using this code sample though, was completely fine: foo.c #include <stdio.h> #include <string.h> #include "bar.h" int main() { printf("%d", strlen("Test String")); somefunc("Some test string..."); return 0; } bar.h #ifndef BAR_H_INCLUDED #define BAR_H_INCLUDED void somefunc(char str[]); #endif bar.c #include <stdio.h> #include <string.h> #include "bar.h" void somefunc(char str[]) { printf(

How to put a warning disable pragma inside a macro gcc

旧巷老猫 提交于 2019-12-10 10:38:48
问题 I need to disable a warning that originates inside the macro '__LOG_W' in following code. To do that, I wrapped this macro inside another macro 'LOG_W' and disabled the warning '-Wold-style-cast' with in that. Then in the code I used the LOG_W instead. However I still get the warning and unable to find out why. Any pointers appreciated. #define LOG_W(expr)\ _Pragma("GCC diagnostic push")\ _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")\ __LOG_W(DEF, UNKNOWN, expr);\ _Pragma("GCC

Compare preprocessor macros for equality

不打扰是莪最后的温柔 提交于 2019-12-10 09:22:07
问题 I have some crude generated header from some .dbc files. Since a few of the messages represent elements from an array the structure is equal and so the generated Macros are equal. Since I fill some array of struct in the code I would like to save effort and use the same macro for all objects, but to ensure the definitions have not changed I would like to test at compile time if the macros are equal. Example: #define GET_PATTERN_01_PATTERNPOINT02Y(buf) (0 \ | (uint16)(-(uint16)((buf[7] >> 6) &

How can I use gcc's -I command to add recursive folders

泪湿孤枕 提交于 2019-12-10 04:13:37
问题 Is there a way to use gcc's -I command and add all the paths to search path by giving a root directory? I'm trying to use :!gcc -E myfile.c to view macro expansions, but myfile.c includes a whole bunch of other header files in different directories, and because I'm executing this command in vim, so I don't want to call a makefile, is there anyway to do this? 回答1: If you are using Apple's GCC (or Clang), then you can use the following approach (which appears to be an extension): the parameter

cpp expansion of macro with no token-string

给你一囗甜甜゛ 提交于 2019-12-10 03:52:08
问题 I am reading on CPP macro expansion and wanted to understand expansion when the (optional) token-string is not provided. I found gcc v4.8.4 does this: $ cat zz.c #define B (B) |B| $ gcc -E zz.c # 1 "zz.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "zz.c" () | | Can anyone explain why the expansion is zero spaces in one instance and one in the other? 回答1: The C preprocessor operates on "tokens" and whenever there's a possibility of changing the meaning or ambiguity, it always adds whitespace in

Valid preprocessor tokens in macro concatenation

…衆ロ難τιáo~ 提交于 2019-12-10 02:33:21
问题 I tried to understand the macros in c using the concatenation preprocessor operator ## but I realized that I have problem with tokens. I thought it was easy but in practice it is not. So the concatenation is for concatenating two tokens to create a new token. ex: concatenating ( and ) or int and * I tried #define foo(x,y) x ## y foo(x,y) whenever I give it some arguments I get always error saying that pasting both argument does not give a valid preprocessor token. For instance why

Compile-time sizeof conditional

我怕爱的太早我们不能终老 提交于 2019-12-10 01:30:39
问题 I want to define a macro if a condition involving sizeof is true and do nothing (but still compile) if it is false. If the preprocessor supported sizeof , it would look like this: #if (sizeof(void*) <= sizeof(unsigned int)) // what goes here? # define POINTER_FITS_INTO_UINT #endif There are some pages (e.g. http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/) which explain how to make a compile-time assertion on sizeof (and fail to compile if it fails), but I don't

Macro overloading

感情迁移 提交于 2019-12-09 19:22:43
问题 Is it possible to define something like this: #define FOO(x, y) BAR() #define FOO(x, sth, y) BAR(sth) so that this: FOO("daf", sfdas); FOO("fdsfs", something, 5); is translated to this: BAR(); BAR(something); ? Edit: Actually, BAR 's are methods of my class. Sorry for not saying that before (didn't think it was relevant). Answering DyP's question: class Testy { public: void TestFunction(std::string one, std::string two, std::string three) { std::cout << one << two << three; } void AnotherOne