In C/C++, what are the build directives for? When should I use them?

Asked

Viewed 1,858 times

3

I came across a C code that used a compilation directive on #ifdef and I didn’t really understand what it was for. I found an explanation, but it wasn’t clear. Here is an example of a directive and the explanation I researched:

#ifdef <token>
/* code */
#else
/* code to include if the token is not defined */
#endif

ifdef checks whether the Given token has been #defined earlier in the file or in an included file. If so, it includes Everything between it and the closing #Else or, if no #Else is present, the closing #endif. ifdef can be used with built-in token Identifiers set by the Compiler to indicate that Additional Functionality is available.

ifdef checks if the token was previously assigned through sets in the file or included in it. If so, it will include everything between #Else or, if #Else is not present, in #endif. ifdef can be used with built-in token identifiers defined by the compiler to indicate functionality availability additional.

Source: http://www.cprogramming.com/reference/preprocessor/ifdef.html

After all, what are compilation directives for? When should I use them? In practice, why are they used?

1 answer

4


They are processed before the compilation of the code itself. In general they instruct the compiler of some actions that should be done with the code.

In this particular example the conditional build directive will make a check if a variable exists. Note that this variable was also created by a build directive, either in code or input at the time of the compiler call. The variable is not part of normal code. It’s like another programming language.

Unlike the if normal code, the #ifdef or #ifndef can only verify if a specific build variable exists, nothing else. The end of it is with the #endif.

If it exists, the compiler will consider the code within the directive to be compile, otherwise it will be discarded and the compiler will not be aware of it. The same goes for the #else.

This is very useful for separating code that should be used when it is thrashing or not, or to choose specific code for different platforms, or to choose which parts of the code should be included in the final result, putting or removing certain characteristics, helping in optimization, or to treat special situations.

It is also possible to use a #if which makes it possible to make a more sophisticated condition, or #else or #elif, as in "normal language".

Another widely used directive is to include other code files within the main code, especially header files with declarations required for the code. #include

Besides the #define to define variables and macros, and #undef to remove the setting, it is quite used to #pragma to give more specific instructions to the compiler, including the shutdown of certain warnings and errors.

There are other less used (#line, #error).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.