Config CMake on Windows

Published:
Updated:
2 minute read

Using pip, it’s pretty straightforward:

pip install cmake

In a sample project create the CMakeLists.txt, something like this:

cmake_minimum_required(VERSION 3.5.1)

set(CMAKE_CXX_STANDARD 14)

project(HelloCmake)

add_executable(HelloCmake src/main.cpp src/vect_add_one.cpp src/increment_and_sum.cpp)

Then move the build directory and run:

cmake ..

If you are using Git bash in Windows with MinGW toolchain, run this:

cmake -G "MSYS Makefiles" ..

Followed by make.

Another straightforward option to make life easier

Set this alias in ~/.bashrc :

alias cmake='cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug "-DCMAKE_C_COMPILER:FILEPATH=C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gcc.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -G "Unix Makefiles"'

Remember to replace the paths with the correct ones.

Useful functions

For full documentation, see here.

FunctionExplanation
configure_file(file.in file.in COPYONLY)Copy a file from the source directory to the destination directory (build). It’s useful for copying input files and config files.
set(srcs file1.c file2.c ...)Creates a variable (e.g. src), and assigns something to it (e.g. the list file1.c file2.c). To clear a variable, do not provide second argument, e.g. set(srcs).
include_directories(dir1 dir2 ...)Adds the provided directory paths to the compilers list of directories that it will search for include files in, for any following targets.
add_library(name STATIC/SHARED/MODULE file1.c file2.c ...)Adds a library target that will be build from the provided source files. DO NOT APPEND lib to the name (this is done automatically by cmake depending on architecture).
add_executable(name file1.c file2.c ...)Adds an executable target (as opposed to a library target).
link_libraries(lib_1 lib_2 ...)Links the provided libraries to all following targets in the CMakeLists.txt file. This is deprecated. It is recommended you use target_link_libraries() instead.
target_link_libraries(target_lib other_lib_1 other_lib_2 ...)Links the provided libraries to the specific target library. link_libraries() can be used to apply to libraries to all following targets (i.e. no specific target is provided), however, it is deprecated.
install(TARGETS targets...)Used to place build output into certain directories on the user’s system (as well as do things like assign privileges to these files).

Test the project executables with CTest

In the Cmake file, add the following lines:

include(CTest)

add_test(NAME validate COMMAND ${PROJECT_NAME})
set_tests_properties(validate PROPERTIES PROCESSOR_AFFINITY ON PROCESSORS 4)

Then, after running cmake .. and make in the build directory, run this:

cmake -T memcheck -VV
# -VV means verbose

If you have valgrind installed, this command runs your test(s) and valgrind’s memcheck on your project.