mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
add build type example
This commit is contained in:
19
01-basic/F-build-type/CMakeLists.txt
Normal file
19
01-basic/F-build-type/CMakeLists.txt
Normal file
@@ -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)
|
||||||
176
01-basic/F-build-type/README.adoc
Normal file
176
01-basic/F-build-type/README.adoc
Normal file
@@ -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
|
||||||
|
----
|
||||||
BIN
01-basic/F-build-type/cmake-gui-build-type.png
Normal file
BIN
01-basic/F-build-type/cmake-gui-build-type.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 89 KiB |
7
01-basic/F-build-type/main.cpp
Normal file
7
01-basic/F-build-type/main.cpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
std::cout << "Hello Build Type!" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user