The Linux version will not use an Integrated Development Environment as this version is designed to show how to use the Meson and Ninja build systems to simplify compiling and linking to create each step of the program being developed during the tutorial. Many projects use other build systems, such as cmake or Gnu autotools (the familiar configure/make combination). Here Meson takes the place of configure
and Ninja takes the place of make
and the Meson/Ninja combination greatly simplifies building a complex executable that requires linking several third-party libraries (Nexus Class Library, Boost Program Options, and BeagleLib).
I will assume you are using Ubuntu 16.04 LTS Linux. The instructions will probably still work if you have a different flavor of Linux, but in this case you will need to figure out how to install the needed tools for your system (e.g. for a Fedora system you would use the package manager yum
rather than apt
to install tools).
The first step is to ensure that your system has some critical capabilities installed.
operatingsystem is linux.
sudo apt install build-essential
Note: Do not type the initial $
symbol. That is the bash prompt character and it is included here so that you know that this code block contains commands to be issued when you are in a bash shell. If you are in a bash shell (bash is the standard command interpreter in Linux), you should see the $
in your terminal (perhaps preceded by other information, such as your username and the current directory), but you should only type (or copy/paste) what follows the $
character.
sudo apt install git
sudo apt install curl
sudo apt install unzip
Because meson is a python app, we will use pip to install it. This will also ensure that we install the latest version. We first must ensure that pip itself is installed:
sudo apt install python3-pip
pip3 install meson
Download the latest linux binary of ninja (see release list. Here’s how to do this using curl for version 1.8.2:
cd
curl -LO https://github.com/ninja-build/ninja/releases/download/v1.8.2/ninja-linux.zip
unzip ninja-linux.zip
rm ninja-linux.zip
sudo mv ninja /usr/local/bin
We can’t use the apt package manager to install ninja because the version of ninja installed will probably be too old to work with the version of meson that you just installed.
Create a folder named strom inside your ~/Documents directory, create a directory src inside ~/Documents/strom, then create the meson.build and main.cpp files inside ~/Documents/strom/src. You should also create a directory distr to serve as an install target. The commands for doing this are:
cd ~/Documents
mkdir strom
cd strom
mkdir distr
mkdir src
cd src
touch meson.build
touch main.cpp
Note: The tilde (~
) in the change-directory (cd
) command is replaced by your home directory. The make-directory (mkdir
) command creates a new directory. The nominal duty of the touch
command is to modify the time stamp for a file, but if the file specified does not exist, it can be used to create an empty file by that name (and that is its purpose here).
You can use whatever editor you like to modify text files. There is a built-in text editor in Ubuntu called “Text Editor”. Because I work on linux systems remotely from a Mac, I use the BBEdit editor on my Mac, editing files on the Ubuntu server remotely using the File > Open From FTP/SFTP Server… menu command. You are of course also free to use emacs, vim, or nano. The program nano is usually the easiest text editor to use for Linux newbies. Just be sure to use a text editor, not a word processor, so that you end up with text files and not documents filled with formatting commands that will not be understood by compilers, autotools, etc.
Use your favorite text editor to add the following text to the currently empty file ~/Documents/strom/src/meson.build:
project('strom', 'cpp',
default_options : ['cpp_std=c++11','cpp_link_args=-static','prefix=/home/paul/Documents/strom/distr'],
version : '1.0')
executable('strom', 'main.cpp', install: true, install_dir: '.')
We will add to this file as the program develops, but this much will enable us to compile the file main.cpp using the C++11 programming language standard.
The first line of meson.build declares the name of the project (strom
), which is a C++ program (cpp
), defines the version (1.0
), and specifies a couple of default options. The cpp_std=c++11
specifies that we want to use features of C++11. The cpp_link_args=-static
causes the standard libraries (and libraries we add) to be directly incorporated into (statically linked to) the strom executable. Without this option, these libraries would be dynamically linked, which would work but slightly complicates running the program if some of the libraries are not installed in standard locations where they can be located by the operating system. We will thus statically link every library to our executable so that everything needed is in a single file on the disk. Documantation for options such as cpp_std
and cpp_link_args
can be found at the Meson Built-in Options web page.
Finally, the prefix=/home/plewis/Documents/strom/distr
option tells meson where we wish to install our program. Note that the prefix directory is an absolute URL on my system; you should modify this so that it specifies an actual directory on your system.
The second line tells meson that we wish to create an executable file (a program) named strom
by compiling main.cpp
and that we wish to install this program into the directory specified by the prefix that we declared in the default_options
section. The dot .
means “current directory”, but note that this is relative to the prefix, so by using '.'
we are telling meson that we wish to install this program directly into the prefix directory, not a subdirectory within the directory defined by the prefix.
We will need at least one source code file in order to test out the build system, so add the following text to the currently empty ~/Documents/strom/src/main.cpp file:
#include <iostream>
int main(int argc, const char * argv[]) {
std::cout << "Hello World!" << std::endl;
return 0;
}
This is the C++ version of the Hello World! program.
Create a directory to hold the build products:
cd ~/Documents/strom/src
mkdir build
Everything in this build folder can later be deleted because the contents of this folder are automatically generated from your source files. The first (cd
) command simply ensures that you are in the correct directory before you issue the mkdir
command; you do not need to type the cd
command if you are sure you are in that directory already.
Now run meson to create the build.ninja file inside the newly-created build directory:
meson ./build
This meson ./build
command is analogous to the configure
command you may have used when installing packages that use Gnu autotools. The build.ninja file is analogous to the Makefile generated by the configure
command and used by the make
command to compile and link a program.
Finally, run ninja to do the actual building:
ninja -C ./build
Ninja looks for the build.ninja file in the directory specified (the -C
switch tells ninja to change to the build directory before doing anything), and uses the information therein to build the program.
The executable file should now be found inside the ~/Documents/strom/src/build directory. Try to run it just to make sure everything worked:
cd ~/Documents/strom/src/build
./strom
You should see the output
Hello World!
While you are still inside the build directory, try typing
meson install
This will install the strom
executable in the directory specified by the variable prefix in the meson.build file, which should be the directory ~/Documents/strom/distr. Because meson install calls ninja automatically, you can actually skip the ninja step in the future.