Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the C Preprocessor for languages other than C

The Wikipedia entry for the C Preprocessor states:

The language of preprocessor directives is agnostic to the grammar of C, so the C preprocessor can also be used independently to process other types of files.

How can this be done? Any examples or techniques?

EDIT: Yes, I'm mostly interested in macro processing. Even though it's probably not advisable or maintainable it would still be useful to know what's possible.

like image 402
user4812 Avatar asked Jul 08 '09 18:07

user4812


People also ask

What is the use of preprocessor in C?

The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. In many C implementations, it is a separate program invoked by the compiler as the first part of translation.

Which language has a preprocessor?

In some computer languages (e.g., C and PL/I) there is a phase of translation known as preprocessing. It can also include macro processing, file inclusion and language extensions.

What are the types of C preprocessor?

There are 4 Main Types of Preprocessor Directives:Macros. File Inclusion. Conditional Compilation. Other directives.


2 Answers

You can call CPP directly:

cpp <file>

Rather than calling it through gcc:

gcc -E filename

Do note however that, as mentioned in the same Wikipedia article, C preprocessor's language is not really equipped for general-purpose use:

However, since the C preprocessor does not have features of some other preprocessors, such as recursive macros, selective expansion according to quoting, string evaluation in conditionals, and Turing completeness, it is very limited in comparison to a more general macro processor such as m4.

Have you considered dabbling with a more flexible macro processing language, like the aforementioned m4 for instance?

like image 102
kjfletch Avatar answered Sep 28 '22 08:09

kjfletch


For example, Assembler. While many assemblers have their own way to #include headers and #define macros, it can be useful to use the C preprocessor for this. GNU make, for example, has implicit rules for turning *.S files into *.s files by running the preprocessor ('cpp'), before feeding the *.s file to the GNU assembler ('as').

like image 34
DevSolar Avatar answered Sep 28 '22 09:09

DevSolar