天道酬勤,学无止境

stringification

Stringify first level macro expansion C

Is it possible to stringify this C macro: #define GPIO_INT_PIN (GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5) using something like MY_STRINGFY(GPIO_INT_PIN) to get "(GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5)" ?

2021-06-13 22:03:00    分类:问答    c   macros   c-preprocessor   stringification

如何对包含逗号的字符串进行字符串化?(How to stringify a string which contains a comma?)

问题 我想在编译命令中传递一个版本字符串: $ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp 在我的代码中,我有以下内容: #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(x) #define VERSION_STR STRINGIFY(VERSION) 这不起作用,因为VERSION宏中有一个逗号,所以看起来我将两个参数传递给TOSTR() (显然, VERSION宏仅在作为一个唯一参数传递给STRINGIFY()后才会扩展STRINGIFY() )。 我在这里找到的以下方法也不起作用: #define CONCAT(...) __VA_ARGS__ #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(CONCAT(x)) #define VERSION_STR STRINGIFY(VERSION) 因为这似乎与 #define VERSIONSTR "CONCAT(2013-12-03 02:15:21, commit cb060df)" 也就是说,宏CONCAT()不会被扩展。 注 1:我宁愿不在命令行中传递C字符串,因为版本字符串实际上是动态生成的,并且它可能包含一些引号。

2021-06-11 06:41:56    分类:技术分享    c++   c-preprocessor   stringification   variadic-macros

How to add a modifier to a quoted regular (qr) expression

Is there an easy way to add regex modifiers such as 'i' to a quoted regular expression? For example: $pat = qr/F(o+)B(a+)r/; $newpat = $pat . 'i'; # This doesn't work The only way I can think of is to print "$pat\n" and get back (?-xism:F(o+)B(a+)r) and try to remove the 'i' in ?-xism: with a substitution

2021-06-09 10:13:34    分类:问答    regex   perl   modifier   stringification   qr-operator

How does this C code work?

What is a##b & #a? #define f(a,b) a##b #define g(a) #a #define h(a) g(a) main() { printf("%s\n",h(f(1,2))); //how should I interpret this?? [line 1] printf("%s\n",g(f(1,2))); //and this? [line 2] } How does this program work? The output is 12 f(1, 2) now I understand how a##b & #a work. But why is the result different in the two cases (line 1 and line 2)?

2021-06-02 17:05:33    分类:问答    c   c-preprocessor   stringification

Error when defining a stringising macro with __VA_ARGS__

I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in gcc: src/include/debug.h:4:70: error: expected expression before ‘)’ token #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) ^ Supposedly, it should stringise format, and pass its variable arguments to printf, but so far I can't get past this error. EDIT After giving up on stringising arguments, and double-hashing (##) __VA_ARGS__ I now have this

2021-06-01 19:46:49    分类:问答    c   macros   c-preprocessor   variadic-macros   stringification

C 预处理器通过连接和字符串化生成宏 [重复](C Preprocessor generate macros by concatenation and stringification [duplicate])

问题 这个问题已经在这里有了答案: ## 预处理器运算符的应用和需要考虑的问题是什么? (13 个回答) 6年前关闭。 我有一组目标宏,我想根据选择宏为其生成别名,如下所示: 选择宏: #define I2C_MODULE 1 别名宏(概念形式): #define I2C_MODULE_BASE I2C<Value of I2C_MODULE>_BASE #define I2C_MODULE_NVIC INT_I2C<Value of I2C_MODULE> 目标宏(来自我无法控制的外部文件): #define INT_I2C0 24 #define INT_I2C1 53 ... #define I2C0_BASE 0x40020000 #define I2C1_BASE 0x40021000 ... 我想让预处理器根据选择的宏I2C_MODULE生成别名宏I2C_MODULE_BASE和I2C_MODULE_NVIC ,但是在阅读了很多 Q1、P1 和许多其他我忘记的参考资料后,我最终硬编码了它们的值。 下面我展示了我当前的工作定义,然后是我最后一次失败的生成宏的尝试: 什么工作: #define I2C_MODULE 1 #define I2C_MODULE_BASE I2C1_BASE #define I2C_MODULE_NVIC INT_I2C1 什么没有奏效:

2021-06-01 08:38:38    分类:技术分享    c   concatenation   c-preprocessor   stringification

How to stringify a string which contains a comma?

I want to pass a version string in the compile command: $ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp Inside my code, I have the following: #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(x) #define VERSION_STR STRINGIFY(VERSION) This doesn't work, because the VERSION macro has a comma in it, so it looks like I'm passing two arguments to TOSTR() (apparently, the VERSION macro only gets expanded after it's passed to STRINGIFY() as one unique argument). The following approach I found in here also doesn't work: #define CONCAT(...) __VA_ARGS__ #define TOSTR_(x) #x

2021-05-31 19:19:49    分类:问答    c++   c-preprocessor   stringification   variadic-macros

解释C预处理程序代码(Interpreting C pre pre processor code)

问题 给定#define LOG_OBJECT(object) (NSLog(@"" #object @" %@ %@:%d”, [object description], [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__));语句LOG_OBJECT(self);将产生: 2014-07-18 17:43:30.041 FrogCamera [12484:2144843] self ViewController.m:20 我希望了解预处理器代码的工作方式。 我如何看到预处理器生成的语句? 具体来说: 为什么整个#define语句都包含在( ) ? #object是否#object替代提供的值? 为什么在#object之前需要@"" ? 即@“” #object @“ %@ %@:%d”转换为@"self %@ %@:%d" ? 这是一个用法示例: @interface ViewController () #define LOG_OBJECT(object) (NSLog(@"" #object @" %@ %@:%d", [object description], [[NSString stringWithUTF8String:__FILE__] lastPathComponent], _

2021-05-31 12:49:40    分类:技术分享    objective-c   c-preprocessor   stringification

Preprocessor tomfoolery (stringifying a #include)

Note: This question has nothing to do with OpenCL per se... check the last paragraph for a succinct statement of my question. But to provide some background: I'm writing some C++ code that makes use of OpenCL. I like to keep the source for my OpenCL kernels in their own files, to keep coding and maintenance easy (as opposed to embedding the sources directly as string constants in associated C++ code). This inevitably leads to the question of how to load them into the OpenCL runtime once it comes time to distribute binaries---ideally, the OpenCL source is included in the binary, so that the

2021-05-24 14:24:10    分类:问答    c++   include   opencl   c-preprocessor   stringification

编译器通过此宏看到了什么? [关闭](What is the compiler seeing with this macro? [closed])

问题 在这里很难说出要问什么。 这个问题是模棱两可,含糊不清,不完整,过于宽泛或夸张的,不能以目前的形式合理地回答。 如需帮助澄清此问题以便可以重新打开,请访问帮助中心。 8年前关闭。 考虑一下: #define STRINGIFY(A) #A 如果我以后再写: STRINGIFY(hello) 编译器是否实际看到了这一点: #hello 我认为是#A前面的其他哈希值使我感到困惑。 回答1 不,编译器会将参数放在引号之间,结果是: "hello" 但是,请注意,宏替换不会在#和##附近发生,因此,如果您确实需要对参数进行字符串化(即使它是另一个宏),则最好编写两个宏,第一个宏扩展参数,而另一个宏一个加引号: #define STRINGIFY(x) STRINGIFY_AUX(x) #define STRINGIFY_AUX(x) #x 或更笼统地说,如果您的编译器支持可变参数宏(在C99中引入): #define STRINGIFY(...) STRINGIFY_AUX(__VA_ARGS__) #define STRINGIFY_AUX(...) #__VA_ARGS__ 如果您确实需要一个在函数的开头粘贴#的类似函数的宏,我认为您需要它来生成一个字符串(否则您将为编译器创建一个无效的令牌),因此您可以简单地生成两个字符串编译器将“粘贴”的文字: #include <stdio

2021-05-18 00:19:29    分类:技术分享    c-preprocessor   stringification