Preprocessor and Makefile Tips and Tricks
Feb. 6th, 2020 12:06 pmHere are some interesting articles and tips related to the C preprocessor and to creating makefiles.
This shows how to print the value of a macro:
http://c-faq.com/ansi/stringize.html
How to use static patterns with makefiles:
https://www.gnu.org/software/make/manual/make.html#Static-Pattern
Preprocessor tips:
The second article shows how to check which include files are used by a C or C++ file.
https://mischasan.wordpress.com/2013/04/07/the-c-preprocessor-not-as-cryptic-as-youd-think/
https://mischasan.wordpress.com/2011/06/14/stupid-gcc-trick-2-finding-all-included-files-recursively/
https://mischasan.wordpress.com/2011/10/12/stupid-gcc-trick-3-list-all-built-in-gcc-define-symbols/
Make tips:
The first article has a great tip for creating directories in makfiles using "%/..:".
https://mischasan.wordpress.com/2012/07/05/two-more-cheap-gmake-tricks-creating-directories-and-printing-variables/
https://mischasan.wordpress.com/2015/03/09/gmake-cheap-trick-3/
https://mischasan.wordpress.com/2015/03/21/gmake-cheap-trick-4-for-non-recursive-make/
To see what commands make will run when invoked, you can use:
make --just-print
Nonrecursive makefile tips:
https://mischasan.wordpress.com/2013/03/30/non-recursive-make-gmake-part-1-the-basic-gnumakefile-layouts/
https://mischasan.wordpress.com/2013/03/30/non-recursive-make-gmake-part-2-rules-mk/
https://mischasan.wordpress.com/2013/04/13/non-recursive-make-part-3-a-tool-for-the-fearless/
Replacing other utilities with sed:
In case you need these in a makefile, build script or elsewhere and don't have them on your system:
https://github.com/aureliojargas/sed.sf.net/blob/master/local/docs/emulating_unix.txt
Miscellaneous compiler tips:
To stop the gnu gcc compiler after the first error use:
-Wfatal-errors
To stop the gnu gcc compiler after the N errors use:
-fmax-errors=N
So for three errors add the following to the command line when invoking gcc:
-fmax-errors=3
My favorite preprocessor tips:
You can use a C preprocessor to generate templates.
Here's an example to generate web pages using the preprocessor and templates:
http://www.distasis.com/cpp/mingw.htm#makeprograms
For my build system, I need a more functionality than the standard C preprocessor could offer.
Rather than reinventing the wheel and writing my own preprocessor, I found gpp has just enough capabilities beyond a standard preprocessor to handle the task.
The gpp preprocessor is available from:
https://logological.org/gpp
Know of some other preprocessor or makefile tips? Have written an article or blog post with your tips on these topics? Please share them. They may get included in this list.
This shows how to print the value of a macro:
http://c-faq.com/ansi/stringize.html
How to use static patterns with makefiles:
https://www.gnu.org/software/make/manual/make.html#Static-Pattern
Preprocessor tips:
The second article shows how to check which include files are used by a C or C++ file.
https://mischasan.wordpress.com/2013/04/07/the-c-preprocessor-not-as-cryptic-as-youd-think/
https://mischasan.wordpress.com/2011/06/14/stupid-gcc-trick-2-finding-all-included-files-recursively/
https://mischasan.wordpress.com/2011/10/12/stupid-gcc-trick-3-list-all-built-in-gcc-define-symbols/
Make tips:
The first article has a great tip for creating directories in makfiles using "%/..:".
https://mischasan.wordpress.com/2012/07/05/two-more-cheap-gmake-tricks-creating-directories-and-printing-variables/
https://mischasan.wordpress.com/2015/03/09/gmake-cheap-trick-3/
https://mischasan.wordpress.com/2015/03/21/gmake-cheap-trick-4-for-non-recursive-make/
To see what commands make will run when invoked, you can use:
make --just-print
Nonrecursive makefile tips:
https://mischasan.wordpress.com/2013/03/30/non-recursive-make-gmake-part-1-the-basic-gnumakefile-layouts/
https://mischasan.wordpress.com/2013/03/30/non-recursive-make-gmake-part-2-rules-mk/
https://mischasan.wordpress.com/2013/04/13/non-recursive-make-part-3-a-tool-for-the-fearless/
Replacing other utilities with sed:
In case you need these in a makefile, build script or elsewhere and don't have them on your system:
https://github.com/aureliojargas/sed.sf.net/blob/master/local/docs/emulating_unix.txt
Miscellaneous compiler tips:
To stop the gnu gcc compiler after the first error use:
-Wfatal-errors
To stop the gnu gcc compiler after the N errors use:
-fmax-errors=N
So for three errors add the following to the command line when invoking gcc:
-fmax-errors=3
My favorite preprocessor tips:
You can use a C preprocessor to generate templates.
Here's an example to generate web pages using the preprocessor and templates:
http://www.distasis.com/cpp/mingw.htm#makeprograms
For my build system, I need a more functionality than the standard C preprocessor could offer.
Rather than reinventing the wheel and writing my own preprocessor, I found gpp has just enough capabilities beyond a standard preprocessor to handle the task.
The gpp preprocessor is available from:
https://logological.org/gpp
Know of some other preprocessor or makefile tips? Have written an article or blog post with your tips on these topics? Please share them. They may get included in this list.