How to build a C++ program from its source code
There are many options to set up a development environment on each operating system. What proposed here is not the only option, but I try to minimize your effort. You may skip some of the steps if they have been properly set up already.
Install a C/C++ compiler
Some compilers can be installed separately, but some compilers do come along with an IDE (Integrated Development Environment). We recommend to install the commonly used IDE for an operating system.
-
Windows: install Microsoft Visual Studio (NOT Visual Studio Code).
-
Mac: install XCode from the Apple's App Store.
-
Linux: install C/C++ compiler and related tools through a few commands
-
If you are using Fedora, Red Hat, CentOS, or Scientific Linux, etc., use the following yum command:
-
yum groupinstall 'Development Tools'
-
If you are using Debian, Ubuntu, and their variants, run the following apt-get commands one by one:
-
sudo apt-get update
-
sudo apt-get install build-essential manpages-dev
Build
Choose ONE of the following (or whatever you are familiar with):
-
Option 1: Use any IDE that can directly handle CMakeLists files to open the CMakeLists.txt in the root directory. Then you should have obtained a usable project and just build. I recommend using CLion or Qt Creator.
-
Option 2: Use the CMake GUI application or from the command line to generate project files for your IDE. Then load the project to your IDE and build.
-
Option 3: Use CMake to generate makefiles and then build. For example
-
on Linux or macOS:
- $ cd /path/to/your/code
- $ mkdir Release
- $ cd Release
- $ cmake -DCMAKE_BUILD_TYPE=Release ..
- $ make
-
on Windows with Microsoft Visual Studio:
- $ cd /path/to/your/code
- $ mkdir Release
- $ cd Release
- $ cmake -DCMAKE_BUILD_TYPE=Release ..
- $ msbuild MyProject.sln /p:Configuration=Release
NOTE: in the last command, replace "MyProject" with your actual project name.
Here are more details about How to Build a CMake-based Project.
Copyright @Liangliang Nan. 2021