1

Let assume that I want to compile a C program for a platform. I know that I must specify CPU architecture in compilation because of differences of instruction sets between different CPU architectures. I also know that I must specify the destination platform operation system because of the differences between executable file structures and system calls and ... between different operation systems.

Q1: The question is that if I need to specify the 32bit or 64bit of OS (not of the CPU architecture) too?

In the other word let assume that I have two systems:

  1. 64bit CPU + 32 Bit Microsoft Windows
  2. 64bit CPU + 64 Bit Microsoft Windows

Is there any difference in compilation a program for above systems?

Q2: When I add -m32 in the options of gcc compiler, what is this 32 for? Is it for Operation System or it is for CPU architecture?

Ebrahim Ghasemi
  • 5,850
  • 10
  • 52
  • 113
  • It also depends on the shared libraries you link to and on the structure of system types as defined by the header files on compile time. Also the compiler optimizations define a minimum feature set provided by the CPU. The -m is only a subset of the target architecture as described here: https://gcc.gnu.org/ml/gcc-help/2009-10/msg00280.html - I would say don't use it. – eckes Jun 04 '17 at 17:05
  • Why you say me don't use it? Even if I have a 32bit CPU arch? – Ebrahim Ghasemi Jun 04 '17 at 17:06
  • Yes don't use it directly, use -march=i386 instead. And consider not to do crosscompiling it is harder to get the environment correct. – eckes Jun 04 '17 at 17:08
  • @eckes Thank you. How can I specify type of OS (64bit vs 32bit os)? Should I specify it? – Ebrahim Ghasemi Jun 04 '17 at 17:18
  • 1
    You already accepted the answer so I assume it helped. – eckes Jun 04 '17 at 17:20
  • @eckes Yes it helped, but I still don't know how I can specify type of OS (64bit vs 32bit os) in the compilation time. – Ebrahim Ghasemi Jun 04 '17 at 17:23
  • You are confused. You have one single common OS kernel which is providing several runtime environments and ABIs. – Basile Starynkevitch Jun 04 '17 at 17:25
  • 1
    I am not an expert in GCC for Windows but normally you have to use a GCC produced for the intended target. You would select the target by using the right compiler. According to this there is at least one multi target version for mingw: https://stackoverflow.com/questions/19690504/how-do-i-compile-and-link-a-32-bit-windows-executable-using-mingw-w64 you did not say what compiler you use. – eckes Jun 04 '17 at 17:29

1 Answers1

5

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547