#include directive in switch case if clause
-
Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.
case ID_ONE:
if (id == 1)
{
#include "Func.h"
Func();
}
if (id == 2)
{
Func();
}
break;I get the following error:
incompatible implicit declaration of function 'Func'
If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?
-
Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.
case ID_ONE:
if (id == 1)
{
#include "Func.h"
Func();
}
if (id == 2)
{
Func();
}
break;I get the following error:
incompatible implicit declaration of function 'Func'
If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?
-
Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.
case ID_ONE:
if (id == 1)
{
#include "Func.h"
Func();
}
if (id == 2)
{
Func();
}
break;I get the following error:
incompatible implicit declaration of function 'Func'
If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?
This won't work.
Include
statements are processed by the C/C++ preprocessor[^] which is executed as first build step before compilation. But theif
condition is processed when your application is executed. It is possible to useinclude
statements somewhere inside the code. But then the file content must be valid code only (imagine what the statement does: It replaces the statement with the file content). -
Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.
case ID_ONE:
if (id == 1)
{
#include "Func.h"
Func();
}
if (id == 2)
{
Func();
}
break;I get the following error:
incompatible implicit declaration of function 'Func'
If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?
It is legal but probably doesn't do what is expected (by you). Suppose "Func.h" is
double Func();
Then, what you get (after preprocessor pass) is:
case ID_ONE:
if (id == 1)
{
double Func();
Func(); // here Func is explicitely declared (returning a double value)
}
if (id == 2)
{
Func(); // here Func is implicitely declared (returning a int value)
} -
Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.
case ID_ONE:
if (id == 1)
{
#include "Func.h"
Func();
}
if (id == 2)
{
Func();
}
break;I get the following error:
incompatible implicit declaration of function 'Func'
If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?
elelont2 wrote:
i have a tool which generates some code
I question that statement. Certainly the professional code generation tools that I have used allow for customization which would always include a way to provide includes. If it an in-house tool then obviously the way to go would be to modify it. However an alternative would be to create your own simple tool which does nothing but insert a header. It is probably possible to do this with existing tools and an appropriate command line script. Even simple file concatenation might be sufficient. Then once you have that methodology down modify your build to do that step after code generation but before the compile.