我需要维护一个支持在 Linux 和 Windows 上运行的项目。一些使用像这样的预处理器指令的代码很好。
#ifdef _WIN32 // _WIN32 is defined by Windows 32 compilers
#include <windows.h>
#else
#include <unistd.h>
#endif
但有些是实际的实现,我想阻止使用预处理器指令。
void Foo()
{
#ifdef _WIN32 // _WIN32 is defined by Windows 32 compilers
code for windows
#else
code for Linux
#endif
some common code...
#ifdef _WIN32 // _WIN32 is defined by Windows 32 compilers
code for windows again
#else
code for Linux again
#endif
}
因此,事情变得复杂且难以维护。有没有更好的办法?