Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Programming Languages, compilers, and portability

Posted on 2008-06-17
3
Medium Priority
?
587 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 500 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 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

927 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