I wrote a simple openGL application using C++ and SDL2. I’ve compiled it using CMake on linux and everything works fine. 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? 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? I assume every system that compiles my program needs to have these libraries installed? What if I want to run my application on Windows? In case every system needs to compile its own version, is there an easy way of creating an installer using the CMake file?
- 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, both SW APIs and (HW architecture) must be “close enough”. 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. 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). 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. Consider using CPack is you are using CMake.
- Source Distribution
Is it possible to compile the program on my Linux machine and then open the compiled output file on another Linux machine? 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? ? 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. Some of the possibilities here is to use CMake pkgconfig or use native CMake Packaging. once you done that, CMake script will handle the dependencies for you, somewhat.
- Cross-platform development
What if I want to run my application on Windows? In case every system needs to compile its own version, is there an easy way of creating an installer using the cmake file?
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. 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. Otherwise, you’ll have to define your portability API, and implement it for all platforms you care about. Once you solved the cross-platform part, it comes down to either source or binary distribution (Part 1 and 2 of this answer).