Single line font for printing macro value from header

I have a header that defines a large number of macros, some of which depend on other macros, however all dependencies are allowed in this header.

I need a single line font to print the macro value defined in this header.

As an example:

#define MACRO_A 0x60000000 #define MACRO_B MACRO_A + 0x00010000 //... 

As the first blush:

 echo MACRO_B | ${CPREPROCESSOR} --include /path/to/header 

... which almost gives me what I want:

 # A number of lines that are not important # ... 0x60000000 + 0x00010000 

... however, I try to keep it from swelling in a huge sequence of "blow it to this, and then connect it to this ...".

I also tried this:

 echo 'main(){ printf( "0x%X", MACRO_B ); }' \ | ${CPREPROCESSOR} --include /path/to/header --include /usr/include/stdio.h 

... but he (the gcc compiler) complains that -E is required when processing the code on standard input, so I have to write to a temporary file to compile / run this.

Is there a better way?

-Brian

+4
source share
5 answers
 echo 'void main(){ printf( "0x%X", MACRO_B ); }' \ | gcc -xc --include /path/to/header --include /usr/include/stdio.h - && ./a.out 

will do it in one line.

(You are not reading the error that GCC gives when reading from stdin. You need -E or -x (you must specify which language is expected))

Also, it is int main() , or, when you don't care, just drop the return type completely. And you do not need to specify a path for stdio.h .

So a little shorter:

 echo 'main(){printf("0x%X",MACRO_B);}' \ | gcc -xc --include /path/to/header --include stdio.h - && ./a.out 
+4
source

What about tail -n1 ? Like this:

 $ echo C_IRUSR | cpp --include /usr/include/cpio.h | tail -n 1 000400 
+1
source

What about artificially generating an error that contains your MACRO_B value in it, and then compiling the code?

0
source

I think the easiest way would be to write a small C program, include a title in it, and print the desired result. Then you can use it in your script, makefile or something else.

0
source
 echo '"EOF" EOF' | cpp --include /usr/include/stdio.h | grep EOF 

prints:

 "EOF" (-1) 
0
source

All Articles