Solved

Programming Languages, compilers, and portability

Posted on 2008-06-17
3
564 Views
Last Modified: 2013-11-14
Maybe it's a lame question but.... why a program written with a compiled language (such as C++) must be compiled specifically for the Operating System on which it will run ?

if I'm not wrong, the compiler converts the source code to the executable which is the machine code. So I guess, when we run an executable, the operating system gives the machine code to the hardware (i.e. CPU) to be executed (correct me if this is not the case). So why that "machine code" cannot be executed both in windows and Linux ?

In the case of Java, I know that the source code is compiled to bytecode, that runs on top of the Java Virtual Machine. As soon as you have the right virtual machine for your platform, you can run any Java "compiled" program which was compiled for that specific virtual machine, regardless of the operating system on which it was compiled.

With this rationale, why a C++ executable (which is suppose to be "machine code") can't be executed on Linux, if it was compiled in windows, or vice versa ?
0
Comment
Question by:Harrris
3 Comments
 
LVL 11

Expert Comment

by:CMYScott
ID: 21808736
I may be WAY off base - but I believe it is in part because of securing the OS from the Hardware.

In Windows at least, software does NOT talk directly to hardware.  So while the program runs on the processor, it cannot make use of serial ports, usb ports, network, video etc directly.  Therefore the code uses API's which act as an intermediate between software, the OS and hardware drivers.  Since the Linux uses different API's and device drivers than Windows does, the compiler needs to compile the program into a binary that's specific to one platform.

I most certainly could be off base here - but this is a simple explanation of my understanding.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 21810314
The problem is not the machine code, it is the same in Linux and Windows. By example, to sum or multiply 2 numbers.
The problem comes when you want to access some OS service, like creating a file, or showing something in the console (or worst, in a window), here is where the compiler needs a specialized library to use those services.
This is solved in some "managed" languages, like Java and C#, where the most important Operating System features are encapsulated by an "environment" or "runtime" library that have a specialized version for each OS, keeping your application the same for all OS.
0
 
LVL 15

Assisted Solution

by:DonConsolio
DonConsolio earned 125 total points
ID: 21839094
- the CPU and memory system may be initiallized in different ways on different OS (little/big endian, stack size and organization, CPU protection mode and exception handling ("privileged instructionss"), etc.)

- the executable format is specific to the OS (e.g. ELF for linux, .EXE for dos/windoes)
The OS has to implement a way to load and start your program (which includes memory allocation, resource reservation, linking to dynamic libraries, finding the entry point and jumping there, provide symbols for loading, debugging, etc.)

- differen OS provide different system calls to access hardware (serial ports, display, keyboard), OS services (like memory allocation, scheduling, file access), networking etc.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 139
groupNoAdj 7 105
Near realtime alert if SQL Server services stop. 20 85
convert Systemjs to Webpack 3 29
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question