Thanks to CMake...
Less from Make (Makefiles)
Cmake was created in an effort to reduce the laborious effort that goes into writing lines of code for Makefiles. It’s perhaps not evident until working with larger projects, but it’s always valuable to work through a Hello World
.
C++ Hello World
Let's get some info on our compiler.
More info on GCC 7 release series or use an Online Compiler
$ g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Clone supporting repo for this tidbit
$ git clone https://github.com/RogerSGarcia/thankstocmake.git
Cloning into 'thankstocmake'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 11 (delta 0), reused 8 (delta 0), pack-reused 0
Unpacking objects: 100% (11/11), done.
$ ls
thankstocmake
$ cd thankstocmake/; ls
helloworld helloworld_cmake helloworld_make README.md
Compile program using the command line
$ cd helloworld; ls
helloworld.cpp
$ g++ helloworld.cpp -o hello.out
$ ls
hello.out helloworld.cpp
$ ./hello.out
Hello World
Compile program using a Makefile
$ cd ../helloworld_make/
$ ls
helloworld.cpp Makefile
$ make
g++ helloworld.cpp -o hello.out
$ ls
hello.out helloworld.cpp Makefile
$ ./hello.out
Hello World
$ make clean
rm hello.out
Compile program using cmake
. In general, you have a CMakeLists.txt
file which contains commands that will generate our Makefile
, this then allows us to invoke make
like in the previous example (Awesome).
This may be old news to you if your frequently working with code, in particuarly C/C++, but it's pretty cool, despite different git-repositories/projects/libraries/ there's a common practice where source files are located and how you build your project using cmake
:
- src
- directory where source code is located.
- build
- directory you create with
mkdir
command and then run the command "cmake ..
", you may find that a project (or yours) use different modes:
Release
for an optimization build or aDebug
mode for making your life easier when debugging (usinggdb
), so basically you can create a two directories for each mode:build-release
directory andbuild-debug
$ cd ../helloworld_cmake/
$ ls
CMakeLists.txt src
$ mkdir build
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/roger/ws/garcroge/tidbits_repo/thankstocmake/helloworld_cmake/build
$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
$ make -j8
Scanning dependencies of target hello.out
[ 50%] Building CXX object CMakeFiles/hello.out.dir/src/helloworld.cpp.o
[100%] Linking CXX executable hello.out
[100%] Built target hello.out
$ ./hello.out
Hello World
Basically, CMake faciliates building Makefiles, but you will still need to invoke Make
in order to call the compiler from the Makefile that contains bunch of flags and source files.
CMake | Make (Makefile) |
---|---|
generator of buildsystems | a buildsystem |
projects/packages | ... |
guess compiler and flags | make sure you indent |
full scripting, globbing, and a cache | good luck |
fewer lines of code | lines of code |