CMake - allyusd/notebook GitHub Wiki

#CMake

##CMake Tutorial

##out-of-source build:

mkdir build
cd build
cmake ..
make

Ref: How to clean up the project files generated by cmake?

##cmake .. -DMyOption=ON

Ref: CMake: Adding command line options

##specify the version of Visual C++

cmake.exe -G "Visual Studio 11 2012" ..

Ref: CMake: how to specify the version of Visual C++ to work with?

##cmake add include path

include_directories

##Linking different libraries for Debug and Release builds in Cmake on windows?

##cmake visual studio subsystem windows

if(WIN32)
   set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
   set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")
   set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE")
   set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE")
   set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:windows")
   set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:windows")
endif(WIN32)

add_executable(MyProject WIN32 main.cpp)

Ref: CMake: How to use different ADD_EXECUTABLE for debug build?

##for the VS folders to mirror the folder structure of your project

set(SrcDir ../src)

set(ALL_FILES
  ${SrcDir}/SomeClass.h ${SrcDir}/SomeClass.cpp
  ${SrcDir}/ToolClass.h ${SrcDir}/ToolClass.cpp
  ...)

add_library(MyLibrary ${ALL_FILES})

# for the VS folders to mirror the folder structure of your project
foreach(FILE ${ALL_FILES}) 
  get_filename_component(PARENT_DIR "${FILE}" PATH)

  # skip src or include
  #string(REGEX REPLACE "(\\./)?(src|include)/?" "" GROUP "${PARENT_DIR}")

  # skip ../
  string(REPLACE "../" "" GROUP "${PARENT_DIR}")

  # changes /'s to \\'s
  string(REPLACE "/" "\\" GROUP "${GROUP}")

  source_group("${GROUP}" FILES "${FILE}")
endforeach()

Ref:2013-06-18-cmake-automatic-source-groups.md

##Use GLOB_RECURSE automatically add all files in a folder to a target

FILE(GLOB_RECURSE ALL_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
  src/*.h src/*.cpp)

Note:

Office document say:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

But I always clear and build, so i not have the problem.

Ref:

CMake - Automatically add all files in a folder to a target?

how to find all *.c files for Cmake build system

How to use cmake GLOB_RECURSE for only some subdirectories

Set startup project of a Visual Studio solution

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})

Ref:

How do I change the startup project of a Visual Studio solution via CMake?

##Temp MSBuild MyProject.sln /p:Configuration=Release