MST

星途 面试题库

面试题:C语言中#error在跨平台项目中的应用

假设你正在开发一个跨平台项目,需要在不同操作系统下编译运行。请举例说明如何使用#error来标识在某些特定平台编译时可能出现的问题,并阻止编译通过。
31.5万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试

在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” 并阻止编译继续进行。这样就标识出在特定平台编译时可能出现的问题并阻止编译通过。