Solved

Programming Languages, compilers, and portability

Posted on 2008-06-17
3
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
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.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

749 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