Skip to content

Instantly share code, notes, and snippets.

@elowy01
Last active October 24, 2019 15:27
Show Gist options
  • Save elowy01/dd6f73fb79d8d9eb17f189d644592d0a to your computer and use it in GitHub Desktop.
Save elowy01/dd6f73fb79d8d9eb17f189d644592d0a to your computer and use it in GitHub Desktop.
CMake_cheat_sheet
//
In CMake we use # for adding comments
//
On the top level of a CMake file we have:
project(MyProject VERSION 1.0
DESCRIPTION "Very nice project"
LANGUAGES CXX)
//
Making an executable
/
add_executable(one two.cpp three.h)
#one is both the name of the executable file generated, and the name of the CMake target created.
The source file list comes next
//
# Adding a library:
add_library(one STATIC two.cpp three.h)
//
#Adding include folder to a target:
target_include_directories(one PUBLIC include)
//
# If you have the initialization of functions/classes in *.cpp files and the declaration in *.h files
at the same level, we can write the CMake file by doing:
add_executable(test_cmake main.cpp add.cpp add.h)
/
# If we have the *.cpp within a src/ dir then we need to modify the CMake to:
add_executable(test_cmake src/main.cpp src/add.cpp add.h)
And we need to modify the src/main.cpp to:
#include <iostream>
#include "../add.h"
int main()
{
std::cout << "The sum of 3 and 4 is " << add(3, 4) << std::endl;
return 0;
}
/
# If we have the *.cpp within a src/ dir and *.h within include/ we need to modify the CMake to:
add_executable(test_cmake src/main.cpp src/add.cpp include/add.h)
And we need to modify the src/main.cpp to:
#include <iostream>
#include "../include/add.h"
int main()
{
std::cout << "The sum of 3 and 4 is " << add(3, 4) << std::endl;
return 0;
}
And we need to modify the src/add.cpp to:
//
// Created by ernesto lowy gallego on 2019-10-09.
//
#include "../include/add.h"
int add(int x, int y)
{
return x + y;
}
//
PROJECT_SOURCE_DIR : contains the full path to the root of your
project source directory, i.e. to the nearest directory where
CMakeLists.txt contains the PROJECT() command
Example of its usage:
include_directories(${PROJECT_SOURCE_DIR})
//
#Adding dependency in cmake to a library (xtensor in this case)
cmake_minimum_required(VERSION 3.14)
project(example_tensor)
set(CMAKE_CXX_STANDARD 14)
find_package(xtl REQUIRED)
find_package(xtensor REQUIRED)
# if xtensor was built with xsimd support:
# find_package(xsimd REQUIRED)
add_executable(example_tensor main.cpp)
/
# Require a certain package from a library
find_package(Boost REQUIRED COMPONENTS system iostreams)
//
# A combination of find_package and if statements and throwing a message:
find_package(Boost REQUIRED COMPONENTS system iostreams)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(practice_cpp ${SOURCE_FILES})
if(Boost_FOUND)
target_link_libraries(practice_cpp ${Boost_LIBRARIES})
endif()
//
# Creation of a variable that can be used later
set(SOURCE_FILES main.cpp)
add_executable(practice_cpp ${SOURCE_FILES})
//
# Good example on how to write a Cmake file including
a library and header files:
https://dmerej.info/blog/post/chuck-norris-part-1-cmake-ninja/
//
# Building a project with cmake:
cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /path/test_scripts
# Then run:
make
#And enter the name of the target:
./test_scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment