![]() Nothing new here – the code is compiled down to an object file which can later be used to build (link) and executable or library. "command": "/usr/bin/c++ -o CMakeFiles/Add.dir/ -c /home/user/hello_cmake/add.cpp", We should already have an idea of what to expect. So how did cmake go about compiling our library? Let’s take a look at the compile_commands.json first. This is a generator-agnostic way of building projects using CMake, which is rather useful for scripting. Previously we relied on invoking make directly – here we tell cmake that we’d like to build (rather than configure) the project with the -build parameter and point it to an already configured build directory. ![]() Then, to build the project we run cmake -build build. The -B flag specifies the build directory (created if necessary), and the -S specifies the source directory. Instead of manually creating the build directory, cd‘ing to it, and invoking cmake there, we do everything with a single command. ![]() From the project root directory run the following: $ cmake -B build -S. We’ll take this opportunity to introduce a different way of configuring and building projects with CMake. Let’s configure and build the project so that we can inspect the output. This is all that’s required to build a library. are able to list them in the project explorer. for Visual Studio) that are aware of these files – i.e. The only required one is add.cpp, headers are optional, listing them, however, may however help CMake better track changes to these files and generate IDE project files (e.g. We give the target a name – Add – this will also be the name of the library itself. Same as add_executable this command defines a target, which encapsulates all of the details necessary to compile a library. Next, we introduce our library: add_library(Add Setting the variable to OFF, NO or FALSE would have had the opposite outcome. Setting this variable to YES or TRUE would have had the same effect. However, CMake interprets some string values as true and some as false, meaning that when evaluated they behave as if they were of type bool. Optionally, we could have quoted the literal, like so: set(CMAKE_EXPORT_COMPILE_COMMANDS “ON”), these calls are equivalent – literals are also of type string. We’ve set the value of the variable to ON. Commands may interpret a variable differently, but the underlying type is always string.Īlso, note that variables with the prefix CMAKE_ are either provided by CMake or are otherwise meaningful to CMake when set by the project. There aren’t really any other data types in CMake. We will gloss over the details, and for now note just that a normal CMake variable is visible from the point of definition onward, and is always of type string. In addition to normal variables set can operate on cache and environment variables, but we will not discuss those for now.ĬMake variables behave more or less the same way one would expect – they obey scope rules and have a type. The form of the set command presented here operates on what CMake documentation calls a normal variable. The set command defines or assigns a value to a variable. We still need them, since we’d like to look at exactly what’s going on. Here we set them in the CMakeLists.txt, so that we don’t have to do it every time we configure the project. These are the same variables we’ve previously set via command-line. The CMakeLists.txt that defines the project looks as follows: cmake_minimum_required(VERSION 3.19) Int Add::add(int l, int r, calc_status& cs) noexcept Int add(int l, int r, calc_status& cs) noexcept Alternatively, a git gist for the complete example is available in the reference section. If you wish to follow along you can copy-paste, or type-in the listings. Let’s assume that we’re very concerned about all the possible integer-operation failure modes and that we’re unable to use exceptions for some reason, so we decide to use simple enum error codes to report them (this will come into play later). To illustrate our example we will define an extremely simple library that exposes just a single function – add, which adds two integers. In this part, we will look at how to build libraries, and use static libraries with CMake. ![]() In the first post of the series we already looked at the very basics of defining a CMake-based project – we know how to write a CMakeLists.txt file and use it to compile a single or multi-source binary.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |