面试题答案
一键面试在C/C++ 中,可以这样使用 #error
:
#ifdef _WIN32
#error This code is not supposed to be compiled on Windows platform
#endif
#ifdef __APPLE__
#error This code is not supposed to be compiled on Mac platform
#endif
在上述代码中:
- 当在Windows平台编译(
_WIN32
宏被定义)时,#error
会输出 “This code is not supposed to be compiled on Windows platform” 并阻止编译继续进行。 - 当在Mac平台编译(
__APPLE__
宏被定义)时,#error
会输出 “This code is not supposed to be compiled on Mac platform” 并阻止编译继续进行。这样就标识出在特定平台编译时可能出现的问题并阻止编译通过。