112 lines
5.6 KiB
Plaintext
112 lines
5.6 KiB
Plaintext
= C++ Standard Common Method
|
|
:toc:
|
|
:toc-placement!:
|
|
|
|
toc::[]
|
|
|
|
# Introduction
|
|
|
|
This example shows a common method to set the C++ Standard. This can be used with most versions of CMake. However, if you are targeting a recent version of CMake there are more convenient methods available.
|
|
|
|
The files in this tutorial are below:
|
|
|
|
```
|
|
A-hello-cmake$ tree
|
|
.
|
|
├── CMakeLists.txt
|
|
├── main.cpp
|
|
```
|
|
|
|
* link:CMakeLists.txt[CMakeLists.txt] - Contains the CMake commands you wish to run
|
|
* link:main.cpp[main.cpp] - A simple "Hello World" cpp file targeting C++11.
|
|
|
|
# Concepts
|
|
|
|
## Checking Compile flags
|
|
|
|
CMake has support for attempting to compile a program with any flags you pass into the function `CMAKE_CXX_COMPILER_FLAG`. The result is then stored in a variable that you pass in.
|
|
|
|
For example:
|
|
|
|
[source,cmake]
|
|
----
|
|
include(CheckCXXCompilerFlag)
|
|
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
|
|
----
|
|
|
|
This example will attempt to compile a program with the flag `-std=c++11` and store the result in the variable `COMPILER_SUPPORTS_CXX11`.
|
|
|
|
The line `include(CheckCXXCompilerFlag)` tells CMake to include this function to make it available for use.
|
|
|
|
## Adding the flag
|
|
|
|
Once you have determined if the compile supports a flag, you can then use the link:../../G-compile-flags/[standard cmake methods] to add this flag to a target. In this example we use the `CMAKE_CXX_FLAGS` to propegate the flag to all targets .
|
|
|
|
[source,cmake]
|
|
----
|
|
if(COMPILER_SUPPORTS_CXX11)#
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
|
elseif(COMPILER_SUPPORTS_CXX0X)#
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
|
|
else()
|
|
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
|
|
endif()
|
|
----
|
|
|
|
The above example only checks for the gcc version of the compile flags and supports fallback from C+\+11 to the pre-standardisation C+\+0x flag. In real usage you may want to check for C++14, or add support for different methods of setting the compile, e.g. `-std=gnu++11`
|
|
|
|
# Building the Examples
|
|
|
|
Below is sample output from building this 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
|
|
-- Performing Test COMPILER_SUPPORTS_CXX11
|
|
-- Performing Test COMPILER_SUPPORTS_CXX11 - Success
|
|
-- Performing Test COMPILER_SUPPORTS_CXX0X
|
|
-- Performing Test COMPILER_SUPPORTS_CXX0X - Success
|
|
-- Configuring done
|
|
-- Generating done
|
|
-- Build files have been written to: /data/code/01-basic/L-cpp-standard/i-common-method/build
|
|
|
|
$ make VERBOSE=1
|
|
/usr/bin/cmake -H/data/code/01-basic/L-cpp-standard/i-common-method -B/data/code/01-basic/L-cpp-standard/i-common-method/build --check-build-system CMakeFiles/Makefile.cmake 0
|
|
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/progress.marks
|
|
make -f CMakeFiles/Makefile2 all
|
|
make[1]: Entering directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
|
|
make -f CMakeFiles/hello_cpp11.dir/build.make CMakeFiles/hello_cpp11.dir/depend
|
|
make[2]: Entering directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
|
|
cd /data/code/01-basic/L-cpp-standard/i-common-method/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /data/code/01-basic/L-cpp-standard/i-common-method /data/code/01-basic/L-cpp-standard/i-common-method /data/code/01-basic/L-cpp-standard/i-common-method/build /data/code/01-basic/L-cpp-standard/i-common-method/build /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake --color=
|
|
Dependee "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/depend.internal".
|
|
Dependee "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles/hello_cpp11.dir/depend.internal".
|
|
Scanning dependencies of target hello_cpp11
|
|
make[2]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
|
|
make -f CMakeFiles/hello_cpp11.dir/build.make CMakeFiles/hello_cpp11.dir/build
|
|
make[2]: Entering directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
|
|
/usr/bin/cmake -E cmake_progress_report /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles 1
|
|
[100%] Building CXX object CMakeFiles/hello_cpp11.dir/main.cpp.o
|
|
/usr/bin/c++ -std=c++11 -o CMakeFiles/hello_cpp11.dir/main.cpp.o -c /data/code/01-basic/L-cpp-standard/i-common-method/main.cpp
|
|
Linking CXX executable hello_cpp11
|
|
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_cpp11.dir/link.txt --verbose=1
|
|
/usr/bin/c++ -std=c++11 CMakeFiles/hello_cpp11.dir/main.cpp.o -o hello_cpp11 -rdynamic
|
|
make[2]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
|
|
/usr/bin/cmake -E cmake_progress_report /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles 1
|
|
[100%] Built target hello_cpp11
|
|
make[1]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/build'
|
|
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/i-common-method/build/CMakeFiles 0
|
|
----
|