用 C++ 编写的程序可以在任何地方运行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14694719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can programs which are written in c++ run anywhere?
提问by UnTraDe
I know that for running c++ on Windows you need to compile specifically for Windows and the same goes for linux and on...
我知道要在 Windows 上运行 C++,您需要专门为 Windows 编译,对于 linux 和...
But for example, if I'm compiling program for Windows written in native c++ I can run it on a freshly installed windows pc? I mean, without downloading visual c++ runtime libraries, etc, I can just compile it, let's say, reinstall windows on my computer, and run it without installing anything else?
但是例如,如果我正在为用本机 C++ 编写的 Windows 编译程序,我可以在新安装的 Windows pc 上运行它吗?我的意思是,无需下载 Visual C++ 运行时库等,我可以编译它,比如说,在我的计算机上重新安装 Windows,然后在不安装其他任何东西的情况下运行它?
(The question above using Windows as an example but the same thing can be done on freshly installed linux distro? e.g Ubuntu) Thanks in advance.
(上面的问题以 Windows 为例,但同样的事情可以在新安装的 linux 发行版上完成?例如 Ubuntu)在此先感谢。
回答by Emilio Garavaglia
The only answer is "it depends".
唯一的答案是“视情况而定”。
There are many ways that an OS can "run" a program, many ways a program can be build, and many way code can be assembled.
操作系统可以通过多种方式“运行”程序,可以通过多种方式构建程序,并且可以通过多种方式组装代码。
A program that uses only "standard libraries" and that links all libraries statically, does not need any other dependency (in the sense that all the code it need is in the binary itself or into OS libraries that -being part of the system itself- are already on the system).
仅使用“标准库”并静态链接所有库的程序不需要任何其他依赖项(从某种意义上说,它需要的所有代码都在二进制文件本身或操作系统库中——作为系统本身的一部分——已经在系统上)。
But:
但:
statically link the standard libraries (which are most likely present in all programs) will bloat the memory usage of many copy of the same code. That's the reason library are often linked dynamically, but this requires "installation" of those libraries as well
Programs that use only standard libraries can do only the things that are somehow "common" (or can be commonly represented) into all systems, thus loosing all the peculiarity that makes an OS different from another.
There are "platforms" that - by the nature of their peripherals - are not represented one into the other: a coffee machine has 12 keys and a textual 2 row x 20 col display. A PC has a mouse, a keyboard, and a display that can reach even 10'000 pixel of width, of millions of color each. A tablet has a touch surface that can seize multiple points at the same time. Can you imagine a program running the same on all those three platform?
静态链接标准库(最有可能出现在所有程序中)会增加相同代码的许多副本的内存使用量。这就是库经常动态链接的原因,但这也需要“安装”这些库
仅使用标准库的程序只能执行在所有系统中以某种方式“通用”(或可以通用表示)的事情,从而失去了使操作系统与众不同的所有特性。
有一些“平台”——就其外围设备的性质而言——并没有代表一个到另一个:咖啡机有 12 个键和一个文本 2 行 x 20 列显示器。PC 有鼠标、键盘和显示器,它们的宽度甚至可以达到 10,000 像素,每个像素都有数百万种颜色。平板电脑有一个可以同时抓住多个点的触摸面。你能想象在这三个平台上运行相同的程序吗?
回答by Kaslai
For what it's worth, when using MinGW with static linkage to libc and libc++, It's very likely that your C++ application will work on any Windows system '95 and later, unless you specifically enabled functionality in the Windows API that isn't available, like NT features. All of my SDL applications compiled for Windows 7 (compiled in 32 bit of course) work fine on my '95 machine.
值得一提的是,当使用 MinGW 与 libc 和 libc++ 的静态链接时,您的 C++ 应用程序很可能可以在任何 Windows 系统 '95 及更高版本上运行,除非您专门启用了 Windows API 中不可用的功能,例如NT 功能。我为 Windows 7 编译的所有 SDL 应用程序(当然是以 32 位编译的)在我的 95 年机器上运行良好。
When deploying a Linux application, it's best to just supply the source plus a configure script or makefile. This will ensure that the user has valid dependencies for your application. You could deploy binaries to specific package managers if you wanted to though.
部署 Linux 应用程序时,最好只提供源代码和配置脚本或 makefile。这将确保用户对您的应用程序具有有效的依赖项。如果您愿意,您可以将二进制文件部署到特定的包管理器。
回答by Tony Velito
No. Often the libraries are different on different systems. If your program involves any GUI then you will definatly have OS specific code that won't run on other OSs.
不。通常,不同系统上的库是不同的。如果您的程序涉及任何 GUI,那么您肯定会拥有无法在其他操作系统上运行的特定于操作系统的代码。
If you write a C++ program targeting the g++ compiler without GUI code there may still be some OS specific code. But you should be able to port it with minimal effort.
如果你编写一个没有 GUI 代码的针对 g++ 编译器的 C++ 程序,可能仍然有一些特定于操作系统的代码。但是您应该能够以最少的努力移植它。
If your program only uses stl and stdio, then it will probably be portable. For example, MS STL's ::c_str() function works a little different than the linux one.
如果您的程序只使用 stl 和 stdio,那么它可能是可移植的。例如,MS STL 的 ::c_str() 函数与 linux 函数有点不同。
回答by Tomas Smagurauskas
Yes, the standard libraries are everywhere. Just think about it, most of your programs that you get are writen in C/C++. Only dependencies come when you use some specified libraries like winsock etc, therefore some windows applications are unlikely to work on linux and vice versa.
是的,标准库无处不在。想想看,你得到的大部分程序都是用 C/C++ 编写的。只有当您使用某些指定的库(如 winsock 等)时才会出现依赖项,因此某些 Windows 应用程序不太可能在 linux 上运行,反之亦然。