C++ 条件编译 不同系统

Posted on 2018-09-06 09:52  yacbo  阅读(608)  评论(0编辑  收藏  举报
#if defined (_WIN32)
    //***
#else //linux
    //***
#endif

 

C++:编写跨平台程序的关键,C/C++中的内置宏定义
分两部分:
 
操作系统判定:
Windows:   WIN32
Linux:   linux
Solaris:   __sun
 
编译器判定:
VC:  _MSC_VER
GCC/G++:   __GNUC__
SunCC:   __SUNPRO_C__SUNPRO_CC#include <stdio.h> 
#include <iostream>
using namespace std;
int main(int argc,char **argv)
{
int no_os_flag=1; #ifdef linux no_os_flag=0; cout<<"It is in Linux OS!"<<endl; #endif

#ifdef _UNIX no_os_flag=0; cout<<"It is in UNIX OS!"<<endl; #endif

#ifdef __WINDOWS_ no_os_flag=0; cout<<"It is in Windows OS!"<<endl; #endif

#ifdef _WIN32 no_os_flag=0; cout<<"It is in WIN32 OS!"<<endl; #endif

if(1==no_os_flag){ cout<<"No OS Defined ,I do not know what the os is!"<<endl; } return 0; }

 

Copyright © 2024 yacbo
Powered by .NET 8.0 on Kubernetes