Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ada have a preprocessor?

To support multiple platforms in C/C++, one would use the preprocessor to enable conditional compiles. E.g.,

#ifdef _WIN32
  #include <windows.h>
#endif

How can you do this in Ada? Does Ada have a preprocessor?

like image 391
paxos1977 Avatar asked Nov 12 '08 13:11

paxos1977


2 Answers

The answer to your question is no, Ada does not have a pre-processor that is built into the language. That means each compiler may or may not have one and there is not "uniform" syntax for pre-processing and things like conditional compilation. This was intentional: it's considered "harmful" to the Ada ethos.

There are almost always ways around a lack of a preprocessor but often times the solution can be a little cumbersome. For example, you can declare the platform specific functions as 'separate' and then use build-tools to compile the correct one (either a project system, using pragma body replacement, or a very simple directory system... put all the windows files in /windows/ and all the linux files in /linux/ and include the appropriate directory for the platform).

All that being said, GNAT realized that sometimes you need a preprocessor and has created gnatprep. It should work regardless of the compiler (but you will need to insert it into your build process). Similarly, for simple things (like conditional compilation) you can probably just use the c pre-processor or even roll your own very simple one.

like image 70
Louis Brandy Avatar answered Sep 18 '22 03:09

Louis Brandy


AdaCore provides the gnatprep preprocessor, which is specialized for Ada. They state that gnatprep "does not depend on any special GNAT features", so it sounds as though it should work with non-GNAT Ada compilers. Their User Guide also provides some conditional compilation advice.

I have been on a project where m4 was used as well, with the Ada spec and body files suffixed as ".m4s" and ".m4b", respectively.

My preference is really to avoid preprocessing altogether, and just use specialized bodies, setting up CM and the build process to manage them.

like image 34
Marc C Avatar answered Sep 19 '22 03:09

Marc C