This will allow us to use modern C++ features (available in C++ 11 or later) such as the auto keyword or raw string literals later on. You can read more about this in the CMake documentation.Īlso note how we set the CMAKE_CXX_STANDARD variable. If the headers were used only to compile the library, we would have used the PRIVATE keyword, and if they were used only by consumers of the library, we would have used the INTERFACE keyword instead. (We used the -I include/ flag both for building libchucknorris.o and ) The target_include_directories informs CMake that there are header files in the include directories that should be used both when builing the library itself, but also by consumers of the library. The add_library and add_executable commands describe the two targets (named chucknorris and cpp_demo) and the sources used to build them, and the target_link_libraries command tells CMake that cpp_demo depends on chucknorris. You can provide it with a separate CMake file to be executed, or simply list a sequence of COMMANDS for it to invoke.Cmake_minimum_required( VERSION 3.10) set( CMAKE_CXX_STANDARD 11) project( ChuckNorris) add_library( chucknorris include/ChuckNorris.hpp src/ChuckNorris.cpp ) target_include_directories(Ĭhucknorris PUBLIC "include" ) add_executable( cpp_demo src/main.cpp ) target_link_libraries( cpp_demo chucknorris) This is typically used to allow the user to control whether to compile in various sub-modules or to specify alternate libraries.Īdd_custom_target() is used to create additional make targets, like 'make dist', 'make uninstall', 'make check', and so on. Option() lets you establish cmake options the user can use to control the build. A common use would be to substitute the current release version number into C files or documentation, or to fill in configuration values into a config.h.in template. Let's take a quick overview of a few of the other common function calls that you'll run across in Inkscape's CMake files.Ĭonfigure_file() is a convenient way to process template files and substitute parameters in them with the values of variables within the cmake script. It has a bunch to help determine what platform the build is building for:ĬMake also has a slew of variables to keep track of directories that things should go to: Note that this will require a full-rebuild of the whole of InkscapeĪs you can imagine, CMake provides a broad set of pre-defined variables that relate to build systems. If you end up in a dead-end with things not building anymore, you can clean the cmake cached files by issuing the full gcc command line used for compilation, by adding VERBOSE=1 to make: When you produce Makefiles using cmake, and make fails, you can get verbose output e.g. Print full command line used when building The '.' argument to the cmake command tells CMake to look in the current directory for source files. CMake assumes that the current directory is where we want all our stuff built to. Build files have been written to: /tmp/cmake-tutorialĬMake will automatically recognize "CMakeLists.txt" as its config file. Now run cmake in the build directory, specifying the path to the source directory: The folder cmake-tutorial is your source folder from cmakes perspectiveĪnd you need a build directory to run cmake without not mess up the source folder with build artefacts: Type the following lines into this CMakeLists.txt file: You now know enough to create a trivial cmake program.Ĭreate an empty directory and open a file named 'CMakeLists.txt' in your favorite text editor. Variables are referenced using a dollar sign and curly brackets: So, these are all functionally equivalent in this case: Strings don't always have to be quoted, although it's also a good convention to follow. We'll adopt the convention of keeping function names lower case, and variable names upper case. CMake isn't strict about casing for function names, so SET() and set() are equivalent, but it is strict about variable name casing. It's fine to add newlines between arguments inside the parens. Essentially, everything is a function call.Įven just setting variables is a function call:įunction calls have parenthesis but arguments aren't separated with commas, just whitespace. 3 Print full command line used when buildingĬmake has its own language and syntax, but it's pretty simple.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |