简体   繁体   中英

GNU g++ preprocessor/compiler: printf value

Hi im looking for a solution for GCC to printf a value which is calculated during compilation.

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.

example

printf("hi %s, my value is %d\n", "john", 15);

example 2: searching solution for this

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.

You cannot do that with a standard GCC 9 .

You could consider writing your own GCC plugin providing, for example, some additional #pragma (or GCC builtin) doing what you want.

However, developing such a plugin might take you several weeks of efforts. You'll need to understand GCC internals to code that plugin. So look into GCC resource center.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM