Introduction to CMake configuration for building and running simple demo C/C++ programs.
Note
To build the sample demo programs that are in the example and project directory, we are
using cmake
build system command on WSL or another virtual machine with a GNU/Linux
system, such as Debian, Ubuntu, Fedora, CentOS, and so on. The choice is yours.
Currently, we only support the amd64 and arm64 architecture.
Make sure you have a program called CMake installed, as well as a compiler for the C/C++ languages, typically by entering the following command in the terminal:
sudo apt update
sudo apt install cmake gcc g++
cmake --version
Also you can install clang-format
and clang-tidy
for formatting the code and other cool stuff,
type the following command in the terminal:
sudo apt install clang-format clang-tidy
First step is a create build directory and generate the build files using cmake
command from the
top-level project directory, type the following command:
cmake -B build
After running the above command, a build
folder will be created, containing the necessary files to
build the demo programs. These files are generated automatically based on the configuration in the
CMakeLists.txt
file.
Navigate to the build
directory and run the make
command to build the executable files for the
demo programs:
cd build ; make
The demo programs should be compiled, and now you can run the programs.
Caution
For the build to succeed, you must copy the header file mdsanima.h
into the /usr/local/include
directory. This is because the mdsanima.h
header file is a part of the MDSANIMA library,
which is not a part of the CMake build system right now. To copy this file to the destination
directory, type the following command in the terminal sudo cp lib/mdsanima.h /usr/local/include
and then run the make
command again.
You can now run the demo programs by typing the following command inside the build
directory:
./mdsanima-amarooke
./mdsanima-blizzard
./mdsanima-conquest
./mdsanima-fantastic
./mdsanima-incredible
./mdsanima-stunning
You can also run the programs from the top-lever directory by simply adding the full path to the
executable files like this ./build/mdsanima-fantastic
command.
The project programs are located in the project
directory, and the executable files are located in
the build
directory. After running the first command cmake -B build
to generate the build files
for the example and project programs, and then running the make
command to build the
executable files, you can run the example programs that we showed above.
The mdsanima-amarooke, mdsanima-blizzard and mdsanima-conquest program is quite different. You can install the project program by typing the following command:
cd build
sudo make install
That command will install the mdsanima-amarooke, mdsanima-blizzard and mdsanima-conquest
project demo programs in the /usr/local/bin
directory, and you can run the program by typing the
following command:
mdsanima-amarooke
mdsanima-blizzard
mdsanima-conquest
Now you can run the program in any directory on your system. The CMakeLists.txt
file contains
configuration for the static library and install command for this project.
The installation command will also create the mdsanima.h
file in the /usr/local/include
directory and the libmdsanima.a
file in the /usr/local/lib
directory that you can use later in
your own project.
There are various ways to create a project, one of which is as I presented here. The source files
are located in the src
directory, but you can also put all the files in one directory; it all
depends on the project.
Note
To manualy build the demo programs, you can use the gcc
or g++
command to compile and link the
source code files.
Here is a example instruction for compiling and linking the source code files, type the following command:
cd example/mdsanima-fantastic ; mkdir build
g++ -o build/mdsanima-fantastic main.cc
./build/mdsanima-fantastic
This method is only recommended if you want to manually build the program.
Important
The sample projects are written in C and C++ languages, and the source code files have
extensions .c
, .cc
, and .cpp
. You can also use the .cxx
file format, as we added in the
example project. Here is just an example. Remember to use the chosen format and be consistent with
it.
All projects are the same; they only differ in the text displayed in the terminal and in other file extensions.
Also, check how Google does it. Here is a C++ Style Guide and Objective-C Style Guide provided by the Google Style Guide. The complete guide for naming conventions, variables, functions, and more by Google.
This comprehensive guide includes code style conventions for C/C++, Python or other languages, covering variable names, function and file names, and more. It has been authored, revised, and maintained by many Googlers.
This adjustment maintains clarity while enhancing the flow of information.