简体   繁体   English

如何在C ++中创建独立的可执行程序?

[英]How to create a Standalone Executable program in C++?

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? 如果我在程序中使用OpenGL,这有什么区别吗?

You cannot code a program in C++ that would work on any computer without giving your source code to be compiled. 如果不提供要编译的源代码,就无法在任何计算机上可以用C ++编写程序。

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 (eg Android phones) unless you consider using emulators 例如,您也许可以使用C ++语言编写程序,然后对其进行编译并构建一个可在Windows x86-64上运行的可执行文件,但是该可执行文件在Linux(或MacOSX)上将不起作用,而在ARM(例如Android手机),除非您考虑使用模拟器

If you are developing your code with Visual C++ you may need to consider two options: 如果使用Visual C ++开发代码,则可能需要考虑两个选项:

  1. Make sure you link all libraries statically. 确保您静态链接所有库。
  2. Install on the target computers along with your program Microsoft Visual C++ Redistributable Package corresponding to the Visual C++ version you use like the one at http://www.microsoft.com/download/en/details.aspx?id=5555 . 将与您使用的Visual C ++版本相对应的程序Microsoft Visual C ++ Redistributable Package与目标计算机一起安装在目标计算机上,例如http://www.microsoft.com/download/en/details.aspx?id=5555上的版本 Some installer generating software will make it for you automatically. 一些生成安装程序的软件会自动为您安装。

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. 通常,您可以告诉编译器链接目标文件并编译blob。 OpenGL is a powerful API and is usually distributed as a.dynamic linked library and is linked at runtime to your program. OpenGL是一个功能强大的API,通常以动态链接库的形式分发,并在运行时链接到您的程序。 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. 如果我记得您,只需要确保dll在您想要的位置,然后从开始就将dll加载到程序中即可。

Your C++ program is compiled and linked into an executable before it is run. 您的C ++程序在运行之前已编译并链接到可执行文件中。 You should be able to find that executable in a Debug or Release subfolder of the folder containing your project. 您应该能够在包含项目的文件夹的“ Debug或“ Release子文件夹中找到该可执行文件。

OpenGL, if you're not using GLUT or similar libraries, should just come with Windows and pose no additional problems. 如果您不使用GLUT或类似的库,则OpenGL应该随Windows一起提供,不会造成任何其他问题。 If you do use GLUT, it's enough to bundle the .dll file with your application. 如果您确实使用GLUT,则将.dll文件与应用程序捆绑在一起就足够了。

In any case, the source code won't be necessary. 无论如何,都不需要源代码。

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? 如果我在程序中使用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. 只要您不使用某些解释语言(如Python,Ruby或类似语言),就不可避免地会为您提供可执行文件。 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; 因此,您将要发行的只是一个.exe; and some .dll maybe. 还有一些.dll。 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). 安装程序是使用“ NullSoft安装程序系统”(NSIS)之类的东西创建的。

OpenGL itself is just a API, provided by a system library. OpenGL本身只是系统库提供的API。 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). 因此,您不随程序一起提供OpenGL,而是希望用户将其安装在系统上(如果安装了图形驱动程序,情况就是这样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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