Skip to content

Commit

Permalink
fix bugs 1-6
Browse files Browse the repository at this point in the history
  • Loading branch information
SFUMECJF committed Dec 2, 2021
1 parent b5a28a8 commit a197d7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 201 deletions.
203 changes: 6 additions & 197 deletions 01-basic/1.11 cpp-standard.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ The examples include:
- link:iii-compile-features[compile-features]. 使用CMake v3.1中引入的target_compile_features函数。


= C++ Standard Common Method
== C++ Standard Common Method
:toc:
:toc-placement!:

toc::[]

# Introduction
## 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.此示例显示了设置C ++标准的常用方法。 可以与大多数版本的CMake一起使用。 但是,如果有CMake的最新版本建议使用其他更便捷的方法。

Expand All @@ -35,9 +35,9 @@ A-hello-cmake$ tree
* 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
## Concepts

## Checking Compile flags
### 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.CMake支持传递一个变量给函数CMAKE_CXX_COMPILER_FLAG来编译程序。 然后将结果存储在您传递的变量中。

Expand All @@ -54,7 +54,7 @@ This example will attempt to compile a program with the flag `-std=c++11` and st
The line `include(CheckCXXCompilerFlag)` tells CMake to include this function to make it available for use.
想使用这个函数,必须使用include(CheckCXXCompilerFlag)包含这个函数

## Adding the flag
### 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 .确定编译器是否支持标志后,即可使用标准cmake方法将此标志添加到目标。 在此示例中,我们使用CMAKE_CXX_FLAGS将标志(c++标准)传播给所有目标。

Expand All @@ -71,7 +71,7 @@ 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`上面的示例仅检查编译标志的gcc版本,并支持从C ++ 11到预标准化C + \ + 0x标志的回退。 在实际使用中,您可能需要检查C14,或添加对设置编译方法的不同支持,例如 `-std = gnu11`

# Building the Examples
## Building the Examples

Below is sample output from building this example.

Expand Down Expand Up @@ -125,194 +125,3 @@ make[2]: Leaving directory `/data/code/01-basic/L-cpp-standard/i-common-method/b
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
----


= Set C++ Standard
:toc:
:toc-placement!:

toc::[]

# Introduction

This example shows how to set the C++ standard using the `CMAKE_CXX_STANDARD` variable. 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 CXX_STANDARD property

Setting the link:https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html[CMAKE_CXX_STANDARD] variable causes the `CXX_STANDARD` property on all targets. This causes CMake to set the appropriate flag at compille time.设置CMAKE_CXX_STANDARD变量会在所有targets上起作用。CMake在编译时设置标准的flags。


[NOTE]
====
The `CMAKE_CXX_STANDARD` variable falls back to the closest appropriate standard without a failure. For example, if you request `-std=gnu++11` you may end up with `-std=gnu++0x`.CMAKE_CXX_STANDARD变量不会报错,如果没有设置的标准,会找到离设置标准最新的标准。 例如,如果您请求-std = gnu11`,则可能会以`-std = gnu0x结尾。
This can result in an unexpected failure at compile time.这可能导致在编译时发生意外故障。
====

# 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
-- Configuring done
-- Generating done
-- Build files have been written to: /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build
$ make VERBOSE=1
/usr/bin/cmake -H/data/code/01-basic/L-cpp-standard/ii-cxx-standard -B/data/code/01-basic/L-cpp-standard/ii-cxx-standard/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/data/code/01-basic/L-cpp-standard/ii-cxx-standard/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/ii-cxx-standard/build'
cd /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /data/code/01-basic/L-cpp-standard/ii-cxx-standard /data/code/01-basic/L-cpp-standard/ii-cxx-standard /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build /data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake --color=
Dependee "/data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles/hello_cpp11.dir/DependInfo.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles/hello_cpp11.dir/depend.internal".
Dependee "/data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/data/code/01-basic/L-cpp-standard/ii-cxx-standard/build/CMakeFiles/hello_cpp11.dir/depend.internal".
Scanning dependencies of target hello_cpp11
make[2]: Leaving directory '/data/code/01-basic/L-cpp-standard/ii-cxx-standard/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/ii-cxx-standard/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/ii-cxx-standard/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/ii-cxx-standard/build'
[100%] Built target hello_cpp11
make[1]: Leaving directory '/data/code/01-basic/L-cpp-standard/ii-cxx-standard/build'
----


= 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.在目标上调用target_compile_features函数将查看传入的功能,并确定要用于目标的正确编译器标志。

[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.与其他target_ *函数一样,您可以为所选目标指定功能范围。 这将填充目标的INTERFACE_COMPILE_FEATURES属性。

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:可用功能列表可从CMAKE_CXX_COMPILE_FEATURES变量中找到。 您可以使用以下代码获取可用功能的列表:

[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
----
9 changes: 5 additions & 4 deletions 01-basic/1.6 Compile Flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ target_compile_definitions(cmake_examples_compile_flags

如果这个目标是一个库(cmake_examples_compile_flags),编译器在编译目标时添加定义-DEX3 ,并且选择了范围PUBLIC或INTERFACE,该定义-DEX3也将包含在链接此目标(cmake_examples_compile_flags)的所有可执行文件中。 注意,本语句使用了PRIVATE,所以编译选项不会传递。

对于编译器选项,还可以使用target_compile_options()函数。
对于编译器选项,还可以使用target_compile_options()函数。 (此处谢谢github用户**[HamsterCoderSim](https://github.com/HamsterCoderSim)**的更正)

```cmake
target_compile_definitions(<target>
2 <INTERFACE|PUBLIC|PRIVATE> [items1...]
3 [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_compile_options(<target> [BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
```

是给 `target` 添加编译选项, `target` 指的是由 `add_executable() `产生的可执行文件或 `add_library() `添加进来的库。` <INTERFACE|PUBLIC|PRIVATE> `指的是` [items...]` 选项可以传播的范围, `PUBLIC and INTERFACE `会传播 `<target> `[INTERFACE_COMPILE_DEFINITIONS](https://cmake.org/cmake/help/v3.0/prop_tgt/INTERFACE_COMPILE_DEFINITIONS.html#prop_tgt:INTERFACE_COMPILE_DEFINITIONS) 属性, `PRIVATE and PUBLIC` 会传播 `target`[COMPILE_DEFINITIONS ](https://cmake.org/cmake/help/v3.0/prop_tgt/COMPILE_DEFINITIONS.html#prop_tgt:COMPILE_DEFINITIONS) 属性。
Expand Down

0 comments on commit a197d7d

Please sign in to comment.