Skip to content

Latest commit

 

History

History
201 lines (165 loc) · 6.25 KB

CMake.md

File metadata and controls

201 lines (165 loc) · 6.25 KB

CMake

About

This is about learning how to compile C/C++ files:

  • with independent platform support
  • with external libraries

Demo project: Example 1 - hello

Follow the steps:

  • Create a folder project and name it as "hello".
  • Create 2 files:
    • hello.cpp
#include <iostream>

int main() {
	std::cout << "Hello Abhijit!" << std::endl;
	return 0;
}
- `CMakeLists.txt`
cmake_minimum_required(VERSION 2.8.9)
project (hello)

set(CMAKE_CXX_STANDARD  17)	// C++17
add_executable(hello hello.cpp)

M-1

  • open bash-terminal at this location.

  • (OPTIONAL) Ensure that cmake is installed. If not Check by the followings:

    • (to install) $ sudo apt-get install cmake
    • (to check) $ cmake --version
  • type cmake . - to build the project and to generate Makefile.
    CMake identified the environment settings for the linux device and created the Makefile for this project.

    Output:

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.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: /mnt/f/Coding/C++/cpp-playground/hello
  • type $ make to generate the output file i.e. hello.

    Output:

Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/helloworld.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
  • ./hello - prints the output

    Output:

Hello Abhijit!

CLion

  • Create the folder as required -

Example 2

A more structured implementation of CMake with

  • all the build files inside ../student/build/
  • all the header files inside ../student/include/
  • all the source files inside ../student/src/

Follow the steps:

  • In ST3, open a folder named "student"
  • open bash terminal here.
  • $ mkdir build include src - create 3 folders: build, include, src.
  • $ touch CMakeLists.txt include/student.h src/student.cpp src/mainapp.cpp - create the files.
  • [OPTIONAL] view the tree - $ tree -L 2
Output:

.
├── CMakeLists.txt
├── build
├── include
│   └── student.h
└── src
		├── mainapp.cpp
		└── student.cpp

3 directories, 4 files

  • edit the header files - student.h
  • edit the source files - student.cpp, mainapp.cpp
  • edit the CMakeLists.txt as:
Output:

cmake_minimum_required(VERSION 2.8.9)
project(directory_test)

#Bring the headers, such as Student.h into the project
include_directories(include)

#Can manually add <table></table>he sources using the set command as follows:
#set(SOURCES src/mainapp.cpp src/Student.cpp)

#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

add_executable(testStudent ${SOURCES})

  • cd build - go to the build directory.
  • cmake .. - build the project
  • make - create the target file student
  • ./student - to get the output

Example 3

  • Just like Example 2, with additional library file like "libhl++.a" & "include" folder.
  • Follow this example
  • "CMakeLists.txt"
cmake_minimum_required(VERSION 3.5)

set(cmake_cxx_standard 17)
set(cmake_cxx_standard_required on)

project(sha384)

include_directories(/usr/local/include)

add_executable(sha384 main.cpp)
target_link_libraries(sha384 /usr/local/lib/libhl++.a)

Why CMake?

  • Usually, I choose to compile .cpp files GNU GCC in bash (ubuntu-wsl) terminal.
  • Using many libraries like "Boost C++", you need to link some files with *.a file types.
  • Freedom to link libraries with a *.cpp file.

Concepts

Make

  • CMake helps in generating a Makefile file, which when called like $ make creates the output file like hello (say for hello.cpp).
  • When run this way, GNU make looks for a file named GNUmakefile, makefile, or Makefile — in that order. If make finds one of these makefiles, it builds the first target specified in that makefile. However, if make doesn’t find an appropriate makefile, it displays the following error message and exits:
make: *** No targets specified and no makefile found. Stop.
  • Permissible names: GNUmakefile, makefile, or Makefile
  • If your makefile has a different name, then call like this:
make -f <filename>
  • makefile should have tabs instead of spaces.

Commands

  • add_test

    • specifies WORKING_DIRECTORY option for long form of the command. Value of this option is used as a directory in which test operates:
    • syntax: add_test(NAME test_exe COMMAND test_exe WORKING_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR})
    • If you just want the test to find the executable, it is sufficient to use: add_test(NAME test_exe COMMAND test_exe)
    • ^ In this form, CMake checks whether COMMAND is a target name, and, if it is so, replaces it with an absolute path to the executable corresponded to that target. Such way the test can be run from any directory.

Coding Examples

Repositories:

References