USE CMAKE TO SIMPLIFY THE BUILD

  • Does not build the code, generates files to feed into a build system
  • Very powerful, still build receipt is readable
  • The library creation and linking can be rewritten as follows:
add_library(tools tools.cpp)
add_executable(main main.cpp)
target_link_libraries(main tools)
  • CMakeLists.txt defines the whole build
  • CMake reads CMakeLists.txt sequentially

CMAKELISTS.TXT

project( first_project ) # Mandatory.
cmake_minimum_required(VERSION 3.1) # Mandatory.
set( CMAKE_CXX_STANDARD 11) # Use c++11.
# tell cmake to output binaries here:
set( EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set( LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# tell cmake where to look for *.h files
include_directories(${PROJECT_SOURCE_DIR}/src)
# create library "libtools"
add_library(tools src/tools.cpp)
# add executable main
add_executable(main src/ tools_main .cpp)
# tell the linker to bind these objects together
target_link_libraries(main tools)

SET COMPILATION OPTIONS IN CMAKE

set ( CMAKE_CXX_STANDARD 14)
# Set build type if not set.
if(NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Release)
endif ()
# Set additional flags.
set( CMAKE_CXX_FLAGS "-Wall -Wextra ")
set( CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set( CMAKE_CXX_FLAGS_RELEASE "-O3")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.