简体   繁体   中英

Can a C executable file run on all operating systems

如果我在操作系统(例如Windows)中创建C可执行文件,则该文件可以在其他操作系统(例如Linux,Mac-OS)上运行。

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 . AC 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.

There is no such thing as a "C executable". AC 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM