MPS-Basic
|
This page explains how to build environment for executing MPS-Basic.
There are 5 things you need.
Component | Explanation | Examples |
---|---|---|
Editor/IDE | to write (, build and execute) programs. | Vim, Emacs, Visual Studio Code, Visual Studio, ... |
Compiler | to compile the program | Clang, GCC, ... |
Libraries | what you include at the beginning of the code | C++ Standard Library (like <vector> ), OpenMP, ... |
Build System | automatically build executable files based on setting files | Make, MSBuild, Ninja, ... |
CMake | automatically generate input files for user defined Build System | — |
The simplest editors, such as Vim and Emacs, specialize in editing programs. To build and run a program, you need to open a terminal in a separate window. In contrast, IDEs have advanced features that are not limited to program editing and can automatically build and run programs. IDE stands for Integrated Development Environment.
Examples:
Simple editors are so simple that it's inconvenient (unless you're a pro), while IDEs are so sophisticated that it's difficult for beginners to use. Visual Studio Code is somewhere in between. VS Code allows you to open a terminal within it's window and to introduce extensions to automate the build and execution of programs. If you're new, use Visual Studio Code.
Sophisticated functionalities in c++
such as std::string
, std::vector
, and many more are provided as a C++ Standard Library. And you have to include the library at the beginning of the code to use it, like #include <string>
or #include <vector>
. Also, if you want to use OpenMP to parallelize your code, you need a library for that. So if you want to use such functionalities in your program, you have to download libraries in addition to the compiler.
Commonly used libraries are usually downloaded at the same time as the compiler, and the compiler and the libraries are automatically linked (meaning the compiler knows which libraries to use). But sometimes you have to let the compiler know the location of libraries when compiler doesn't come with libraries or you want to use additional libraries that doesn't come with the compiler.
When we have a single source file main.cpp
, the compile is quite easy.
This command creates executable file named main
.
Things get complicated when we have more files. Let's assume we have multiple source files (main.cpp
, file1.cpp
and file2.cpp
), and header files (file1.hpp
and file2.hpp
). In this case, our command will be:
Now this is complicated. If we want to add new files or change compiler options, then it'll be something I don't want to imagine.
That's why we use a build system. One of the most widely used build systems is Make
.
Make
include MSBuild
(Microsoft Build Engine) and Ninja
.Let's see how we compile the code using Make
. First, you need to prepare a file named Makefile
:
You don't have to understand this, but I hope you get the idea that you specify all the settings (like compiler and options), and Make
generates the command for you. Now that you have your Makefile
, all you have to do is:
Thats's it.
The advantages of using make can be summarized as follows.
Makefile
.Makefile
.Make
only re-compile files that have been changed. This makes compile process much faster when there are many files to deal with.So it's better to use a Build System such as Make
to compile. Then why use CMake
?
There are some problems when using a specific build system.
MSBuild
or Ninja
instead of Make
, and do you want to prepare setting files for all of them?Makefile
example looked difficult, right? So using a specific build system is problematic especially when you are working as a team.Now CMake
solves these problems. CMake
is a tool for generating input files that user-defined build system will use to generate executable file. To use CMake
, you need a file named CMakeLists.txt
:
That's it! How easy it is compared to the above example of Makefile
! Now what you do is:
Argument -S
specifies the location of CMakeLists.txt
, and -B
specifies the location where the input files for build system will be generated. CMake Generator
is responsible for generating input files for build system, so you specify which generator you want to use based on your build system using -G
option. For example,
will create input files that Make
uses (which is Makefile
). Likewise,
generates Visual Studio 2022 project files that MSBuild
uses, and
generates build.ninja
files for Ninja
to use.
Now you can see that by introducing cmake
you can make your project platform-independent, meaning your project can be built in Windows, Mac, Linux, or any other platforms, just by a single and easy CMakeLists.txt
file.
Finally let's see how we compile the code using CMake
and build system. Here, let's assume we use Make
as a build system.
If you want to use MSBuild
, Ninja
or any other build system, you just change the -G
option at the first command and the third command as appropriate.
@tips -G
flag is optional. If not provided, CMake
will use default generator which is determined based on your platform. Type cmake --help
to see your default generator.
A simpler approach would be to do this:
This time we didn't call Make
, but CMake
calls it for us based on the input files generated in build
directory. This way, you don't have to remember the commands to call the build system you want to use, and cmake handles it all for you.