A template CMake project to get you started with C++ and tooling
First, click the green
Use this templatebutton near the top of this page. This will take you to Github's 'Generate Repository' page. Fill in a repository name and short description, and click 'Create repository from template'. This will allow you to create a new repository in your Github account, prepopulated with the contents of this project. Now you can clone the project locally and get to work!
$ git clone https://github.com//.git
If you know you're not going to use one or more of the optional gui/graphics frameworks (fltk, gtkmm, imgui, etc.), you can remove them with
$ git rm -r src/
sudo apt install build-essential
choco install mingw -y
brew install gcc
bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
Visual Studio 2019 ships with LLVM (see the Visual Studio section). However, to install LLVM separately:
choco install llvm -y
llvm-utils for using external LLVM with Visual Studio generator:
git clone https://github.com/zufuliu/llvm-utils.git cd llvm-utils/VS2017 .\install.bat
brew install llvm
On Windows, you need to install Visual Studio 2019 because of the SDK and libraries that ship with it.
Visual Studio IDE - 2019 Community (installs Clang too):
choco install -y visualstudio2019community --package-parameters "add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --includeOptional --passive --locale en-US"
Put MSVC compiler, Clang compiler, and vcvarsall.bat on the path:
choco install vswhere -y refreshenv
# change to x86 for 32bit $clpath = vswhere -products * -latest -prerelease -find **/Hostx64/x64/* $clangpath = vswhere -products * -latest -prerelease -find **/Llvm/bin/* $vcvarsallpath = vswhere -products * -latest -prerelease -find **/Auxiliary/Build/* $path = [System.Environment]::GetEnvironmentVariable("PATH", "User") [Environment]::SetEnvironmentVariable("Path", $path + ";$clpath" + ";$clangpath" + ";$vcvarsallpath", "User") refreshenv
- Via pip - https://docs.conan.io/en/latest/installation.html#install-with-pip-recommended
pip install --user conan
choco install conan -y
brew install conan
sudo apt-get install cmake
choco install cmake -y
brew install cmake
sudo apt-get install doxygen sudo apt-get install graphviz
choco install doxygen.install -y choco install graphviz -y
brew install doxygen brew install graphviz
sudo apt-get install ccache
choco install ccache -y
brew install ccache
sudo apt-get install cppcheck
choco install cppcheck -y
brew install cppcheck
Follow instructions here: https://github.com/include-what-you-use/include-what-you-use#how-to-install
This project can be made to work with several optional GUI frameworks.
If desired, you should install the following optional dependencies as directed by their documentation, linked here:
The following dependencies can be downloaded automatically by CMake and Conan. All you need to do to install them is to turn on a CMake flag during configuration. If you run into difficulty using them, please refer to their documentation, linked here:
Make a build directory:
By default (if you don't set environment variables
CXX), the system default compiler will be used.
Conan and CMake use the environment variables CC and CXX to decide which compiler to use. So to avoid the conflict issues only specify the compilers using these variables.
CMake will detect which compiler was used to build each of the Conan targets. If you build all of your Conan targets with one compiler, and then build your CMake targets with a different compiler, the project may fail to build.
Set your desired compiler (
Temporarily (only for the current shell)
Run one of the followings in the terminal:
~/.bashrc using your text editor:
CXX to point to the compilers:
export CC=clang export CXX=clang++
Save and close the file.
Run one of the followings in PowerShell:
Visual Studio generator and compiler (cl)
[Environment]::SetEnvironmentVariable("CC", "cl.exe", "User") [Environment]::SetEnvironmentVariable("CXX", "cl.exe", "User") refreshenv
Set the architecture using vsvarsall:
[Environment]::SetEnvironmentVariable("CC", "clang.exe", "User") [Environment]::SetEnvironmentVariable("CXX", "clang++.exe", "User") refreshenv
[Environment]::SetEnvironmentVariable("CC", "gcc.exe", "User") [Environment]::SetEnvironmentVariable("CXX", "g++.exe", "User") refreshenv
Temporarily (only for the current shell):
To configure the project and write makefiles, you could use
cmakewith a bunch of command line options. The easier option is to run cmake interactively:
1) Open cmake-gui from the project directory:
cmake-gui .2) Set the build directory:
3) Configure the generator:
In cmake-gui, from the upper menu select
Warning: if you have set
CXXalways choose the
use default native compilersoption. This picks
CXX. Don't change the compiler at this stage!
Choose MinGW Makefiles as the generator:
You should have already set
Choose "Visual Studio 16 2019" as the generator:
You should have already set
Choose "Visual Studio 16 2019" as the generator. To tell Visual studio to use
ClangClunder "optional toolset to use".
LLVM_v142under "optional toolset to use".
4) Choose the Cmake options and then generate:
with the Cmake Curses Dialog Command Line tool:
ccmake -S . -B ./build
ccmakehas finished setting up, press 'c' to configure the project, press 'g' to generate, and 'q' to quit.
Once you have selected all the options you would like to use, you can build the project (all targets):
cmake --build ./build
For Visual Studio, give the build configuration (Release, RelWithDeb, Debug, etc) like the following:
cmake --build ./build -- /p:configuration=Release
Many problems that users have can be resolved by updating Conan, so if you are having any trouble with this project, you should start by doing that.
To update conan:
$ pip install --user --upgrade conan
You may need to use
pipin this command, depending on your platform.
If you continue to have trouble with your Conan dependencies, you can try clearing your Conan cache:
$ conan remove -f '*'
The next time you run
cmake --build, your Conan dependencies will be rebuilt. If you aren't using your system's default compiler, don't forget to set the CC, CXX, CMAKECCOMPILER, and CMAKECXXCOMPILER variables, as described in the 'Build using an alternate compiler' section above.
If you have a dependency 'A' that requires a specific version of another dependency 'B', and your project is trying to use the wrong version of dependency 'B', Conan will produce warnings about this configuration error when you run CMake. These warnings can easily get lost between a couple hundred or thousand lines of output, depending on the size of your project.
If your project has a Conan configuration error, you can use
conan infoto find it.
conan infodisplays information about the dependency graph of your project, with colorized output in some terminals.
$ cd build $ conan info .
In my terminal, the first couple lines of
conan info's output show all of the project's configuration warnings in a bright yellow font.
For example, the package
spdlog/1.5.0depends on the package
fmt/6.1.2. If you were to modify the file
cmake/Conan.cmakeso that it requires an earlier version of
fmt, such as
fmt/6.0.0, and then run:
$ conan remove -f '*' # clear Conan cache $ rm -rf build # clear previous CMake build $ mkdir build && cd build $ cmake .. # rebuild Conan dependencies $ conan info .
...the first line of output would be a warning that
spdlogneeds a more recent version of
See Catch2 tutorial