mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 12:14:36 +03:00
98 lines
6.2 KiB
Plaintext
98 lines
6.2 KiB
Plaintext
= Set C++ Standard
|
|
:toc:
|
|
:toc-placement!:
|
|
|
|
toc::[]
|
|
|
|
# Introduction
|
|
|
|
This example shows how to set the C++ standard using the `target_compile_features` function. This is available since CMake v3.1
|
|
|
|
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
|
|
|
|
## Using target_compile_features
|
|
|
|
Calling the link:https://cmake.org/cmake/help/v3.1/command/target_compile_features.html[target_compile_features] function on a target will look at the passed in feature and determine the correct compiler flag to use for your target.
|
|
|
|
[source,cmake]
|
|
----
|
|
target_compile_features(hello_cpp11 PUBLIC cxx_auto_type)
|
|
----
|
|
|
|
As with other `target_*` functions, you can specify the scope of the feature for the selected target. This populates the link:https://cmake.org/cmake/help/v3.1/prop_tgt/INTERFACE_COMPILE_FEATURES.html#prop_tgt:INTERFACE_COMPILE_FEATURES[INTERFACE_COMPILE_FEATURES] property for the target.
|
|
|
|
The list of available features can be found from the link:https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_COMPILE_FEATURES.html#variable:CMAKE_CXX_COMPILE_FEATURES[CMAKE_CXX_COMPILE_FEATURES] variable. You can obtain a list of the available features using the following code:
|
|
|
|
[source,cmake]
|
|
----
|
|
message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")
|
|
----
|
|
|
|
# Building the Examples
|
|
|
|
Below is sample output from building this example.
|
|
|
|
[source,bash]
|
|
----
|
|
$ mkdir build
|
|
$ cd build
|
|
|
|
$ cmake ..
|
|
-- The C compiler identification is GNU 5.4.0
|
|
-- The CXX compiler identification is GNU 5.4.0
|
|
-- 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
|
|
-- Detecting C compile features
|
|
-- Detecting C compile features - 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
|
|
-- Detecting CXX compile features
|
|
-- Detecting CXX compile features - done
|
|
List of compile features: cxx_template_template_parameters;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates
|
|
-- Configuring done
|
|
-- Generating done
|
|
-- Build files have been written to: /data/code/01-basic/L-cpp-standard/iii-compile-features/build
|
|
|
|
|
|
$ make VERBOSE=1
|
|
/usr/bin/cmake -H/data/code/01-basic/L-cpp-standard/iii-compile-features -B/data/code/01-basic/L-cpp-standard/iii-compile-features/build --check-build-system CMakeFiles/Makefile.cmake 0
|
|
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles /data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles/progress.marks
|
|
make -f CMakeFiles/Makefile2 all
|
|
make[1]: Entering directory '/data/code/01-basic/L-cpp-standard/iii-compile-features/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/iii-compile-features/build'
|
|
cd /data/code/01-basic/L-cpp-standard/iii-compile-features/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /data/code/01-basic/L-cpp-standard/iii-compile-features /data/code/01-basic/L-cpp-standard/iii-compile-features /data/code/01-basic/L-cpp-standard/iii-compile-features/build /data/code/01-basic/L-cpp-standard/iii-compile-features/build /data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake --color=
|
|
Dependee "/data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles/hello_cpp11.dir/depend.internal".
|
|
Dependee "/data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles/hello_cpp11.dir/depend.internal".
|
|
Scanning dependencies of target hello_cpp11
|
|
make[2]: Leaving directory '/data/code/01-basic/L-cpp-standard/iii-compile-features/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/iii-compile-features/build'
|
|
[ 50%] Building CXX object CMakeFiles/hello_cpp11.dir/main.cpp.o
|
|
/usr/bin/c++ -std=gnu++11 -o CMakeFiles/hello_cpp11.dir/main.cpp.o -c /data/code/01-basic/L-cpp-standard/iii-compile-features/main.cpp
|
|
[100%] Linking CXX executable hello_cpp11
|
|
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_cpp11.dir/link.txt --verbose=1
|
|
/usr/bin/c++ CMakeFiles/hello_cpp11.dir/main.cpp.o -o hello_cpp11 -rdynamic
|
|
make[2]: Leaving directory '/data/code/01-basic/L-cpp-standard/iii-compile-features/build'
|
|
[100%] Built target hello_cpp11
|
|
make[1]: Leaving directory '/data/code/01-basic/L-cpp-standard/iii-compile-features/build'
|
|
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/iii-compile-features/build/CMakeFiles 0
|
|
|
|
----
|