Search for a problem C, C ++ to determine whether the operating system is Linux or Windows or UNIX [compiler built-in macro exploration (not particularly satisfied )]

Source: Internet
Author: User

The answer is not good. Why, because they do not know the principle. In fact, the principle is very simple. You can judge this only when macro definitions are added to the compiler parameters or makefile.

For example, if the compiler sets macro is_windows, you can use it. It cannot be used without setting.

However, I am sure that various compilers have built-in macros, such as cl.exe of MS vc, GNU gcc of Linux, which have different built-in macros on various platforms.

 

Therefore, if I search for "various compilers with built-in macro operating systems", there is one of the few answers:

 
C ++: Write cross-platformProgramKey: C/C ++The built-in macro definition is divided into two parts: the operating system judgment: Windows: win32linux: linuxsolaris: _ sun compiler judgment: VC: _ msc_vergcc/G ++: _ Gnuc _ suncc: _ sunpro_c and _ sunpro_cc are reproduced from: http://Blog.csdn.net/avagrant158/article/details/6298145

It's not enough. How can we know the macros built into each compiler? You have to tell me the method. You can't do it. If you have time, go to msdn and check it out. (They may not be able to check it out. They may not necessarily say so.) GCC can check it out.Source code, But so manyCodeIt's hard.

Based on this, I wrote a cross-platform C ++ program. However, the _ UNIX windows macros are incorrect for the moment. No output

Testdefineos. cpp source code

# 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  ;} 

In Windows/Linux, use the ms vc cl.execompiler (without knowing what cl.exe, we generally do not call it, but GCC can indeed be called), and GCC compiler results

Windows cl.exe compiler results ==================== D :\ > Cl.exe testdefineos. cpp is used for 8  0x86 Microsoft (r) 32 Bit C/C ++ optimized compiler 16.00 . 40219.01  Copyright (c) Microsoft Corporation. All rights reserved. Testdefineos. cppc: \ Program Files (x86) \ Microsoft Visual Studio  10.0 \ Vc \ include \ xlocale ( 323  ): Warning c4530: C is used. ++ Exception handler, but not enable extended semantics. Please specify/Ehscmicrosoft (r) incremental linker version  10.00 . 40219.01  Copyright (c) Microsoft Corporation. All rights reserved. / Out  : Testdefineos.exe testdefineos. objd :\ > Testdefineos.exe it  Is   In Win32 OS! D :\ > Linux GCC compiler results ============================ Ayanmw @ ayanmw -Desktop :~ $ G ++ test testdefineos. cpp;./. Out  It  Is   In Linux OS! Ayanmw @ ayanmw -Desktop :~ $ Windows gcc.exe (mingw) ====================================== D :\ > G ++ Testdefineos. CPPD :\ > A.exe it  Is   In Win32 OS! D :\ >

 

for more information about the pre-compiled macro definition in C, see the http:// OS .chinaunix.net/a2008/1216/989/000000989777.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.