The code and examples used in this blog are from CMake 3.16 on Ubuntu 20.04 LTS using the GNU Arm Embedded Toolchain and can be download from the GitHub project. It is the first of a small series of posts looking at how we build our training projects comprising application code, supporting library code, real-time operating system and bare metal driver code. This blog post is a mix of musings and advice when using CMake for cross-compiling to the STM STM32F407 Discovery board that we use for our embedded C and C++ training. For this reason, we’ve put aside our prejudices and reconsidered the way we build our example projects used during training by replacing scons with CMake. Here at Feabhas, we find ourselves falling in the latter category, despite the fact the CMake is widely used within the embedded and deeply embedded development community.īut we also know that many of the C/C++ static analysis and code quality tools integrate well with the CMake build system. In this post we will examine how to use CMake to mange the build process for a cross compilation project.ĬMake can be described as a marmite application: you either love it or hate it. Let's see how to use cmake in this case.In our previous post Why We Need Build Systems we examined the need for Build Systems in modern software development. Sometimes you just need to compile dynamic and static libraries and wait for other programs to use them. compilation control of dynamic library and static library This command is /bin)Īt the same time, cmakelists in the src directory Txt delete. Cmake provides a command that can store all the source files in the specified directory in a variable. However, if there are 100 source files, it will be a bit of a pit to do so again, which can not reflect the advantages of cmake. Then execute cmake again, generate Makefile and run make,īy analogy, if there are multiple source files in the same directory, just add_ Just add all the source files in the executable. Modify cmakelists Txt, in add_ In the parameter of executable, put testfunc C add in cmake_minimum_required (VERSION 2.8) Call testfunc The function func() declared in H, #include TestFunc.h the contents are as follows:, /* TestFunc.c the contents are as follows:, /* After adding, the overall file structure is as follows:, Let's move on to a slightly more complicated example: there are multiple source files in the same directory.Īdd two files in the previous directory, testfunc C and testfunc h. PS: if you want to regenerate main, enter make clean to delete the elf file main. You can see that the elf file main we need has also been successfully generated, and then run main, You can see that the Makefile generated by executing cmake can display the progress with color. Then enter make under the terminal and press enter, You can see that the Makefile is successfully generated, and there are some files automatically generated by cmake runtime. Let's look at the files in the directory, The following information will be output, ![]() Switch to main under the terminal C, and then enter the following command to run cmake, The first line means that the minimum version requirement of cmake is 2.8, and we installed 3.10.2 The second line represents the project information, that is, the project name is demo The third line is more critical, indicating that the name of the elf file to be generated is main, and the source file used is main c Then in main C write cmakelists in the same directory Txt, as follows, cmake_minimum_required (VERSION 2.8) Then in main C write cmakelists in the same directory Txt, as follows, #include Two simple examplesįirst, let's start with the simplest code and experience how cmake operates. To install cmake, use the following command:,Īfter installation, enter cmake -version under the terminal to view the cmake version, This article mainly describes how to use CMake to compile our program under Linux. This configuration file is independent of the running platform and compiler, so there is no need to write Makefile in person, and the configuration file can be directly used on other platforms without modification, which is very convenient. CMake is an open source and cross platform construction tool, which allows us to generate local Makefile by writing a simple configuration file.
0 Comments
Leave a Reply. |