

In this example, a project with a typical directory structure is used. These Makefiles are then individually invoked by the Makefile in the parent directory.ĬMake can be very useful in this situation. Makefiles become more verbose when there are sub-directories present - in fact, it is usual practice to place an individual Makefile in each sub-directory. We can now examine more complex CMake examples.Īs your project grows, it is likely that you will organize it into sub-directories. It is important though, because it explains the basic operation of CMake. It works! However, this process is a somewhat excessive way to just build the HelloWorld.cpp program.

Building CXX object CMakeFiles/hello.dir/ Once the Makefile has been created, the make command can be used to build the make # The shell in which to execute make rules. # Generated by "Unix Makefiles" Generator, CMake Version 2.8 rw-r-r- 1 molloyd molloyd 4811 Apr 1 01:52 more Makefile Do not make edits to this Makefile, as any edits will be overwritten the next time that the cmake utility is lsĬMakeCache.txt CMakeFiles CMakeLists.txt Makefile cmake_install.cmake ls -l Makefile Build files have been written to: /home/molloyd/exploringBB/extras/cmake/helloworldĬMake identified the environment settings for the Linux device and created the Makefile for this project, which can be viewed. Check for working CXX compiler: /usr/bin/c++ - works Check for working CXX compiler: /usr/bin/c++ Check for working C compiler: /usr/bin/gcc - works

Check for working C compiler: /usr/bin/gcc The CXX compiler identification is GNU 4.6.3 The C compiler identification is GNU 4.6.3 Now you are ready to build the Hello World project using CMake - execute the cmake command and pass it the directory that contains the source code and the CMakeLists.txt file - in this case “. The project code is in the GitHub repository directory, where you will see only the two files described in Listings 1 and 2 cd ls For example, under sudo apt-get install cmake -version To build the project, first test that you have CMake installed, and if not, install it using the package manager that is used by your flavor of Linux. The first argument to the add_executable() function is the name of the executable to be built, and the second argument is the source file from which to build the executable.

The first line sets the minimum version of CMake for this project, which is major version 2, minor version 8, and patch version 9 in this example.The CMakeLists.txt file in Listing 2 consists of only three lines: It can do much more than that too (e.g., build MS Visual Studio solutions), but in this discussion I focus on the auto-generation of Makefiles for C/C++ projects. The make utility and Makefiles provide a build system that can be used to manage the compilation and re-compilation of programs that are written in any programming language. I use Makefiles quite often in my projects to automate the build process however, there are times when Makefiles become overly complex for the task - particularly when building projects that have multiple sub directories, or projects that are to be deployed to multiple platforms.īuilding complex projects is where CMake really shines - CMake is a cross-platform Makefile generator! Simply put, CMake automatically generates the Makefiles for your project. This article provides a straightforward set of “Hello World!” introductions to using CMake for building C++ projects. All steps are performed using Linux on the BeagleBone platform, but the instructions are relevant to most Linux platforms. 7 Example 5: Using a Shared or Static Library.6 Example 4: Building a Static Library (.a).5 Example 3: Building a Shared Library (.so).4 Example 2: A Project with Directories.
