How to implement your own preprocessor directive

D

_dwin

Hi,

Does anyone know how to implement your own preprocessor directive?

For instance, I would like to have a directive which goes like:
#<directive_name> <parameters, ...>

ie. #compress input_file output_file

Thanks.
 
K

Kira Yamato

Hi,

Does anyone know how to implement your own preprocessor directive?

For instance, I would like to have a directive which goes like:
#<directive_name> <parameters, ...>

ie. #compress input_file output_file

have you considered using a make file for this instead?
 
J

Juha Nieminen

Does anyone know how to implement your own preprocessor directive?

Yes: You download the source code of an opensource compiler and modify
it to support your preprocessor directive.

However, the "standard" way of enhancing the preprocessor is not to
create a new # command, but to create a new #pragma sub-command. This is
because other compilers will simply ignore #pragmas they don't support
instead of giving an error message. If your #pragma has been designed
properly, any source code using it will still be portable.

Even then, you still probably have to modify and recompile the
compiler's source code for this. I don't understand why you would want
to do this, but whatever floats your boat.
 
K

Kira Yamato

Hi,

But is it possible to prefix '#' sign in front?

I'm not suggesting that you write your own directive at all. Your
example of compressing a file does not affect that very same source
code directly. So, you could've written a Makefile with a target/rule
that compresses the input file before compiling the source. But that
is only that one example.

Exactly, what are you trying to do that requires you to make
nonstandard extensions instead of just doing things the standard way?
 
J

Joe Greer

(e-mail address removed) wrote in (e-mail address removed):
Hi,

Does anyone know how to implement your own preprocessor directive?

For instance, I would like to have a directive which goes like:
#<directive_name> <parameters, ...>

ie. #compress input_file output_file

Thanks.

The short answer is no. Not without writing your own non-standard
preprocessor. The point of the preprocessor is code generation prior to
actual compilation. Any other commands (such as compress) should be done
in your build environment and not by the C++ compiler. In general, your
best bet would be macros (see the boost PP library for an extensive set of
code generation macros). Alternately, you can invoke another text
processing language on the code before you compile it to get additional
effects. (awk, sed, perl, m4 come to mind). The problem with invoking
another macro language is that then to port your code, you have to be sure
there is a working version of your processing language on that platform.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top