Set up a build system using CMake and VSCodium

2022-04-30 0 By

Provide an appropriate CMake profile to make it easier for others to build, use, and contribute to your project.This article is part of a series on C/C++ development using open source DevOps tools.If you build your project on a powerful tool chain from the start, your development will be faster and more secure.Among other things, this will make it easier for others to get involved in your project.In this article, I will build a C/C++ build system based on CMake and VSCodium.As usual, sample code can be found on GitHub.I have tested the steps described in this article.This is a solution that works for all platforms.Why CMake?CMake is a build system generator that creates makefiles for your project.What seems simple at first glance can be quite complex.At a high level, you can define the various parts of your project (executables, libraries), compilation options (C/C++ standards, optimizations, architectures), dependencies (header files, libraries), and file-level project structure.The information used by CMake is available in the file cmakelists.txt, written in a special descriptive language.When CMake processes this file, it will automatically detect the compiler installed on your system and create a Makefile to start it.In addition, the configuration described in cmakelists.txt can be read by many editors, such as QtCreator, VSCodium/VSCode, or Visual Studio.Example Program Our example program is a simple command-line tool that takes an integer as an argument and outputs a random number ranging from 1 to the supplied input value.In our main function in the executable, we only process input parameters and exit the program if we don’t provide a value (or a value that can’t be processed).int main(int argc, char** argv){ if (argc != 2) {STD ::cerr ut_of_range&) {STD :: cerproducer.cpp the actual work is done in the generator, which will be compiled,It will be a static library to link to our Producer executable.std::stringstream &Generator::generate(std::stringstream &stream, Const int range) {STD ::vectorGenerator. CPP the function generate references a STD ::stringstream and an integer as arguments.Make a vector of integers in the range 1 to n, based on the integer range n, and then scramble it.The scrambled vector value is then converted to a string and pushed into the Stringstream.This function returns the same Stringstream reference as passed as an argument.The top cmakelists. TXT is the entry point of our project.There may be multiple cmakelists.txt files in subdirectories (for example, libraries or other executables associated with the project).Let’s take a step-by-step look at the top level of cmakelists.txt.The first line tells us what version of CMake we need to process the file, the project name and its version, and the predetermined C++ standard.Cmake_minimum_required (VERSION 3.14) project (CPP_Testing_Sample VERSION 1.0) set (CMAKE_CXX_STANDARD17)set(CMAKE_CXX_STANDARD_REQUIRED True) We tell CMake to look at the subdirectory Generator with the following line.This subdirectory contains all the information for building the Generator library and contains its own cmakelists.txt.We’ll talk about that in a minute.Add_subdirectory (Generator) Now we’ll cover one absolutely special feature: the CMake module.Loading modules can extend CMake functionality.In our project, we load the FetchContent module, which enables us to download external resources while CMake is running, in our case GoogleTest.include(FetchContent)FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/bb9216085fbbf193408653ced9e73c61e7766e80.zip)FetchContent_MakeAvailable(gooGletest) in the next section, we’ll do what we normally do in a regular Makefile: specify which binaries to build, their associated source files, which libraries to link to, and where the compiler can find the header files.add_executable(Producer Producer.cpp)target_link_libraries(Producer PUBLIC Generator)target_include_directories(ProducerPUBLIC “${PROJECT_BINARY_DIR}”) with the following statement, we make CMake to create a file named compile_commands.json in the build folder.This file shows the compiler options for each file in the project.Loading the file in VSCodium tells the IntelliSense function where to look for the header file (view the document).The last part of the set(CMAKE_EXPORT_COMPILE_COMMANDS ON) defines some tests for our project.The project uses the previously loaded GoogleTest framework.The whole topic of unit testing will be relegated to another article.enable_testingadd_executable(unit_test unit_test.cpp)target_link_libraries(unit_test Gtest_main include(GoogleTest)gtest_discover_tests(unit_test) library level cmakelists.txt now,Let’s take a look at the cmakelists.txt file in the subdirectory Generator that contains the library of the same name.The contents of the cmakelists.txt file are shorter, containing only two statements except for the unit test-related commands.add_library(Generator STATIC Generator.cpp Generator.h)target_include_directories(Generator INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) we use add_library(…)To define a new build target: a static Generator library.We use the statement target_include_directories(…)To add the current subdirectory to the search path for the headers of other build targets.We also specify the scope of this property as type INTERFACE: this means that the property affects only the build target linked to the library, not the library itself.By using the information in the cmakelists.txt file, an IDE like VSCodium can configure the build system accordingly.If you have no experience with VSCodium or VS Code, this sample project is a good place to start.First, go to their web sites and download the latest installation packages for your system.Open VSCodium and navigate to the “Extend Extensions” TAB.To build, debug, and test your project properly, search for the following extensions and install them: If not, Clone the Repository by clicking “Clone Git Repository” on the start page.Or manually input: git clone https://github.com/hANSIc99/cpp_testing_sample.git, by entering the following devops_1 to check out the tag:Git Checkout Tags /devops_1 or, by clicking the “Main” branch button (red box) and selecting the tag from the drop-down menu (yellow box).After you open the root folder of the repository within VSCodium, the CMake Tools extension detects the cmakelists. TXT file and immediately scans your system for the appropriate compiler.You can now click the “Build” button (red box) at the bottom of the screen to begin the Build process.You can also change the compiler by clicking the button (yellow box) marker in the bottom area, which displays the currently active compiler.To start debugging the Producer executable, click the debugger symbol (yellow box) and select debug Debug&nbsproducer (green box) from the drop-down menu.As mentioned above, the Producer executable requires the number of elements as a command-line argument.The command line arguments can be specified in.vscode/launch.json.Now you can build and debug your project.Thanks to CMake, the above steps should work no matter what operating system you’re running.Especially with cma-related extensions, VSCodium becomes a powerful IDE.I didn’t mention Git integration for VSCodium because you can already find a lot of resources on the web.I hope you can see that providing a proper CMake profile makes it easier for others to build, use, and contribute to your project.In future articles, I’ll cover unit testing and CMake’s testing utility, CTest.via: https://opensource.com/article/22/1/devops-cmake: author Stephan Avenwedde topics: lujun9972 translator: robsean proofreading: wxy original compilation, this paper by LCTT Linux honor launch in China