cmake_minimum_required(VERSION 2.8) project(LearnCMake) add_executable(HelloWorld main.cpp)
$ mkdir bin $ cd bin $ cmake .. $ make $ ./HelloWorld
cmake_minimum_required(VERSION 2.8) project(LearnCMake) add_library(useless useless.cpp)
cmake_minimum_required(VERSION 2.8) project(LearnCMake) add_library(useless useless/useless.cpp) add_executable(HelloWorld hello/main.cpp) target_link_libraries(HelloWorld useless)
cmake_minimum_required(VERSION 2.8) project(LearnCMake) include_directories("${PROJECT_SOURCE_DIR}/include") set(CMAKE_CXX_FLAGS -Wall) add_library(useless useless/useless.cpp) add_executable(HelloWorld hello/main.cpp) target_link_libraries(HelloWorld useless)
option(WARN_ALL "Throw warnings when you write dumb code" ON) if (WARN_ALL) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") endif ()
# Enable super extra warnings in debug mode set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wextra")
cmake_minimum_required(VERSION 2.8) project(LearnCMake) add_library(useless useless/useless.cpp) target_include_directories(useless PUBLIC "${PROJECT_SOURCE_DIR}/include") target_include_directories(useless PRIVATE "${PROJECT_SOURCE_DIR}/useless") add_executable(HelloWorld hello/main.cpp) target_link_libraries(HelloWorld useless)
add_library(useful useful/useful.cpp) add_library(useless useless/useless.cpp) target_link_libraries(useless useful) add_executable(HelloWorld hello/main.cpp) target_link_libraries(HelloWorld useless)
$ mkdir bin $ cd bin $ cmake --graphviz=graphs/learn .. $ ls graphs $ cat graphs/learn.HelloWorld $ dot -T png graphs/learn.HelloWorld -o HelloWorld.png
set(FOO "bar") set(UHOH "FOO") set(WTF "${${UHOH}}") message("${FOO}") message("${UHOH}") message("${WTF}")
set(MEMES "CAT;TREX") message("All memes: ${MEMES}") foreach(meme ${MEMES}) message("Next meme: ${meme}") endforeach()
set(POEMS ONE FISH TWO) message("All poems: ${POEMS}") foreach(poem IN LISTS POEMS) message("Next poem? ${poem}") endforeach()
set(THIS_IS_TRUTHY TRUE) # ≈ ON, YES, Y, non-zero numbers set(THIS_IS_FALSY FALSE) # ≈ OFF, NO, N, IGNORE, NOTFOUND, *-NOTFOUND, "" if (${THIS_IS_TRUTHY}) message("Test 1 passed") endif () if (THIS_IS_TRUTHY) message("Test 2 passed") endif () if (NOT ${THIS_IS_FALSY}) message("Test 3 passed") endif () if (NOT THIS_IS_FALSY) message("Test 4 passed") endif ()
if (VAR1 OR VAR2) if (TARGET another_exe) if (EXISTS "${SOME_FILE}") if ("${VAR1}" EQUAL "${VAR2}") if ("${ACTUAL_VERSION}" VERSION_GREATER "${REQUESTED_VERSION}")
add_executable(HelloWorld main.cpp) add_custom_target(create_archive COMMAND ${CMAKE_COMMAND} -E tar "cfz" "app.tar.gz" $<TARGET_FILE:HelloWorld> DEPENDS HelloWorld)
macro(set_dummy name value) set("${name}" "${value}") endmacro() set_dummy(FOO BAR) message("FOO=${FOO}") # Output = "FOO=BAR" set_dummy(BAZ;FOOBAR) message("BAZ=${BAZ}") # Output = "BAZ=FOOBAR"
function(set_dummy name value) set("${name}" "${value}") message("${name}=${${name}}") endfunction() set_dummy(FOO BAR) # Output = "FOO=BAR" message("FOO=${FOO}") # Output = "FOO="
function(set_dummy name value) set("${name}" "${value}" PARENT_SCOPE) message("${name}=${${name}}") endfunction() set_dummy(FOO BAR) # Output = "FOO=" message("FOO=${FOO}") # Output = "FOO=BAR"
function (do_foo) cmake_parse_arguments(DO_FOO "FLAG1;FLAG2" "OPT1;OPT2" "M_OPT1" ${ARGN}) if (DO_FOO_FLAG1) # Do stuff if `FLAG1` was passed into the `do_foo(...)` invocation endif () # ... More code... endfunction ()
find_library(USELESS_LIB useless "it/might/be/installed/here/lib64") add_executable(Hello hello.cpp) target_link_libraries(Hello "${USELESS_LIB}")
find_path(USELESS_ROOT "include/useless/useless.h" "it/might/be/installed/here") find_path(USELESS_INCLUDE_DIR useless/useless.h "${USELESS_ROOT}/include") find_library(USELESS_LIB useless "${USELESS_ROOT}/lib64") add_executable(Hello hello.cpp) target_include_directories(Hello PUBLIC "${USELESS_INCLUDE_DIR}") target_link_libraries(Hello "${USELESS_LIB}")
find_package(ZLIB REQUIRED) add_executable(myProgram file1.cpp file2.cpp) target_include_directories(myProgram PUBLIC "${ZLIB_INCLUDE_DIRS}") target_link_libraries(myProgram "${ZLIB_LIBRARIES}")
find_package(Qt4 REQUIRED) # No target_include_directories(...) needed! add_executable(myProgram file1.cpp file2.cpp) target_link_libraries(myProgram Qt4::QtGui)
DESTINATION
PERMISSIONS
CONFIGURATIONS
CMAKE_BUILD_TYPE
COMPONENT
OPTIONAL
# Install the "HelloWorld" target to the `bin` directory install(TARGETS HelloWorld DESTINATION bin) # Install (almost) the entire contents of a directory... install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/" DESTINATION include FILES_MATCHING PATTERN "*.h") # Install some specific files. DON'T COPY THIS EXAMPLE! See install(PROGRAMS ...) install(FILES "${PROJECT_SOURCE_DIR}/scripts/doFoo.sh" "${PROJECT_SOURCE_DIR}/scripts/doBar.py" DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE)