I have had problems because where I work they use a version of dev c ++ 4.9 and I use 5.1 in my home, I searched the internet to insert the code as a #ifdef header ..., can someone tell me how to use it and if Can you solve my problem?
I have had problems because where I work they use a version of dev c ++ 4.9 and I use 5.1 in my home, I searched the internet to insert the code as a #ifdef header ..., can someone tell me how to use it and if Can you solve my problem?
It is a conditional, so that the code is only compiled under certain conditions, in this case it would be for that code to only be compiled if you are in an environment with MSDOS
Yes, you can use it, since you can define a symbol yourself (in the environment where you want your code to run) and that the code is only compiled when you have it defined.
NOTE: This will not make your code compatible, but will not be compiled in certain cases
#ifdef
is a precompiler directive that allows conditional compilations. Its syntax is as follows:
// opción 1
#ifdef [NOMBRE]
[código1]
#endif
// opción 2
#ifdef [NOMBRE]
[código1]
#else
[código2]
#endif
And it means the following: "If [NAME] is an identifier that exists then compile the fragment [code1], otherwise compile the fragment (if applicable) [code2]".
To define identifiers, you can use the #define
directive, as shown in the following example:
#define COMPILA_1
#ifdef COMPILA_1
int func()
{ return 1; }
#else
int func()
{ return 2; }
#endif
int main()
{
int valor = func();
printf("%d\n",valor);
}
If you compile it as is, it will print 1
, since the identifier exists. However, if you comment the line of #define
the identifier will cease to exist and the program, after recompiling it, will print 2
.
It should be noted that compilers usually define some constants depending on the environment they are running on (Windows / Linux, 32/64 bits, ...).
In your case, you see that the code that gives you problems only fails (or works, because it is not very clear) if you are compiling under the MSDOS environment. By adding the conditional compilation you can remove the harmful code when compiling the program in an environment that does not support those instructions.
Greetings.
#ifdef
? The #ifdef
instruction is a preprocessor directive that checks if a preprocessor definition exists. Your area of influence starts at the #ifdef
instruction and lasts until you get the #endif
instruction, optionally adding the denied form #else
or an additional condition using #elif
.
The preprocessor is a program unrelated to the compiler that is responsible for performing a series of operations on the code before that the compiler tries to compile. Preprocessor and compiler are different programs 1 and do not interact with each other; simply the preprocessor prepares the code for the compiler and then the compiler uses it.
#ifdef
? Activates or deactivates the lines within its area of influence according to whether the condition is met or not complied with. So for example:
int main() {
#ifdef FABADA
int asturiana = 0xfabada;
#else
std::string patatas_fritas { "Que ricas!" };
#endif
return 0;
}
In the previous code the #ifdef
is checking the existence of the definition FABADA
; if the preprocessor exists, it will activate the lines between #ifdef
and #else
while deactivating the lines between #else
and #endif
, so it will send the compiler the following code:
int main() {
int asturiana = 0xfabada;
return 0;
}
In case the definition FABADA
does not exist the activated / deactivated lines will be the opposite and the preprocessor will send the following code to the compiler:
int main() {
std::string patatas_fritas { "Que ricas!" };
return 0;
}
#ifdef_MSDOS_
? In the title you have #ifdef_MSDOS_
without spaces between #ifdef
and _MSDOS_
what I assume is a typo because it does not work:)
What you are checking is whether the _MSDOS_
definition exists to activate or deactivate lines of code accordingly.
_MSDOS_
definition come from? These definitions can come from three different sites:
#define
D
parameter, for example -D_MSDOS_
). If somewhere in the code you write:
#define _MSDOS_
You create that definition of the preprocessor and any verification of the existence of such a definition will be true (as long as the definition is visible from the point where the check is made). Sometimes the definition is assigned a value:
#define _MSDOS_ 5
But it is indifferent to the #ifdef
, since it checks if ( if
) is defined ( def
) it does not have value; To do a check on value, use the #if
instruction:
#define _MSDOS_ 5
std::cout <<
#if _MSDOS_ == 3
"Microsoft DOS Version 3\n";
#elif _MSDOS_ == 5
"Microsoft DOS Version 5\n";
#endif
You get the same effect if compiling adds the definition in the compiler's command line:
C: \ projects \ my_project > compile main.cpp -D_MSDOS _
With the difference that the definition applied from the command line will be visible in all files instead of only those that can see the #define _MSDOS_
instruction.
Finally, the compilers predefine several processor definitions to provide information to the user, such as the compiler version, operating system, version of the C ++ standard, etc ...
Here are the predefined definitions of some compilers:
_MSDOS_
come from ?, second part. I have looked at the predefined definitions of several compilers and I have not seen any reference to _MSDOS_
, so I deduce that this definition is specific to your project; surely it is defined in some header file (extension .h
or .hpp
) or it is added from the command line.
It's hard to be sure with so little information, but I gather from the name that it will make the code you compile compatible with Microsoft DOS ; which makes me think you're playing quite old code.
1 This is not necessarily always true, some compilers incorporate the preprocessor. But it's still true that preprocessor and compiler work separately.