Tuesday, September 11, 2018

CMake

Reference

CMake Tutorial

CMake is designed to be used in conjunction with the native build environment. configuration files placed in each source directory (called CMakeLists.txt files) are used to generate standard build files Makefile

CMakeLists.txt places

  • Source folder
  • Root of the source tree of any application, library
  • For each module that can be compile and build separately

Hello cmake

  • source
#include <iostream> int main(void) { std::cout << "Hello World" << std::endl; return(0); }
  • CMakeLists.txt (with the source folder)
# Set the minimum required version of cmake for a project cmake_minimum_required(VERSION 2.8) # Project name project(hello) # Add an executable to the project using the specified source files add_executable(hello hello.cpp)
  • Run
cmake . make

Build and bin sub folders

To keep files in place , and hold all cmake file in one place

mkdir build cd build cmake .. make

Project structure

p1 ├── bin ├── build ├── src | └── CMakeLists.txt └─ CMakeLists.txt
  • Source CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(hello) # Assign value to variable (make output folder) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) # Adds sub folder to build add_subdirectory(src)
  • Root src CMakeLists.txt
# Print message message ("main application") add_executable(hello test.cpp)
  • from build sub folder
cmake .. make

CMake variables

cmake-variables

  • Set variable data with set function/macro
  • Access value with ${variable_name}
set(CMAKE_CXX_STANDARD 11) message("CXX Standard:" ${CMAKE_CXX_STANDARD})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  • Custom variables
set(TRIAL_VARIABLE "VALUE") message("${TRIAL_VARIABLE}")

Building a Library with cmake

  • Add lib/math sub folder under src
  • Add header and source file
  • Change main app to use the new functionality

code

  • header file (src/lib/math/calc.hpp)
#ifndef CALC_HPP #define CALC_HPP namespace math { class calc{ public: int sum(const int &a, const int &b); }; } #endif
  • implementation (src/lib/math/calc.cpp)
#include "calc.hpp" int math::calc::sum(const int &a, const int &b){ return a + b; }
  • main
#include <iostream> #include "lib/math/calc.hpp" using namespace std; int main(void) { math::calc calc; int sum = calc.sum(3, 4); cout << "3+4=" << sum << endl; return(0); }
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(hello) # set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) # CMAKE_ARCHIVE_OUTPUT_DIRECTORY set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") add_subdirectory(src)
  • CMakeLists.txt (src)
# create shared library add_library(math SHARED lib/math/calc.cpp) add_executable(hello test.cpp) # link libmath.so to executable target_link_libraries(hello math)
  • It's create shared library libxxx.so at lib sub folder
  • It's create binary use's the lib in bin sub folder

No comments: