1

I've written a relatively small C++ program in CLion, on a Mac. CLion uses CMake to compile an executable file which can only be run on my Apple machine, I know this much.

My project includes the 'ncurses' library, but other than that it only uses standard C++ ones. My question is, how do I go about running my program on another laptop, running Linux? What is the standard way of deploying C++ applications between platforms, at least between Linux and OSX which both come with a C/C++ compiler? Must CMake exist on both machines for this to be done?

Sorry for the very general question, I've been learning how to write code but not really how to go about sharing it!

Edward Gargan
  • 153
  • 10
  • No magic to it. Just make sure you have `cmake` and `gcc` installed on the laptop, and recompile there. As long as it is standard C++, a simple re-compile on each different OS is all that is required. – David C. Rankin Dec 22 '17 at 00:53
  • Related https://stackoverflow.com/q/33238345/3002139 – Baum mit Augen Dec 22 '17 at 01:03
  • I've been reading around the issue and there's a lot of mention of static or dynamically linking libraries relevant to my program - is this something I need to consider or this all handled by CMake? – Edward Gargan Dec 22 '17 at 01:09
  • 1
    That's mostly relevant when shipping binaries. – Baum mit Augen Dec 22 '17 at 01:11
  • 1
    Perfect I've got it up and running - thanks for the help! Just out of curiosity, larger industry-level applications that use C++, how do their creators ensure the application runs across all platforms? Surely they can't assume too much about what a consumer's machine can support? – Edward Gargan Dec 22 '17 at 01:32
  • There are cross-platform frameworks like e.g. Qt that allow fairly painless porting ... – tink Dec 22 '17 at 03:12
  • [`ncurses`](https://www.gnu.org/software/ncurses/) (or maybe termcap or gettext) on OS X may require [The GNU Portability Library](https://www.gnu.org/software/gnulib/). Those libraries are kind of tricky on AIX, OS X, Solaris and a few other OSes. – jww Dec 22 '17 at 05:13

1 Answers1

2

To those in a similar situation, I did the following to share my code between Mac and Linux.

As I said, CLion uses CMake. I thought there would be a lot going on under the hood but it turns out all you need to compile your source files is the CMakeLists file, and an installation of CMake itself.

CLion generates the CMakeLists file, but it can be written from scratch and it only needs around 5 lines to begin compiling. In it, you declare the source files you wish to include in compilation, and a few other things, like minimum CMake version and the version of C++ your project uses.

There's a good explanation of what goes into the CMakeLists file here.

My Linux machine runs Ubuntu, and the version of CMake I was using was 2.8.something - quite early compared to 3.6 on my Mac. So firstly this meant I had to change cmake_minimum_required(VERSION 3.6) to 2.8.

Then I tried running cmake . in the directory containing the source files, which threw up a load of compile errors to the terminal. Most of which concerned things like template classes and curly brace initialisation - features of more recent versions of C++.

This was because CMake's set(CMAKE_CXX_STANDARD 11) syntax, which sets the project's C++ version, wasn't around in CMake 2.8. Replacing this line with set (CMAKE_CXX_FLAGS "-std=c++11") fixed this issue.

Another cmake . worked just fine, which then spat out a load of files into the source directory. I'm not entirely sure what all of them do, but they all surround a generated 'Makefile'. Running make in the same directory compiles all of the source files and outputs an executable, which worked perfectly.

The ncurses library seems pretty baked-in to Unix-like/based systems, so I was able to use and compile this library by simply #includeing it in my code. 3rd party libraries might require a bit more work!

Edward Gargan
  • 153
  • 10
  • 1
    In general, folks do *"out-of-source"* building with **Cmake**. That generally means you make a subdirectory called `build` in your top-level directory which contains `CMakeLists.txt`, then go in there and run `cmake ..` then all the generated stuff it spits out is not mixed in with your project's version-controlled files. – Mark Setchell Dec 23 '17 at 22:48
  • 1
    Thanks for the insight - looking at the state of my current project directory this approach makes a lot more sense! – Edward Gargan Dec 24 '17 at 23:55