Skip to content

Instantly share code, notes, and snippets.

@ShigekiKarita
Last active August 29, 2015 14:11
Show Gist options
  • Save ShigekiKarita/30da70bbcc18d230f1c3 to your computer and use it in GitHub Desktop.
Save ShigekiKarita/30da70bbcc18d230f1c3 to your computer and use it in GitHub Desktop.
My CMake Project Template
cmake_minimum_required(VERSION 2.8.4)
project(MyCMakeTemplate)
## Configure compiler and libraries : Boost
add_compile_options(-std=c++1y -Wall -Wextra)
include_directories(${PROJECT_SOURCE_DIR}/include)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fdiagnostics-color=auto)
set(Boost_LIBRARY_DIR $ENV{BOOST_GCC_LIB}) # if set manually
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-stdlib=libc++)
set(Boost_LIBRARY_DIR $ENV{BOOST_CLANG_LIB}) # if set manually
endif()
# ref to add libs: https://sites.google.com/site/boostjp/tips/build_link
find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
## Generate unit tests in ./test
enable_testing()
file(GLOB test_sources RELATIVE ${PROJECT_SOURCE_DIR} test/*.cpp)
foreach(path IN LISTS test_sources)
get_filename_component(target ${path} NAME_WE)
add_executable(${target} ${path})
add_test(NAME ${target} COMMAND $<TARGET_FILE:${target}>)
set_property(TEST ${target} APPEND PROPERTY LABELS ${path})
TARGET_LINK_LIBRARIES(${target} ${Boost_LIBRARIES})
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# link libc++
TARGET_LINK_LIBRARIES(${target} c++)
endif()
endforeach()
## Generate executables in ./src
file(GLOB sources RELATIVE ${PROJECT_SOURCE_DIR} src/*.cpp)
foreach(path IN LISTS sources)
get_filename_component(target ${path} NAME_WE)
add_executable(${target} ${path})
TARGET_LINK_LIBRARIES(${target} ${Boost_LIBRARIES})
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# link libc++
TARGET_LINK_LIBRARIES(${target} c++)
endif()
endforeach()
.
├── CMakeLists.txt
├── include
│   ├── catch.hpp /* nice portable test framework */
│   └── mylib
│   ├── foo.hpp
│   └── hoge.hpp
├── run_test.sh
├── src
│   ├── fuga.cpp
│   └── main.cpp
└── test
├── fuga_catch_test.cpp
├── hage_catch_bdd_test.cpp
└── hoge_boost_test.cpp
4 directories, 10 files
#!/use/bin/env sh
if [ ! -e build ]; then
mkdir build
fi
# set favorite compilers
export CC=clang
export CXX=clang++
# -V or -VV options are helpful for testing
cd build && cmake .. && make && ctest $@
#!/usr/bin/env sh
# git clone `this gist`
rm ./dir_tree
mkdir include src test
wget -O ./include/catch.hpp https://raw.githubusercontent.com/philsquared/Catch/master/single_include/catch.hpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment