The question is that if I need to specify the 32bit or 64bit of OS (not of the CPU architecture) too?
You don't specify an Operating System (you have only one OS running on a given machine; to run several of them use an hypervisor or some VM). You specify & choose an Application Binary Interface (ABI) since some OSes are able to offer several ABIs (and runtime systems).
Is there any difference in compilation a program for above systems?
Yes, there is some difference (think of sizeof(void*)
at least; also the 64 bits ISA uses more registers and the ABI could define different calling conventions, by passing more arguments thru registers). I can't tell more about Windows, which I don't know.
When I add -m32 in the options of gcc compiler, what is this 32 for?
Dive into the documentation of GCC, notably the Invoking GCC chapter.
It is some x86 option:
The -m32 option sets int, long, and pointer types to 32 bits, and generates code that runs on any i386 system.
The -m64 option sets int to 32 bits and long and pointer types to 64 bits, and generates code for the x86-64 architecture. For Darwin only the -m64 option also turns off the -fno-pic and -mdynamic-no-pic options.
Read also about the x32 ABI (which is a Linux specific thing).
I can't say more about Windows specific (which I don't know and never used). But I will explain what is happening on Linux. I leave you to seek similar knowledge for your proprietary Microsoft operating system.
On Linux, the operating system kernel can be configured (at kernel build time) to accept both 32 bits and 64 bits ELF executables and provide a system call runtime environment for both architectures. Such kernels are able to execute with execve(2) both 32 bits executables, and 64 bits executables and provide two different ABIs (one for -m32
for 686 instruction set architecture, another for -m64
for x86-64 ISA) for them. Notice that the same OS kernel enable executions of binary executables in either 32 bits or 64 bits mode.
I don't know Windows, but I could imagine that Microsoft provide also two different runtime environments & ABIs, one for 32 bits 686 ISA and another for 64 bits x86-64 ISA. Maybe one of them is optional and needs to be installed or bought separately (I really don't know and I don't care).
It is up to you to dive into Microsoft documentations to find the details of the differences.
See also x86 calling conventions wikipage.
I recommend reading something like Operating Systems : Three Easy Pieces (freely downloadable, chapter by chapter) to understand more the role of an OS, and also more about the x86-64 ISA, including its long mode.
Of course you need to care of other dependencies (or coupling). Read about DLL hell & dependency hell.