![]() We can fix this by changing the CMakeLists.txt a bit, and add the header files as components of the executable. cpp files into a project, without any grouping or headers, as shown in this picture: The provided CMakeLists.txt template works well for building the project, but does not generate good project files, as it just dumps all. Note that we did not have to do any work to track dependencies between files, as CMake does that for us. With this, we have a CMake build file that lets us build our toy project with GCC/Clang on Linux/OS X/BSD/others and with MSVC on Windows, with a reasonable set of warnings and using C++14 features. If ( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" ) Target_compile_options( vector-test PRIVATE -Wweak-vtables -Wexit-time-destructors -Wglobal-constructors -Wmissing-noreturn ) If ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" ) Target_compile_options( vector-test PRIVATE -Wall -Wextra -Wunreachable-code -Wpedantic) This is a bit of a sore spot because CMake does nothing to help you with setting (un)reasonable warning levels in a cross-platform fashion, so you have to do it yourself, by using appropriate flags for each compiler, like so : if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang|GNU" ) You might have noticed that there are now no warnings. Enabling CMAKE_CXX_STANDARD_REQUIRED tells CMake to fail generation step if C++14 is not supported (the default is to keep going with older standard) and disabling CMAKE_CXX_EXTENSIONS tells CMake to prefer flags that do not enable compiler-specific extensions - this means that GCC will be given -std=c++14 rather than -std=gnu++14. For GCC/Clang this is -std=c++14 (or -std=gnu++14), for MSVC this is nothing (it supports C++14 by default). Setting CMAKE_CXX_STANDARD to 14 tells CMake that we want to add whatever flags are needed for our compiler to be able to compile C++14. There are some arguments for setting it per-target, and some good arguments against, but at the time of writing this I am against setting C++ standard per-target. The desired C++ standard is still set globally. We also told CMake that this project will use only C++ - this cuts down the time it needs to create projects, as it does not have to look for a C compiler, check if it works, etc. Notice that we had to bump the required CMake version for this to work. The modern CMake version of the CMakeLists.txt for the same toy problem is this: cmake_minimum_required(VERSION 3.5) The proper way, sometimes also called modern CMake, minimizes the use of global settings and combines using target-specific properties with CMake's understanding of building C++. This is not a problem for a trivial build like this, but as with many things, it is better to get into the habit of doing things the correct way right from the start. The second is that it changes compilation flags and include paths globally, for all binaries/libraries. The first problem is that it is non-portable because it sets GCC/Clang specific flags ( -Wall, -std=c++14) globally, no matter the platform/compiler. This is an example of CMakeLists.txt that contains two fundamental problems, that are painfully common. A large problem with CMake is that there are many tutorials giving bad advice, including its own documentation. ![]() it can output MSBuild files for Visual Studio when used on Windows, but can also output makefiles when used on Linux.ĬMake works by reading a single input file named CMakeLists.txt and generating platform-specific files for different build systems from the declarations and commands within. What this means is that CMake does not build things, it generates files for other build systems to use. CMakeĬMake is cross-platform meta build-system. It is important to note that these tutorials are not meant to build a bottom-up understanding of either of the two, but rather provide a person with an easy-to-modify template that they can use for themselves and quickly get back to the interesting part - their code. Their exact contents do not matter, but main.cpp includes vector.hpp, vector.cpp includes array.hpp and both vector.cpp and array.cpp include their respective headers, vector.hpp and array.hpp. It is start of an implementation of growing array (ala std::vector), consisting of 5 files: Through these tutorials, I'll use a very simple example from one of our labs. This is the CMake tutorial, the Make one can be found here. After seeing the same errors repeated over and over again, I decided to write a short tutorial towards writing simple Makefiles and CMakeLists. The end of a semester is here and, as I grade our student's semestral works, I get to use Makefiles and CMakeLists of dubious quality. By Martin Hořeňovský May 20th 2018 Tags: CMake, Tutorial, C++
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |