

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) Where do I start with CMake This step will provide an introduction to some of CMakes basic syntax, commands, and variables. Then in main C write cmakelists in the same directory Txt, as follows, #include Compatible build tools include Make, Ninja, and others - see CMake Generators for more information. 6 min read - 1 Photo by Yancy Min on Unsplash If you have ever worked or written a C or C++ project, you have probably heard of the build system called CMake.

CMake and a compatible build tool for building the project. A compatible C++ compiler that supports at least C++14.
#Cmake tutorial c code#
Two simple examplesįirst, let's start with the simplest code and experience how cmake operates. To complete this tutorial, you’ll need: A compatible operating system (e.g.
#Cmake tutorial c install#
To install cmake, use the following command:,Īfter installation, enter cmake -version under the terminal to view the cmake version,

#Cmake tutorial c how to#
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. Do not edit by hand -*- cmake_minimum_required ( VERSION 2.8.CMake is an open source and cross platform construction tool, which allows us to generate local Makefile by writing a simple configuration file. # -*- CMakeLists.txt generated by CodeLite IDE. It provides step-by-step instructions on how to use CMake to build a simple. Now you are ready to build your project, by simply clicking F7Īnd you should get an output similar to this: CMake Tutorial: The official CMake tutorial is a great place to start for beginners. Build files have been written to: C:/Users/Eran/Desktop/Test/CMakeTest/cmake-build-Debug/CMakeTest Check for working CXX compiler: C:/compilers/mingw64/bin/g++.exe - works Check for working CXX compiler: C:/compilers/mingw64/bin/g++.exe Check for working C compiler: C:/compilers/mingw64/bin/gcc.exe - works Check for working C compiler: C:/compilers/mingw64/bin/gcc.exe The CXX compiler identification is GNU 7.3.0 The C compiler identification is GNU 7.3.0 Adding user code to a generated CMakeLists.txtĬ : \CMake\bin\cmake.exe C:\Users\Eran\Desktop\Test\CMakeTest -G"MinGW Makefiles"
