How to check the current operating system is Windows, Linux or OSX?

I am writing a compiler project that will generate assembly code as the target language. However, depending on the operating system, there are some small changes that need to be considered, and I am not sure how to check the operating system .If it matters, I only care about 32 bits. I have seen something similar in some source code

#ifdef WIN32

but I don’t know how this works.

Edit: some clarification. I use gcc on all three platforms. I don’t know if macros like WIN32 are defined through gcc in each platform If so, these constants seem to solve my problem.

#ifdef Handle program symbols to conditionally compile code, depending on the defined symbols. In your example, the compiler or #include file can define WIN32, which means that the code is compiled in the Win32 environment.

Depending on the compiler and platform, there may be different predefined symbols that indicate the operating system or processor architecture (and many other possible things) related to the current compilation environment.

Use gcc, you can use the following command line to display predefined symbols:

gcc -E -dM-

On my machine Above, one of the defined symbols is:

#define __FreeBSD__ 8

This just means that I am running FreeBSD 8.

< p>(The above applies to C and C series languages, such as C.)

I am writing a compiler project that will generate assembly code as the target language. However, Depending on the operating system, there are some small changes that need to be considered. I am not sure how to check the operating system. If it matters, I only care about 32-bit. I have seen something similar in some source code

< p>

#ifdef WIN32

But I don’t know how this works.

Edit: some clarification. I use it on all three platforms gcc. I don’t know if macros like WIN32 are defined through gcc in every platform. If so, these constants seem to solve my problem.

< p>#ifdef uses preprocessor symbols to conditionally compile code Code, depending on the defined symbols. In your example, the compiler or #include file can define WIN32, which means the code is compiled in the Win32 environment.

According to the compiler And platform, there may be different predefined symbols, which indicate the operating system or processor architecture (and many other possible things) related to the current compilation environment.

Using gcc, you can use the following command line Display predefined symbols:

gcc -E -dM-

On my machine, one of the defined symbols is :

#define __FreeBSD__ 8

This just means that I am running FreeBSD version 8.

(The above applies to C and C series languages, such as C.)

Leave a Comment

Your email address will not be published.