#ifndef _WINDOWS
if(condition)
{
printf("to do in linux");
}
else
#endif
{
printf("should work in both linux and windows...");
}
我的问题:这段代码是否适用于 linux 和 windows?
#ifndef _WINDOWS
if(condition)
{
printf("to do in linux");
}
else
#endif
{
printf("should work in both linux and windows...");
}
我的问题:这段代码是否适用于 linux 和 windows?
你有比你需要的更多的逻辑 - 你可以这样做:
#ifndef _WINDOWS
printf("to do in Linux");
// ...
#endif
printf("to do in both Linux and Windows");
// ...
由于#ifdef
,#ifndef
和#endif
是预处理器的指令,这些将在编译开始之前应用,如果_WINDOWS
定义了以下行,则完全忽略:
#ifndef _WINDOWS
if(condition)
{
printf("to do in linux");
}
else
#endif
请注意,condition
仅当它不在 Windows 上时才会对其进行评估。
在 Windows 上,除了以下嵌套的匿名范围外,什么都没有:
{
printf("should work in both linux and windows...");
}
实际上,ELSE 语句不会在 Linux 上运行,因为编译后的源代码是:
if(true)
{
printf("to do in linux");
}
else
{
printf("should work in both linux and windows and i have multiple statements in this else bolck");
}
并且请记住,true
我们有非零值,而不是 C 中的值,if(1)
例如(或者您需要为true
关键字正确定义,或者像 @JonathanLeffler 建议的那样只需要 `#include)。
但是您绝对可以在代码中使用不同的定义进行尝试。
它不会在 WINDOWS 上运行
用这个 :
#ifndef _WINDOWS
printf("to do in linux");
//...
#else
printf("should work in both linux and windows and i have multiple statements in this else bolck");
//...Other blocks of code
#endif