Solved

Programming Languages, compilers, and portability

Posted on 2008-06-17
3
562 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

832 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