Can a C executable file run on all operating systems

我的未来我决定 提交于 2020-08-10 04:14:41

问题


If I create a C executable file in an operating system (ex: Windows), can this file run on other operating systems (as: Linux, Mac-OS).


回答1:


Trying to give a complete answer, it would be a theoretical "yes" with a really big "but".

The first thing to understand: a binary executable created by a C compiler doesn't differ in anything relevant from one created by a compiler for some other language. So the following doesn't have to do anything with C.

There are two issues about that. Operating systems have different file formats for storing the binary (which typically consists of sections for code, data, ... as well as some meta-information telling the system where to place them, what relocations to do, and so on). And then, operating systems also expose their API to binaries in some way (for Windows, a set of system DLLs).

Indeed, as long as the processor architecture is the same, you could still run the binary on another system. You would just need some software that a.) knows how to load a windows binary and b.) translates win32 API calls to API calls of your target platform. This is btw what wine does.

I just guess where you're coming from is having read about C being platform independent. This is true, but just applies to source code. A C program using nothing but standard C will compile (and THEN run) on any platform as long as you have a C compiler for it available.




回答2:


There is no such thing as a "C executable". A C compiler translates C source code to the specific target system. The format of the executable will be the one specified by the OS. So if you use a C compiler for Linux, it will produce an executable that only works on Linux.

However, if the code is pure standard C, you can take the very same source code and compile it for a different platform on a different compiler. Also known as porting the program.



来源:https://stackoverflow.com/questions/32645978/can-a-c-executable-file-run-on-all-operating-systems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!