mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
add compile flags example
This commit is contained in:
@@ -10,7 +10,7 @@ toc::[]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
CMake has a number of build in build configurations which can be used to compile
|
||||
CMake has a number of built 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.
|
||||
|
||||
@@ -21,9 +21,6 @@ The levels provided are:
|
||||
* 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:
|
||||
|
||||
```
|
||||
@@ -78,9 +75,6 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
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
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
10
01-basic/G-compile-flags/CMakeLists.txt
Normal file
10
01-basic/G-compile-flags/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
# Set a default C++ compile flag
|
||||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)
|
||||
|
||||
# Set the project name
|
||||
project (compile_flags)
|
||||
|
||||
# Add an executable
|
||||
add_executable(cmake_examples_compile_flags main.cpp)
|
||||
122
01-basic/G-compile-flags/README.adoc
Normal file
122
01-basic/G-compile-flags/README.adoc
Normal file
@@ -0,0 +1,122 @@
|
||||
= Compile Flags
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
CMake supports setting compile flags using the +CMAKE_C_FLAGS+ and +CMAKE_CXX_FLAGS+
|
||||
variables. Similarly linker flags can be set using the +CMAKE_LINKER_FLAGS+.
|
||||
|
||||
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-cpp-flag]]
|
||||
Set C++ Flag
|
||||
^^^^^^^^^^^^
|
||||
|
||||
Similar to the build type a C++ compiler flag can be set using the following methods.
|
||||
|
||||
- Using a gui tool such as ccmake / cmake-gui
|
||||
|
||||
image::cmake-gui-set-cxx-flag.png[cmake-gui set cxx flag]
|
||||
|
||||
- Passing into cmake
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
cmake .. -DCMAKE_CXX_FLAGS="-DEX3"
|
||||
----
|
||||
|
||||
[[set-default-flag]]
|
||||
Set Default C++ Flags
|
||||
^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The default `CMAKE_CXX_FLAGS` is either empty or contains the appropriate flags
|
||||
for the build type.
|
||||
|
||||
To set additional default compile flags you can add the following to your
|
||||
top level CMakeLists.txt
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)
|
||||
----
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
The values `CACHE STRING "Set C++ Compiler Flags" FORCE` from the above command
|
||||
are used to force this variable to be set in the CMakeCache.txt file.
|
||||
|
||||
For more details, see https://cmake.org/cmake/help/v3.0/command/set.html[here]
|
||||
====
|
||||
|
||||
[[building-the-example]]
|
||||
Building the Example
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ mkdir build
|
||||
|
||||
$ cd build/
|
||||
|
||||
$ cmake ..
|
||||
-- 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/G-compile-flags/build
|
||||
|
||||
$ make VERBOSE=1
|
||||
/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags -B/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build --check-build-system CMakeFiles/Makefile.cmake 0
|
||||
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/progress.marks
|
||||
make -f CMakeFiles/Makefile2 all
|
||||
make[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build'
|
||||
make -f CMakeFiles/cmake_examples_compile_flags.dir/build.make CMakeFiles/cmake_examples_compile_flags.dir/depend
|
||||
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build'
|
||||
cd /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/DependInfo.cmake --color=
|
||||
Dependee "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/DependInfo.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/depend.internal".
|
||||
Dependee "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/depend.internal".
|
||||
Scanning dependencies of target cmake_examples_compile_flags
|
||||
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build'
|
||||
make -f CMakeFiles/cmake_examples_compile_flags.dir/build.make CMakeFiles/cmake_examples_compile_flags.dir/build
|
||||
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build'
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles 1
|
||||
[100%] Building CXX object CMakeFiles/cmake_examples_compile_flags.dir/main.cpp.o
|
||||
/usr/bin/c++ -DEX2 -o CMakeFiles/cmake_examples_compile_flags.dir/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/main.cpp
|
||||
Linking CXX executable cmake_examples_compile_flags
|
||||
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmake_examples_compile_flags.dir/link.txt --verbose=1
|
||||
/usr/bin/c++ -DEX2 CMakeFiles/cmake_examples_compile_flags.dir/main.cpp.o -o cmake_examples_compile_flags -rdynamic
|
||||
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build'
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles 1
|
||||
[100%] Built target cmake_examples_compile_flags
|
||||
make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build'
|
||||
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles 0
|
||||
----
|
||||
17
01-basic/G-compile-flags/main.cpp
Normal file
17
01-basic/G-compile-flags/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::cout << "Hello Compile Flags!" << std::endl;
|
||||
|
||||
// only print if compile flag set
|
||||
#ifdef EX2
|
||||
std::cout << "Hello Compile Flag EX2!" << std::endl;
|
||||
#endif
|
||||
|
||||
#ifdef EX3
|
||||
std::cout << "Hello Compile Flag EX3!" << std::endl;
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
# Basic Examples
|
||||
= Basic Examples
|
||||
|
||||
The basic examples in this directory show how the setup a cmake project and create an executable. The examples included are
|
||||
The basic examples in this directory show how the setup a CMake project,
|
||||
set compile flags, create executables and libraries, and install them.
|
||||
|
||||
create an executable. The examples included are
|
||||
|
||||
- hello-cmake. A hello world example.
|
||||
- hello-headers. A slighly more complicated hello world example, with using Hello class and seperate source and include folders.
|
||||
- shared-library. An example using a shared library.
|
||||
- static-library. An example using a static library.
|
||||
- installing. Shows how to create a 'make install' target to install the binaries and libraries
|
||||
- build-type. An example showing how to set a default build type of your project.
|
||||
- build-type. An example showing how to set a default build type of your project.
|
||||
- compile-flags. Shows how to set compile flags
|
||||
Reference in New Issue
Block a user