diff --git a/01-basic/F-build-type/CMakeLists.txt b/01-basic/F-build-type/CMakeLists.txt new file mode 100644 index 0000000..3aa4ef6 --- /dev/null +++ b/01-basic/F-build-type/CMakeLists.txt @@ -0,0 +1,19 @@ +# Set the minimum version of CMake that can be used +# To find the cmake version run +# $ cmake --version +cmake_minimum_required(VERSION 2.6) + +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message("Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" + "MinSizeRel" "RelWithDebInfo") +endif() + +# Set the project name +project (build_type) + +# Add an executable +add_executable(cmake_examples_build_type main.cpp) diff --git a/01-basic/F-build-type/README.adoc b/01-basic/F-build-type/README.adoc new file mode 100644 index 0000000..c44d0f8 --- /dev/null +++ b/01-basic/F-build-type/README.adoc @@ -0,0 +1,176 @@ += Build Type + +:toc: +:toc-placement!: + +toc::[] + + +[[intro]] +Introduction +------------ + +CMake has a number of build in build configurations which can be used to compile +your project. These specify the optimization levels and if debug information is +to be included in the binary. + +The levels provided are: + + * Release - Adds the `-O3 -DNDEBUG` flags to the compiler + * Debug - Adds the `-g` flag + * MinSizeRel - Adds `-Os -DNDEBUG` + * RelWithDebInfo - Adds `-O2 -g -DNDEBUG` flags + + +Shows a hello world example which first creates and links a static library + +The files in this tutorial are below: + +``` +$ tree +. +├── CMakeLists.txt +├── main.cpp +``` + + * CMakeLists.txt - Contains the CMake commands you wish to run + * main.cpp - The source file with main + +[[concepts]] +Concepts +~~~~~~~~ + +[[set-build]] +Set Build Type +^^^^^^^^^^^^^^ + +The build type can be set using the following methods. + + - Using a gui tool such as ccmake / cmake-gui + +image::cmake-gui-build-type.png[cmake-gui build type] + + - Passing into cmake + +[source,cmake] +---- +cmake .. -DCMAKE_BUILD_TYPE=Release +---- + +[[set-default]] +Set Default Build Type +^^^^^^^^^^^^^^^^^^^^^^ + +The default build type provided by CMake is to include no compiler flags for +optimization. For some projects you may want to +set a default build type so that you do not have to remember to set it. + +To do this you can add the following to your top level CMakeLists.txt + +[source,cmake] +---- +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message("Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" + "MinSizeRel" "RelWithDebInfo") +endif() +---- + +When creating an executable that will use your library you must tell the compiler +about the library. This can be done using the +target_link_library()+ function. + +[[building-the-example]] +Building the Example +~~~~~~~~~~~~~~~~~~~~ + +[source,bash] +---- +$ mkdir build + +$ cd build/ + +$ cmake .. +Setting build type to 'RelWithDebInfo' as none was specified. +-- The C compiler identification is GNU 4.8.4 +-- The CXX compiler identification is GNU 4.8.4 +-- Check for working C compiler: /usr/bin/cc +-- Check for working C compiler: /usr/bin/cc -- works +-- Detecting C compiler ABI info +-- Detecting C compiler ABI info - done +-- Check for working CXX compiler: /usr/bin/c++ +-- Check for working CXX compiler: /usr/bin/c++ -- works +-- Detecting CXX compiler ABI info +-- Detecting CXX compiler ABI info - done +-- Configuring done +-- Generating done +-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build + +$ make VERBOSE=1 +/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/F-build-type -B/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build --check-build-system CMakeFiles/Makefile.cmake 0 +/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/progress.marks +make -f CMakeFiles/Makefile2 all +make[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/depend +make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +cd /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/F-build-type /home/matrim/workspace/cmake-examples/01-basic/F-build-type /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake --color= +Dependee "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal". +Dependee "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal". +Scanning dependencies of target cmake_examples_build_type +make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/build +make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles 1 +[100%] Building CXX object CMakeFiles/cmake_examples_build_type.dir/main.cpp.o +/usr/bin/c++ -O2 -g -DNDEBUG -o CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/F-build-type/main.cpp +Linking CXX executable cmake_examples_build_type +/usr/bin/cmake -E cmake_link_script CMakeFiles/cmake_examples_build_type.dir/link.txt --verbose=1 +/usr/bin/c++ -O2 -g -DNDEBUG CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -o cmake_examples_build_type -rdynamic +make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles 1 +[100%] Built target cmake_examples_build_type +make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles 0$ mkdir build +$ cd build/ +/build$ cmake .. +Setting build type to 'RelWithDebInfo' as none was specified. +-- The C compiler identification is GNU 4.8.4 +-- The CXX compiler identification is GNU 4.8.4 +-- Check for working C compiler: /usr/bin/cc +-- Check for working C compiler: /usr/bin/cc -- works +-- Detecting C compiler ABI info +-- Detecting C compiler ABI info - done +-- Check for working CXX compiler: /usr/bin/c++ +-- Check for working CXX compiler: /usr/bin/c++ -- works +-- Detecting CXX compiler ABI info +-- Detecting CXX compiler ABI info - done +-- Configuring done +-- Generating done +-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build +/build$ make VERBOSE=1 +/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/F-build-type -B/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build --check-build-system CMakeFiles/Makefile.cmake 0 +/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/progress.marks +make -f CMakeFiles/Makefile2 all +make[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/depend +make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +cd /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/F-build-type /home/matrim/workspace/cmake-examples/01-basic/F-build-type /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake --color= +Dependee "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal". +Dependee "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal". +Scanning dependencies of target cmake_examples_build_type +make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/build +make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles 1 +[100%] Building CXX object CMakeFiles/cmake_examples_build_type.dir/main.cpp.o +/usr/bin/c++ -O2 -g -DNDEBUG -o CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/F-build-type/main.cpp +Linking CXX executable cmake_examples_build_type +/usr/bin/cmake -E cmake_link_script CMakeFiles/cmake_examples_build_type.dir/link.txt --verbose=1 +/usr/bin/c++ -O2 -g -DNDEBUG CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -o cmake_examples_build_type -rdynamic +make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles 1 +[100%] Built target cmake_examples_build_type +make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/F-build-type/build' +/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/F-build-type/build/CMakeFiles 0 +---- diff --git a/01-basic/F-build-type/README.md b/01-basic/F-build-type/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/01-basic/F-build-type/cmake-gui-build-type.png b/01-basic/F-build-type/cmake-gui-build-type.png new file mode 100644 index 0000000..37a92d0 Binary files /dev/null and b/01-basic/F-build-type/cmake-gui-build-type.png differ diff --git a/01-basic/F-build-type/main.cpp b/01-basic/F-build-type/main.cpp new file mode 100644 index 0000000..eb3dda0 --- /dev/null +++ b/01-basic/F-build-type/main.cpp @@ -0,0 +1,7 @@ +#include + +int main(int argc, char *argv[]) +{ + std::cout << "Hello Build Type!" << std::endl; + return 0; +}