如何根据操作系统添加不同的标题?

| 我正在编写一个便携式C ++应用程序。如何根据运行的操作系统包含不同的标头。有没有办法在C ++中做到这一点,或者我必须使用构建系统吗?     
已邀请:
使用预处理器:
#ifdef _SUNOS
//code
#elseif _LINUX
//code
#elseif _HPUX
//code
#elseif _WIN32
//code
#else
#error OS not supported
#endif
    
我将使用预处理程序指令和跨平台的构建系统,例如CMake。您可以这样做:
#ifdef LINUX
#include <unistd.h>
#elif defined(WINDOWS)
#include <algorithm.h>
# elif Defined(MAC_OSX)
//... etc.
#else
#error No operating system defined
#endif
然后将相应的预处理器标志添加到构建中,例如:
-DLINUX
。     
我们在Linux(Red Hat Enterprise 5),Sun(Solaris)和Windows上开发。我们的系统将使用以下内容:
#ifndef MSWINDOWS
#include <unistd.h>
#else
#include <winbase.h>
#endif
//More includes here
    

要回复问题请先登录注册