如何写宏让跨平台编译更简单?

How to write a macro to make cross-platform compile easier?

我正在编写 C 程序,我的操作系统是 MacOS。 我想知道如何添加宏以便在不同操作系统下编译我的 C 程序,主要是 WindowsLinux?

非常感谢您为我提供有关这个问题的想法。

你可以像下面那样做:-

#if defined(_WIN32)
    #define OS_NAME "windows"
    /* add your windows specific codes here */
#elif defined(__linux__)
    #define OS_NAME "linux" 
    /* add your Linux specific codes here */
#elif defined(__APPLE__) && defined(__MACH__)
   #define OS_NAME "MacOS"
   /* add your Mac specific codes here */