Need help understanding which OS types support C++ (sort of...)

Hi

I am trying to find the words to describe this, so please excuse me if I don't make much sense...

So, when a C++ program is compiled, it is made into an executable file, right? If so, how can that be run on a linux OS, such as Ubuntu, when they do not run .exe files like windows does? I know that linux OS's can run C++ made programs, so what file type are they running then? Does C++ compile to a different file type on linux? If so, what? Or does linux not have 'file types' and just plain data?

Thanks,
James
Compilers are OS-specific (MVC++, XCode, ...) or compile based on the OS they're running on if they support multiple OSs. XCode won't generate .exe files, and .exe files "produced" by MVC++ won't run on an iOS computer.

Generally, you're better off sending your code and having others recompile it on their own system.
So, let's say I wrote a "Hello, world!" program (on a windows computer if it matters) and sent the code to someone on an Ubuntu computer to compile. What would the output type of the produced file be?
closed account (1vRz3TCk)
It would be an executable file.
See: http://en.wikipedia.org/wiki/Executable
Ah, thank you. It all makes sense now! An executable does not necessarily end with '.exe', it is the data in the file that counts. The file extension on Windows is merely its way of finding which program to open the file with!

So C++ compilers create executable files and these files contain the instructions for the computer to perform. The executable files can be read by any OS, yet the file extension .exe is just Windows' way of telling the user that "This file is an executable". In linux, this message is in the file's permissions. Right?
How the OS keeps track of which files can be executed is just sugar, but broadly speaking yes, windows does it by the extension and Linux likes permissions. Changing the extension or permission doesn't change the executable nature of the file.

The executable files can be read by any OS


It can be read by any OS, but any file can be read by any OS - a file is just data. Being able to read the file doesn't mean being able to actually execute it. The key is that the compiler is designed to create a different executable for different OSes. You can compile the C++ code into an executable on windows and then transfer that file to Linux; the Linux OS will happily read the file (ultimately, it's just numbers), but will not be able to actually execute it. To do that, you need a compiler made to turn the C++ code into an executable for Linux, and similarly for all other OSes.

To summarise; C++ code is plain text, readable by anything that can read plain text. Compiler takes that plain text and turns it into an executable for a given target system, which will (usually) not be usable by different target systems.
Last edited on
Ok, thanks! That was a great explanation.
Topic archived. No new replies allowed.