开发者

is binary executable file portable

开发者 https://www.devze.com 2023-01-23 08:12 出处:网络
After compiling a C program can I take the binary executable file, and run it on a开发者_如何转开发nother system where there is no gcc loaded, such as an Ubuntu box?Technically: yes, but use a static

After compiling a C program can I take the binary executable file, and run it on a开发者_如何转开发nother system where there is no gcc loaded, such as an Ubuntu box?


Technically: yes, but use a static link if you need max portability


Strictly speaking you don't need gcc, but you may need various libraries. By default, language processors produce dynamically linked binaries which require extensive run-time support in the form of libraries, and compatible versions must be found on the target system that can be substituted for the ones you linked against on the development host.

This requires that the target be a similar version of the same OS, for example, linux to linux. There are more subtle issues of version skew. On windows, this is known as DLL-hell.

You can isolate yourself from many of these concerns by static linking. This will make the executable file larger and it won't share memory anymore (except with additional instances of itself) but the program will be able to survive more target version skew.


gcc compiles C into machine code, this means that he code will only run on the same architecture for which it was compiled. Additionally there are typically some dependencies to other binaries (e.g. C-runtime, posix, Win32) so if you compile a program on Ubuntu it will run on Ubuntu even if it doesn't have gcc installed but it will not run on Windows or other unixes (like Solaris or HPUX).

This is where C is different from Java and C# where the code is compiled to a virtual machine code and runs on any system which has this language runtime (JVM/CLR). Other portable languages are scripting languages (Perl/Python/JavaScript) where the script can run anywhere there is an interpreter.


Partially true. Yes, you don't need the compiler itself, but you need the C runtime library. Anyway, both C compiler and C runtime is part of POSIX, therefore you won't find a Unix system without them.


As long as it's the same architecture and has the same run-time libraries, it should run fine. For example, compiling a program on 32-bit Linux wont run on 64 bit Windows, but if you compile a program on Ubuntu and then put it on another ubuntu machine it should run fine.


Depends on the platforms and libraries installed on the platform. A compiled executable will likely have dynamically linked library dependencies on, say, libc, and whatever else it was linked to during compilation.

What platforms/compilers are involved, and where do you want to move this binary to?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号