Makefile vs CMake Client. The client can be compiled both with the historical Makefile and with a newer CMakeLists.txt. At the moment both are maintained because they don't perfectly overlap yet. CMake generates files for other build systems. These can be Makefiles, Ninja files or projects file for IDEs like Visual studio or Eclipse. The build files contains calls to compilers like GCC, Clang or cl.exe. If you have several compilers installed, you can choose one. There are a million different ways to 'do' C projects, but using CMake and the google testing framework has my preference. That's not to say that using a Makefile or Boost Unit Test is bad, use whatever suits your needs. This guide however will focus on just CMake and gtest. It assumes a system running Ubuntu (18.04).
This article will describe converting Makefile source tree to CMakeLists.txt source tree manually.
Table of Contents
- 2. Makefile source tree
- 3. CMakeLists.txt source tree
1 Makefile and CMakeLists.txt
CMakeLists.txt is similar with configure and Makefile. Unix can generate Makefile from CMakeLists.txt with cmake command. This Makefile supports include file dependencies and parallel build. Amount of statement in CMakeLists.txt is equal with Makefile.
2 Makefile source tree
2.1 Makefile at top directory
Makefile at top directory is as below.
- Build process is done in Makefile of lib and src direcotry. Because src directory needs library in lib directory, build process in lib directory is done before src directory.
2.2 Makefile for library
Makefile in lib directory is as below.
- Getting objects from .cpp source code, creating libexample.a from objects with ar command. Objects will be built with implicit rule.
- Writing dependencies for object in .d file, running build process when header file which is included by source code is update.
2.3 Makefile for binary which uses library
Makefile in src directory is as below.
- Getting objects from .cpp source code, creating example from objects with c++ command. Objects will be built with implicit rule.
- .d file is as same as Makefile for library.
2.4 Build with Makefile
3 CMakeLists.txt source tree
3.1 CMakeLists.txt at top directory
CMakeLists.txt at top directory is as below.
- The cmake command can separate build directory from source tree. Because the cmake command generates some files, force separating build directory from source tree with checking CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR.
- Add lib directory and src directory to CMakeLists.txt source tree with add_subdirectory. Not writing dependency of lib directory and src directory. The dependency will be determined by target_link_libraries of CMakeLists.txt in src directory.
3.2 CMakeLists.txt for library
CMakeLists.txt in lib directory is as below.
- Set current directory source codes to variable with AUX_SOURCE_DIRECTORY.
- Create libexample from source codes in variable with add_library. Because this library name is liblibexample.a, rename to libexample.a with set_target_properties.
- Publish library with target_include_directories.
3.3 CMakeLists.txt for binary which uses library
CMakeLists.txt in src directory is as below.
- Setting current directory source codes to variable with AUX_SOURCE_DIRECTORY.
- Creating example from source codes in variable with add_executable. This binary name is example.
- Refer libexample with target_link_libraries. This libexample is name which is set by target_include_directories.
3.4 Build with CMakeLists.txt
Running cmake command create Makefile. Running make command build source code again when source code is update. Need to run cmake command when CMakeLists.txt is update.
CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.
Plex shield tv currently unavailable. CMake is part of Kitware’s collection of commercially supported open-source platforms for software development.
Contact Us
Have a question about a CMake project? We can help
Training Courses
Book
Vogue artist. Get the latest edition of the Mastering CMake book
Netflix
“…CMake, CTest, and CDash have proven to be invaluable tools for us to build multiplatform code, track changes, run tests, and improve code quality by performing code coverage and memory leak analysis.”
Inria
“I am thankful for CMake’s shadow builds feature. For a project like SOFA, which has many optional features and possible option combinations, being able to quickly test a modification on several typical setups on my machine before pushing a significant change to the repository is a very valuable security. This is especially true for a complex project like ours.”
The HDF Group
“CMake, CTest, and CDash were the tools that we desperately needed. With the help of our user community, we added the CMake build system for all HDF products. We rely on CMake for HDF software being successfully built and tested on platforms that range from supercomputers to Windows desktops and tablets.”
ReactOS
“…in early 2010 a decision was made to look for a third party solution instead of continuing to expend time and effort maintaining RBuild. CMake was an early favorite and while the transition was not without its bumps, it has allowed the project to not only increase the number of compilers that could be used to build ReactOS, it also set the stage for significantly decreasing build times, making development faster and easier.”
KDE
“Our working relationship aside, CMake has greatly improved the process of building KDE. Projects using CMake take less time to get started, since there is less time spent fighting with the build system. One KDE developer says, “CMake doesn’t make you want to shoot yourself with a nailgun when building your project anymore.”
Second Life
“A great technology choice that our community helped us make was moving to CMake, a cross-platform build tool, which makes it much simpler to maintain makefiles for many different platforms.”
- Allegro library
- Armadillo
- Avidemux
- awesome
- BCI2000
- Blender
- BRL-CAD
- Bullet Physics Engine
- CGAL
- Chipmunk physics engine
- CLion
- Compiz
- Conky
- cURL
- Deal.II
- Doomsday Engine
- Dust Racing 2D
- Drishti
- Ettercap
- Falcon (programming language)
- FlightGear Flight Simulator
- GDCM
- Geant4
- Gmsh
- GNU Radio
- GROMACS
- Hiawatha (web server)
- Hypertable
- Hugin
- iCub robot and YARP
- IGSTK
- ITK
- KDE SC 4
- KiCad
- libpng
- LAPACK
- LLVM and Clang
- LMMS
- Mir
- MiKTeX
- MLPACK
- MuseScore
- MySQL and MariaDB
- OGRE
- OpenCV
- OpenCog
- OpenCPN
- OpenSceneGraph
- OpenSync
- Orthanc
- Point Cloud Library
- Poppler
- PvPGN
- QGIS
- Qt
- Raw Therapee
- ReactOS
- ROOT
- ROS
- Ryzom
- Scribus
- SDL
- Second Life
- SFML
- Spring RTS
- SuperTux
- Synergy
- Slicer
- Stellarium
- Trilinos
- Vortexje
- VTK and ParaView
- VXL
- zlib
- PCSX2
- Zdoom
- ZeroMQ
CMake 3.20.2 available for download
Cmake Makefile Tutorial
CMake 3.20.2 is now available for download …
CMake 3.20.1 available for download
CMake 3.20.1 is now available for download …
Cmake Generate Makefile
CMake 3.19.8 available for download
Cmake Call Makefile
CMake 3.19.8 is now available for download …