Skip to content

Instantly share code, notes, and snippets.

@bbkane
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbkane/08a685ed5cc9256f77fe to your computer and use it in GitHub Desktop.
Save bbkane/08a685ed5cc9256f77fe to your computer and use it in GitHub Desktop.
Simple (no libraries or assets) Sandbox projects for CMake. For those one-off times you just want to try something.
# Author: Ben Kane
cmake_minimum_required(VERSION 3.1)
# Name the Project that holds all other projects
project(Sandbox)
# add warnings to all projects
if(MSVC)
add_compile_options(/W4 /wd4100)
else(MSVC)
add_compile_options(-std=c++1y -Wall -Wextra -pedantic -Wno-unused-parameter)
endif(MSVC)
# set output for all projects
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/bin)
# Usage: create_unmade <file1>...
# creates files if they don't exist. Starts looking in the directory of the CMakeLists.txt file it's called in
function(create_unmade)
foreach(word ${ARGV}) # ${ARGV} expands to all arguments
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${word})
message("---- creating: ${word} ----")
file(WRITE ${word} "")
endif()
endforeach()
endfunction()
# Usage: add_proj <name> <file1>...
# creates a simple project from files. Files are placed in a folder called name
function(add_proj name)
foreach(word ${ARGN}) # ${ARGN} expands to all unexpected arguments
set(word ${name}/${word}) # prefix every filename with the project name
set(Files_To_Make ${Files_To_Make} ${word})
endforeach()
create_unmade(${Files_To_Make})
source_group(${name}_Files FILES ${Files_To_Make})
add_executable(${name} ${Files_To_Make})
endfunction()
# For easy use with project.vim, name the main file the same as the project name
add_proj(Primes Primes.cpp Header.hpp)
add_proj(Other Other.cpp)
" Load this with command LoadProjectVimrc
set makeprg=cd\ ../build\ &&\ make
" assumes the current file is the name of the executable
map <F5> :w<CR> :make<CR> :!../bin/%:r<CR>
command! EditCMakeFile vsplit ../CMakeLists.txt
command! CMake !cd ../build && cmake ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment