如何在 C++ 中创建独立的可执行程序?
我想创建一个可以在没有源代码的情况下在任何计算机上运行的程序,这怎么可能?如果我在程序中使用 OpenGL,会有什么不同吗?
I want to create a program that could work on any computer without the source code, How is that possible? and does it make any difference if I used OpenGL in the Program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果不提供要编译的源代码,您无法用 C++ 编写可在任何计算机上运行的程序。
例如,您也许可以用 C++ 编写一个程序,然后对其进行编译并构建一个可在 Windows x86-64 上运行的可执行文件,但该可执行文件无法在 Linux(或 MacOSX)上运行,也无法在 ARM 上运行(例如 Android 手机)除非您考虑使用模拟器
You cannot code a program in C++ that would work on any computer without giving your source code to be compiled.
For example, you could perhaps code in C++ a program, and compile it and build an executable which works on Windows x86-64, but that executable won't work on Linux (or MacOSX), and it won't work on ARM (e.g. Android phones) unless you consider using emulators
如果您使用 Visual C++ 开发代码,您可能需要考虑两个选项:
If you are developing your code with Visual C++ you may need to consider two options:
通常,您会将目标文件与某种依赖于平台的加载器链接起来。该加载器加载您的对象并执行所有操作以从内存中启动它。通常,您可以告诉编译器链接目标文件并编译 blob。 OpenGL 是一个功能强大的 API,通常作为动态链接库分发,并在运行时链接到您的程序。如果我记得你只需要确保 dll 位于你想要的位置并从一开始就将 dll 加载到你的程序中。
Normally you would link your object file with some sort of a platform dependent loader. This loader loads your object and does all the stuff to start it from memory. Normally you can tell your compiler to link your object file and compile a blob. OpenGL is a powerful API and is usually distributed as a.dynamic linked library and is linked at runtime to your program. If I remember you just have to make sure the dll is where you want it and load the dll in your program from start.
您的 C++ 程序在运行之前会被编译并链接为可执行文件。您应该能够在包含项目的文件夹的
Debug
或Release
子文件夹中找到该可执行文件。如果您不使用 GLUT 或类似的库,OpenGL 应该随 Windows 一起提供,不会造成其他问题。如果您确实使用了 GLUT,那么将 .dll 文件与您的应用程序捆绑在一起就足够了。
无论如何,源代码不是必需的。
Your C++ program is compiled and linked into an executable before it is run. You should be able to find that executable in a
Debug
orRelease
subfolder of the folder containing your project.OpenGL, if you're not using GLUT or similar libraries, should just come with Windows and pose no additional problems. If you do use GLUT, it's enough to bundle the .dll file with your application.
In any case, the source code won't be necessary.
通过编译并将其链接为可执行文件。只要您不使用某种解释性语言(例如 Python、Ruby 等),您就不可避免地会遇到可执行文件。您可能/将遇到的最大问题是对库的依赖。然而,这些也链接到二进制文件中。所以你要发布的只是一个 .exe;也许还有一些 .dll 。通常您会将其包装在安装程序包中以部署给最终用户。安装程序是使用“NullSoft 安装程序系统”(NSIS) 之类的东西创建的。
OpenGL本身只是一个API,由系统库提供。因此,您不会随程序一起提供 OpenGL,而是期望用户将其安装在系统上(如果他安装了图形驱动程序,就会出现这种情况)。
By compiling and linking it into an executable. As long as you're not using some interpreted language (like Python, Ruby or such) you're presented with an executable inevitably. The biggest problem you may/will run into is dependencies on libraries. However those are linked into a binary as well. So what you're going to ship will be just a .exe; and some .dll maybe. Usually you'd wrap this in a installer package for deployment to the end user. Installers are created with something like the "NullSoft Installer System" (NSIS).
OpenGL itself is just a API, provided by a system library. So you don't ship OpenGL with your program, but expect the user to have it installed on the system (which will be the case if he installed the graphics drivers).