[英]GNU g++ preprocessor/compiler: printf value
Hi im looking for a solution for GCC to printf a value which is calculated during compilation.嗨,我正在寻找 GCC 的解决方案来打印在编译期间计算的值。
There is message pragmas but they can only print a user input string.有消息编译指示,但它们只能打印用户输入字符串。 what im looking for is a printf style output where i can input parameters.我正在寻找的是 printf 风格的输出,我可以在其中输入参数。
example例子
printf("hi %s, my value is %d\n", "john", 15);
example 2: searching solution for this示例 2:为此搜索解决方案
void dummy(MyObjectReference & obj)
{
#if(sizeof(obj) != 512)
#pragma message "cannot build, your object size is not 512, it is %d", sizeof(obj)
#error "stop build"
#endif
obj.do_stuff();
return obj.get_result();
}
Hi im looking for a solution for GCC to printf a value which is calculated during compilation.嗨,我正在寻找 GCC 的解决方案来打印在编译期间计算的值。
You cannot do that with a standard GCC 9 .你不能用标准的GCC 9做到这一点。
You could consider writing your own GCC plugin providing, for example, some additional #pragma
(or GCC builtin) doing what you want.您可以考虑编写自己的GCC 插件,例如,提供一些额外的#pragma
(或 GCC 内置)来执行您想要的操作。
However, developing such a plugin might take you several weeks of efforts.但是,开发这样的插件可能需要您花费数周的时间。 You'll need to understand GCC internals to code that plugin.您需要了解 GCC 内部结构才能编写该插件。 So look into GCC resource center.因此,请查看GCC 资源中心。
With C++11 or later, you might use static_assert(sizeof(obj) == 512, "bad size of obj")
which works after preprocessing (but won't display sizeof(obj)
as an integer).对于 C++11 或更高版本,您可以使用static_assert(sizeof(obj) == 512, "bad size of obj")
在预处理后工作(但不会将sizeof(obj)
显示为整数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.