Monday, April 16, 2018

Quick CMake Tutorial

Tutorial link

Notes:
Uses a single script to generate build scripts.  The first command in the root CMakeLists.txt is the minimum required CMake version, cmake_minimum_required(VERSION x.x.x). This is automatically done on CLion (the website this tutorial is from).  CMakeLists.txt should be in the root directory of all project-related files for CLion.
After the minimum required version, define the name of the project. A project is a collection of files that are somehow related.
You can create text variables by using the set() command.  To refer to the variable later on, enclose it in round braces prepended by a dollar sign. Ex:  set a variable MY_VAR with the value "hello" -> set (MY_VAR "hello")  set(OTHER_VAR "${MY_VAR} world!")
Header search paths let the compiler know where to search for headers.  The compiler starts searching in several predefined locations, based on the OS, and the user can specify further directories using include_directories(). Users can control the order of inclusion of the directories by using BEFORE and AFTER keywords. 
Can enable a particular language standard by using CMAKE_CXX_VARIABLE, which can also set up additional complier-related settings using CMAKE_CXX_FLAGS
Build targets define an executable or library that the CMake scrip helps build. One script can have more than one build target.  To create an executable binary, use add_executable() command.  Can build a library instead.  There are 3 kinds of library one can build.  STATIC a static library that gets embedded into the executable that decides to use it.  SHARED builds a shared library (.so or .dll depending on OS). MODULE builds a plugin library.  Don't need to link against, but it can load dynamically at runtime.
To include libraries, one needs to instruct the compiler to find the desired library and its components using find_package. In the example below, component1 and component2 are the required/mandatory components for a project, and opt_component is optional. Next, one needs to link an executable to the located library using target_link_libraries. 

Stopped at "Using Boost"

CMakeLists.txt (growing as I read through the site):
// specify minimum cMake version requirement
cmake_minimum_required(VERSION x.x.x)
// define name of project
project(MyProject)
// set example
set (MY_VAR "hello")
set (OTHER_VAR "${MY_VAR} world!")
// specifying header search paths
include_directories( ${MY_SOURCE_DIR}/src )
     // MY_SOURCE_DIR is the desired directory
// setting language standard
set(CMAKE_CXX_STANDARD 11) // enable c++11 standard
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -Wall")
     //telling the compiler to show all compiler errors/warnings.  To get latest versions of the compiler, use -std=c++1y, -std=c++1z, etc. 
add_executable (my_executable my_source1.cpp my_source2.cpp)
     // my_executable is the target name, *.cpp are the source files needed to make the executable
add_library (my_library STATIC|SHARED|MODULE my_surce.cpp)
     // to build a library instead
find_package (my_library COMPONENTS REQUIRED component1 component2 OPTIONAL_COMPONENTS opt_component)
target_link_libraries (my_target my_library another_library)
     // target_link_libraries() should be placed after add_executable() command

placeholder

No comments:

Post a Comment