Solved

Programming Languages, compilers, and portability

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now