This is a template for novices learning Programming: Principles and Practice Using C++ (3rd Edition). It requires no C++ or cmake experience.
- Git
- a C++ IDE that supports CMake (latest Visual Studio, Qt Creator, CLion, etc.)
- Click the green
Codebutton near the top of this page. - Click the
Download ZIPbutton. This will download the latest repository as a zip file. - Unzip the downloaded zip file somewhere you are going to store your code.
- Open your IDE (latest Visual Studio, Qt Creator, CLion, etc.) or configured Editors (VSCode with CMake Tools, etc.).
- In your IDE, open this unzipped folder
as a folderoras a cmake project.
The best thing about studying C++ with cmake is that a single project can manage multiple programs: you're not required to setup a new project in order to do the next exercise.
In this template, you can simply add a program by:
- open
CMakeLists.txtin the root folder. - add
add_program(<program_name> <source_file1> [source_file2...]). For example,add_program(example_single src/example_single/main.cpp)adds an executable namedexample_single, with its associated code file located atsrc/example_single/main.cpp. This code file contains anint main()function, which serves as the entry point for the program. After adding this line ofadd_program, we can use CMake to generate the program from the corresponding code and then execute the program.add_program(example_multiple src/example_multiple/main.cpp src/example_multiple/hello.cpp)adds an executable namedexample_multiple, with its associated code files located atsrc/example_multiple/main.cppandsrc/example_multiple/hello.cpp. Among these code files, there is only oneint main()function, which serves as the entry point for the program. After adding this line ofadd_program, we can use CMake to generate the program from the corresponding code and then execute the program.
- Reconfigure the project by using some button or reopening the IDE.
The headers used in book is configured correctly by default, just do the add_program step, then you can #include "PPP.h" or #include "PPPheaders.h" freely.
It's highly recommended to put your code inside src folder.
As for header files (.h, .hpp, etc.), you can simply put them together with source files. Then source files will be able to correctly #include "<header_file>". For example, in src/example_multiple folder, hello.cpp can #include "hello.hpp" directly.
If you want to make a header file includable globally, you can put it inside include folder. For example, in src/example_single folder, main.cpp can #include "add.hpp" which is put inside include folder.
NOTE: In order to make Qt (in chapter 12-16) work correctly, you should also add header file paths in your add_program like source file paths.
Currently the module feature is not supported well. If your #include "PPP.h" issues error, you should use #include "PPPheaders.h" instead of #include "PPP.h".
TLDR: If you're using clang 18+ as the compiler or the latest version of Visual Studio 2022 as the IDE, you might be lucky enough to successfully #include "PPP.h". But for now, you'll usually fail, and continuing to tinker with it will leave you physically and mentally exhausted.
click here to see current tools support for module. What's more,
- for MacOS homebrew clang users: There's a bug for homebrew clang with CMake's standard module library support, which requires your manual fix. After fix, you should remove the if condition in the FIXME part of
cmake/detect_std_module.cmakefile to enable module support for this project template. - for clangd users (possibly using VSCode, Qt Creator, vim, etc.): Although clangd has supported module since 19, it hasn't supported it very well.
In chapter 12-16, we use Qt as the graph library (see how to install it below).
However, the example code in the textbook fails and issues QWidget: Must construct a QApplication before a QWidget. Please check src/example_gui/main.cpp to learn how to fix it.
In a nutshell, you should put these lines of code around the example code:
int main() {
using namespace Graph_lib;
Application app;
/* example code */
app.gui_main();
}Here I provide three ways to install Qt. After installation, You're able to use it in this project template directly.
- Download Qt installer in this link.
- Double click the downloaded Qt installer to install it.
- Clear the CMake cache and reconfigure CMake in your IDE in some way (for example, you might delete the build or out folder and restart the software). Then you can use Qt in this project template.
For more details, see villevoutilainen/ProgrammingPrinciplesAndPracticeUsingQt.
Edit CMakeLists.txt, add a line run_vcpkg() between include(fetch_project_options) and project(cpp_novice LANGUAGES CXX). That is:
cmake_minimum_required(VERSION 3.25)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(fix_msvc)
include(enable_cpp_module NO_POLICY_SCOPE)
include(cpp_novice_fetch_project_options)
run_vcpkg()
project(cpp_novice LANGUAGES CXX)Clear the CMake cache and reconfigure CMake in your IDE in some way (for example, you might delete the build or out folder and restart the software). Then if you're lucky, the installation should have happened automatically.
- Install conan 2 somehow. For example, you can download it from the official website.
- Similarly, add
run_conan()betweeninclude(fetch_project_options)andproject(cpp_novice LANGUAGES CXX). - Clear the CMake cache and reconfigure CMake in your IDE in some way (for example, you might delete the build or out folder and restart the software).
If you're lucky, the installation should have happened automatically.
See README_install_thirdparty_libraries.
I learnt cmake mostly from Modern CMake for C++; my C++ learning map is listed in 学习大纲 (although the table of contents is in Chinese, almost all resources in it are in English).
What's more, this repository highly depends on aminya/project_options, which improves the CMake experience a lot.
For conan 2.0, the official documentation is helpful.
Details about this repository can be found in 对配置文件的解释.