mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 12:14:36 +03:00
123 lines
5.4 KiB
Plaintext
123 lines
5.4 KiB
Plaintext
= 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
|
|
----
|