简体   繁体   English

在多个系统上使用 CMake 安装 C++ 应用程序

[英]install C++ application using CMake on multiple systems

I wrote a simple openGL application using C++ and SDL2.我使用 C++ 和 SDL2 编写了一个简单的 openGL 应用程序。 I've compiled it using CMake on linux and everything works fine.我已经在 linux 上使用 CMake 编译它,一切正常。 How do I now run my program on other systems?我现在如何在其他系统上运行我的程序? Is it possible to compile the program on my linux machine and then open the compiled output file on another linux machine?是否可以在我的 linux 机器上编译程序,然后在另一台 linux 机器上打开编译后的 output 文件? Or does every machine have to compile its own version of the program?还是每台机器都必须编译自己的程序版本? What about the SDL2 and Freetype library which I link in my CMake file?我在 CMake 文件中链接的 SDL2 和 Freetype 库怎么样? I assume every system that compiles my program needs to have these libraries installed?我假设编译我的程序的每个系统都需要安装这些库? What if I want to run my application on Windows?如果我想在 Windows 上运行我的应用程序怎么办? In case every system needs to compile its own version, is there an easy way of creating an installer using the CMake file?如果每个系统都需要编译自己的版本,是否有使用 CMake 文件创建安装程序的简单方法?

  1. Binary Distribution二进制分布

    How do I now run my program on other systems?我现在如何在其他系统上运行我的程序?

This depends on 2 things: In order to run pre-built app on another system, that other system must be compatible in both SW and HW;这取决于两件事:为了在另一个系统上运行预构建的应用程序,另一个系统必须在软件和硬件上都兼容; meaning, bothSW APIs and ( HW architecture ) must be "close enough".这意味着,SW API和(硬件架构)都必须“足够接近”。 For example, if you built the app on any kind of PC on Linux, and used OpenGL and SDL2, as well as Linux system libraries, you program "depends" on each of those things, in order to run.例如,如果您在 Linux 上的任何类型的 PC 上构建了应用程序,并使用了 OpenGL 和 SDL2,以及 Linux 系统库,您需要在每个系统库上运行“依赖”的程序。 More specifically, it depends on particular API versions of OpenGL, SDL2, and the OS APIs, as well as HW (including but not limited to CPU Instruction set ).更具体地说,它取决于特定的 API 版本的 OpenGL、SDL2 和 OS API,以及硬件(包括但不限于CPU 指令集)。 To make a portable app that can run on other machines, you need to define a distribution mechanism (source, or binary), and then solve the problem.要制作可以在其他机器上运行的可移植应用程序,您需要定义分发机制(源代码或二进制文件),然后解决问题。 This part of your question looks more like a binary distribution.您问题的这一部分看起来更像是二进制分布。 For binary distribution, you need to provide the dependencies with your app, either directly (by including them in your package), or indirectly, by providing a smart installer that would download dependencies and install them along with your app.对于二进制分发,您需要直接(通过将它们包含在您的包中)或间接地通过提供将下载依赖项并将它们与您的应用程序一起安装的智能安装程序来为您的应用程序提供依赖项。 The easiest way to begin solving that is to statically link against your major dependencies if you know they do not come out of the box with your target OS.开始解决该问题的最简单方法是静态链接您的主要依赖项,如果您知道它们不是与您的目标操作系统一起开箱即用的。 In general, consider building a package out of your app, if you want to go this way.一般来说,如果您想以这种方式构建 go,请考虑从您的应用程序中构建 package。 Consider using CPack is you are using CMake.如果您使用的是 CMake,请考虑使用CPack

  1. Source Distribution源分布

    Is it possible to compile the program on my Linux machine and then open the compiled output file on another Linux machine?是否可以在我的 Linux 机器上编译程序,然后在另一台 Linux 机器上打开编译后的 output 文件? Or does every machine have to compile its own version of the program?还是每台机器都必须编译自己的程序版本? What about the SDL2 and Freetype library which I link in my CMake file?我在 CMake 文件中链接的 SDL2 和 Freetype 库怎么样? ? ? I assume every system that compiles my program needs to have these libraries installed?我假设编译我的程序的每个系统都需要安装这些库?

If you mean, is it possible to rebuild your app on another machine and run it on that same machine, the answer is: such approach will eliminate HW dependencies, but will still leave all SW dependencies.如果您的意思是,是否可以在另一台机器上重建您的应用程序并在同一台机器上运行它,答案是:这种方法将消除硬件依赖关系,但仍会保留所有软件依赖关系。 This approach looks like a source code distribution.这种方法看起来像一个源代码分发。 To solve the SW dependency problem, you have to, first, define them in your CMake file.要解决软件依赖问题,您必须首先在 CMake 文件中定义它们。 Some of the possibilities here is to use CMake pkgconfig or use native CMake Packaging .这里的一些可能性是使用CMake pkgconfig或使用原生 CMake 打包 once you done that, CMake script will handle the dependencies for you, somewhat.完成此操作后,CMake 脚本将在某种程度上为您处理依赖关系。

  1. Cross-platform development跨平台开发

    What if I want to run my application on Windows?如果我想在 Windows 上运行我的应用程序怎么办? In case every system needs to compile its own version, is there an easy way of creating an installer using the cmake file?如果每个系统都需要编译自己的版本,是否有使用 cmake 文件创建安装程序的简单方法?

In order to do that, you need to define an OS portability layer in your app.为此,您需要在应用程序中定义操作系统可移植性层。 you can not use Linux or Windows system libraries directly.您不能直接使用 Linux 或 Windows 系统库。 If you can keep yourself strictly within a subset of POSIX that Windows supports, it is possible do that with a little-to-moderate effort.如果您可以将自己严格控制在 Windows 支持的 POSIX 子集中,那么只需稍加努力即可做到这一点。 Otherwise, you'll have to define your portability API, and implement it for all platforms you care about.否则,您必须定义您的可移植性 API,并为您关心的所有平台实现它。 Once you solved the cross-platform part, it comes down to either source or binary distribution (Part 1 and 2 of this answer).一旦解决了跨平台部分,它就归结为源代码或二进制分发(此答案的第 1 部分和第 2 部分)。

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

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