At the moment of creating a project in CodeBlocks, a sentence #endif
always appears in the bottom. What does this mean? What function can I give to this?
At the moment of creating a project in CodeBlocks, a sentence #endif
always appears in the bottom. What does this mean? What function can I give to this?
That's what they're called Preprocessor Directives , in this case they would be direct conditional inclusion directives .
According to the documentation:
These directives allow you to include or discard part of the code of a program if a certain condition is met.
#ifdef
allows you to compile a section of a program only if the macro specified as a parameter has been defined, no matter which be its value.
For example let's say you have an application and you would like it when you are developing, always start with a specific user. In this case, these directives can help you:
#define PRUEBA
#ifdef PRUEBA
// CODIGO PARA CARGAR USUARIO POR DEFECTO
#endif
The code within the directive will only be compiled if PRUEBA
is defined. So when you go to publish your application, you only have to eliminate the definition of PRUEBA
and the program will follow its normal flow.
As discussed Einer directives preprocessor #endif
%%% and #if
code% (and #ifdef
, #ifndef
, #elif
and #else
that you did not mention) are conditional code inclusion directives.
These directives are not exclusive to C ++, they are also used in c .
The code compilation process is divided into 9 phases in C ++, roughly:
#endif
). \
). In phase 4 the pre-processor is running, all macro are replaced by the defined value, the content of the files included by
#define
is copy-pasted at the point where the policy is displayed ( it by applying the phases 1 to 4 recursively) and the code that does not meet the conditions of conditional inclusion ( #include
, #endif
, #if
, #ifdef
, #ifndef
, #elif
and #else
) is removed.
Suppose we have these files:
#endif
#ifndef OBJETO_HPP
#define OBJETO_HPP
struct Objeto {
int a, e, i, o, u;
const bool debug =
#ifdef _DEBUG
true;
#else
false;
#endif
};
#endif
Objeto.hpp
#include "Objeto.hpp"
#include "Objeto.hpp"
int main()
{
return 0;
}
The file main.cpp
will suffer the following transformations when compiled (in the example we will compile it with main.cpp
defined):
Resolve inclusions ( _DEBUG
).
#ifndef OBJETO_HPP
#define OBJETO_HPP
struct Objeto {
int a, e, i, o, u;
const bool debug =
#ifdef _DEBUG
true;
#else
false;
#endif
};
#endif
#ifndef OBJETO_HPP
#define OBJETO_HPP
struct Objeto {
int a, e, i, o, u;
const bool debug =
#ifdef _DEBUG
true;
#else
false;
#endif
};
#endif
int main()
{
return 0;
}
Delete the code not included conditionally ( #include "archivo"
#endif
% #if
, #ifdef
#ifndef
%% #elif
and #else
).
#ifndef OBJETO_HPP
#define OBJETO_HPP
struct Objeto {
int a, e, i, o, u;
const bool debug =
#ifdef _DEBUG
true;
#else
#endif
};
#endif
#ifndef OBJETO_HPP
#endif
int main()
{
return 0;
}
Eliminate pre-processor directives (everything that starts with #endif
)
struct Objeto {
int a, e, i, o, u;
const bool debug =
true;
};
int main()
{
return 0;
}
The compiler will receive the code files after the pre-processor has digested them, both routines are independent and do not interact. This is important since it implies that you can not use anything from the pre-processor in C ++ or anything from C ++ in the preprocessor:
#define CONCUPISCENCIA
bool funcion()
{
// vvvvvvvvvvvvvv <--- No funciona!!
if (CONCUPISCENCIA)
return false;
else
return true;
}
// vvvvvvvvv <--- No funciona
#if funcion()
int main()
#else
int WinMain()
#endif
{
return 0;
}
In the previous example #
is a pre-processor symbol so it can not be used in a CONCUPISCENCIA
of C ++ since the language does not know of its existence. On the other hand the function if
belongs to C ++ so the pre-processor can not interpret it for conditional inclusion.