@chengzhao-dev/dsh-cpp-workflow 0.7.5 → 0.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +6 -10
- package/README.md +6 -10
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +2 -2
- package/skills/building-cpp/assets/config/.clang-format +27 -3
- package/skills/building-cpp/assets/config/.clang-tidy +0 -4
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +49 -54
- package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +5 -5
- package/skills/building-cpp/assets/templates/projects/multi/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +8 -9
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +8 -8
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +3 -3
- package/skills/building-cpp/assets/templates/projects/single/src/main.cpp +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +8 -8
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cpp +2 -2
- package/skills/building-cpp/references/01-build-script.md +1 -1
- package/skills/building-cpp/references/02-environment.md +4 -4
- package/skills/building-cpp/steps/01-build.md +1 -1
- package/skills/building-cpp/steps/02-tooling.md +3 -3
- package/skills/building-cpp/steps/03-check.md +2 -0
- package/skills/building-cpp/steps/04-vscode.md +2 -2
- package/skills/developing-cpp/SKILL.md +9 -5
- package/skills/developing-cpp/references/comments/01-comment-rules.md +15 -1
- package/skills/developing-cpp/references/comments/02-style.md +7 -6
- package/skills/developing-cpp/references/comments/examples/11-declaration-constant.md +26 -0
- package/skills/developing-cpp/references/design/02-raii.md +42 -0
- package/skills/developing-cpp/references/design/03-third-party.md +35 -0
- package/skills/developing-cpp/references/design/examples/09-option-menu.md +36 -0
- package/skills/developing-cpp/references/design/session-layer.md +16 -13
- package/skills/developing-cpp/references/index.md +10 -6
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +3 -3
- package/skills/developing-cpp/references/language/value-types/04-enum.md +3 -3
- package/skills/developing-cpp/references/layout/01-source-layout.md +1 -1
- package/skills/developing-cpp/references/layout/02-file-template.md +24 -38
- package/skills/developing-cpp/references/layout/03-declaration-order.md +11 -6
- package/skills/developing-cpp/references/naming/01-identifier-names.md +7 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +6 -6
- package/skills/developing-cpp/scripts/checking-comments.mjs +77 -6
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +214 -13
- package/skills/developing-cpp/steps/03-declaration.md +4 -4
- package/skills/developing-cpp/steps/04-implementation.md +2 -2
- package/skills/engineering/SKILL.md +8 -8
- package/skills/engineering/references/cmake/assets/CompileSettings.cmake +152 -123
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +62 -43
- package/skills/engineering/references/cmake/assets/OutputSettings.cmake +38 -0
- package/skills/engineering/references/cmake/linking/01-libraries.md +1 -1
- package/skills/engineering/references/cmake/modules/01-functions.md +26 -16
- package/skills/engineering/references/cmake/standards/01-cxx-standard.md +2 -2
- package/skills/engineering/references/cmake/standards/c++11/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++14/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++17/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++20/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++23/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/examples/01-standard.md +1 -1
- package/skills/engineering/references/cmake/standards/index.md +5 -5
- package/skills/engineering/references/cmake/standards/versions.md +1 -1
- package/skills/engineering/references/cmake/targets/01-target-rules.md +50 -36
- package/skills/engineering/references/cmake/targets/examples/01-rpath.md +1 -1
- package/skills/engineering/references/formatting/01-encoding.md +4 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +2 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -2
- package/skills/engineering/references/formatting/examples/01-clang-format.md +13 -4
- package/skills/engineering/references/index.md +5 -5
- package/skills/engineering/steps/01-targets.md +7 -7
- package/skills/engineering/steps/02-modules.md +8 -5
- package/skills/engineering/steps/03-check.md +2 -1
- package/templates/cpp-workflow-project/SKILL.md +3 -2
|
@@ -1,128 +1,157 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
#[=======================================================================[.rst:
|
|
2
|
+
CompileSettings
|
|
3
|
+
---------------
|
|
4
|
+
|
|
5
|
+
在一个目标上设置 C++ 语言标准与警告等级。
|
|
6
|
+
|
|
7
|
+
.. code-block:: cmake
|
|
8
|
+
|
|
9
|
+
include(CompileSettings)
|
|
10
|
+
|
|
11
|
+
Commands
|
|
12
|
+
^^^^^^^^
|
|
13
|
+
|
|
14
|
+
.. command:: set_target_cxx_standard
|
|
15
|
+
|
|
16
|
+
.. code-block:: cmake
|
|
17
|
+
|
|
18
|
+
set_target_cxx_standard(<target> <visibility> <standard>)
|
|
19
|
+
|
|
20
|
+
让目标使用指定的 C++ 语言标准。
|
|
21
|
+
|
|
22
|
+
``<visibility>`` 取 ``PUBLIC``、``PRIVATE`` 或 ``INTERFACE``。
|
|
23
|
+
``INTERFACE`` 通过 compile feature 传播最低标准,纯头文件的 target
|
|
24
|
+
仍需使用方自己调用一次。
|
|
25
|
+
|
|
26
|
+
``<standard>`` 取 ``c++11``、``c++14``、``c++17``、``c++20`` 或
|
|
27
|
+
``c++23``。
|
|
28
|
+
|
|
29
|
+
函数先把 ``c++NN`` 的后两位转成 ``NN``,再设置两处:compile
|
|
30
|
+
feature ``cxx_std_NN`` 负责传播「至少该标准」,``CXX_STANDARD``
|
|
31
|
+
属性固定精确标准,``CXX_STANDARD_REQUIRED`` 防止编译器静默降级,
|
|
32
|
+
``CXX_EXTENSIONS`` 关闭编译器扩展。
|
|
33
|
+
|
|
34
|
+
.. command:: set_target_warnings
|
|
35
|
+
|
|
36
|
+
.. code-block:: cmake
|
|
37
|
+
|
|
38
|
+
set_target_warnings(<target> <visibility> [LEVEL <level>])
|
|
39
|
+
|
|
40
|
+
给目标加一组编译警告。
|
|
41
|
+
|
|
42
|
+
只改传入的目标,``PRIVATE`` 不向使用方传播。
|
|
43
|
+
|
|
44
|
+
``<level>`` 取 ``basic``、``standard`` 或 ``strict``,省略时按
|
|
45
|
+
``standard``。
|
|
46
|
+
|
|
47
|
+
档位依次收紧:``strict`` 包含 ``standard``,``standard`` 包含
|
|
48
|
+
``basic``;MSVC 与 Clang/GNU 使用各自等价的开关。
|
|
49
|
+
#]=======================================================================]
|
|
50
|
+
|
|
7
51
|
include_guard(GLOBAL)
|
|
8
52
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
# :param target: 已经存在的 CMake 目标。
|
|
13
|
-
# :param visibility: 取 PUBLIC、PRIVATE 或 INTERFACE。
|
|
14
|
-
# INTERFACE 只传播 compile feature(最低标准),不能锁死 CXX_STANDARD
|
|
15
|
-
# 与 CXX_EXTENSIONS;纯头文件 target 的消费者仍需自己调用一次。
|
|
16
|
-
# :param standard: 取 c++11、c++14、c++17、c++20 或 c++23。
|
|
17
|
-
function(CompileSettings_SetCxxStandard target visibility standard)
|
|
18
|
-
if(NOT TARGET "${target}")
|
|
19
|
-
message(FATAL_ERROR
|
|
20
|
-
"CompileSettings_SetCxxStandard: target '${target}' does not exist")
|
|
21
|
-
endif()
|
|
22
|
-
|
|
23
|
-
if(NOT visibility MATCHES "^(PUBLIC|PRIVATE|INTERFACE)$")
|
|
24
|
-
message(FATAL_ERROR
|
|
25
|
-
"CompileSettings_SetCxxStandard: visibility must be "
|
|
26
|
-
"PUBLIC, PRIVATE, or INTERFACE")
|
|
27
|
-
endif()
|
|
28
|
-
|
|
29
|
-
set(supported_standards c++11 c++14 c++17 c++20 c++23)
|
|
30
|
-
if(NOT standard IN_LIST supported_standards)
|
|
31
|
-
message(FATAL_ERROR
|
|
32
|
-
"CompileSettings_SetCxxStandard: standard must be one of "
|
|
33
|
-
"${supported_standards}")
|
|
34
|
-
endif()
|
|
35
|
-
|
|
36
|
-
# c++23 -> 23:两种声明使用同一个编号。
|
|
37
|
-
string(SUBSTRING "${standard}" 3 -1 number)
|
|
38
|
-
target_compile_features("${target}" "${visibility}" "cxx_std_${number}")
|
|
39
|
-
set_target_properties("${target}" PROPERTIES
|
|
40
|
-
CXX_STANDARD "${number}"
|
|
41
|
-
CXX_STANDARD_REQUIRED ON
|
|
42
|
-
CXX_EXTENSIONS OFF
|
|
43
|
-
)
|
|
44
|
-
endfunction()
|
|
53
|
+
function(set_target_cxx_standard target visibility standard)
|
|
54
|
+
set(supported_standards c++11 c++14 c++17 c++20 c++23)
|
|
45
55
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"CompileSettings_SetWarnings: visibility must be PRIVATE or INTERFACE")
|
|
72
|
-
endif()
|
|
73
|
-
|
|
74
|
-
if(NOT arg_LEVEL)
|
|
75
|
-
set(arg_LEVEL standard)
|
|
76
|
-
endif()
|
|
77
|
-
|
|
78
|
-
if(MSVC)
|
|
79
|
-
set(basic_warnings /W4)
|
|
80
|
-
set(standard_warnings ${basic_warnings} /permissive- /WX)
|
|
81
|
-
set(strict_warnings
|
|
82
|
-
${standard_warnings}
|
|
83
|
-
/w14242 /w14254 /w14263 /w14265 /w14287 /w14296
|
|
84
|
-
/w14311 /w14545 /w14546 /w14547 /w14549 /w14555
|
|
85
|
-
/w14619 /w14640 /w14826
|
|
86
|
-
)
|
|
87
|
-
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
|
|
88
|
-
set(basic_warnings -Wall -Wextra)
|
|
89
|
-
set(standard_warnings
|
|
90
|
-
${basic_warnings}
|
|
91
|
-
-Werror # 把告警当错误
|
|
92
|
-
-Wshadow # 变量遮蔽
|
|
93
|
-
-Wold-style-cast # C 风格转换
|
|
94
|
-
-Wpedantic # 非标准扩展
|
|
95
|
-
-Wnon-virtual-dtor # 有虚函数但析构非虚
|
|
96
|
-
-Woverloaded-virtual
|
|
97
|
-
)
|
|
98
|
-
set(strict_warnings
|
|
99
|
-
${standard_warnings}
|
|
100
|
-
-Wcast-align
|
|
101
|
-
-Wcast-qual
|
|
102
|
-
-Wconversion
|
|
103
|
-
-Wsign-conversion
|
|
104
|
-
-Wdouble-promotion
|
|
105
|
-
-Wformat=2
|
|
106
|
-
-Wimplicit-fallthrough
|
|
107
|
-
-Wnull-dereference
|
|
108
|
-
-Wundef
|
|
56
|
+
if(NOT TARGET "${target}")
|
|
57
|
+
message(FATAL_ERROR
|
|
58
|
+
"set_target_cxx_standard: target '${target}' does not exist")
|
|
59
|
+
endif()
|
|
60
|
+
|
|
61
|
+
if(NOT visibility MATCHES "^(PUBLIC|PRIVATE|INTERFACE)$")
|
|
62
|
+
message(FATAL_ERROR
|
|
63
|
+
"set_target_cxx_standard: visibility must be "
|
|
64
|
+
"PUBLIC, PRIVATE, or INTERFACE")
|
|
65
|
+
endif()
|
|
66
|
+
|
|
67
|
+
if(NOT standard IN_LIST supported_standards)
|
|
68
|
+
message(FATAL_ERROR
|
|
69
|
+
"set_target_cxx_standard: standard must be one of "
|
|
70
|
+
"${supported_standards}")
|
|
71
|
+
endif()
|
|
72
|
+
|
|
73
|
+
string(SUBSTRING "${standard}" 3 -1 number)
|
|
74
|
+
|
|
75
|
+
target_compile_features("${target}" "${visibility}" "cxx_std_${number}")
|
|
76
|
+
|
|
77
|
+
set_target_properties("${target}" PROPERTIES
|
|
78
|
+
CXX_STANDARD "${number}"
|
|
79
|
+
CXX_STANDARD_REQUIRED ON
|
|
80
|
+
CXX_EXTENSIONS OFF
|
|
109
81
|
)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
82
|
+
endfunction()
|
|
83
|
+
|
|
84
|
+
function(set_target_warnings target visibility)
|
|
85
|
+
cmake_parse_arguments(PARSE_ARGV 2 arg "" "LEVEL" "")
|
|
86
|
+
|
|
87
|
+
if(arg_UNPARSED_ARGUMENTS)
|
|
88
|
+
message(FATAL_ERROR
|
|
89
|
+
"set_target_warnings: unexpected arguments: "
|
|
90
|
+
"${arg_UNPARSED_ARGUMENTS}")
|
|
91
|
+
endif()
|
|
92
|
+
|
|
93
|
+
if(NOT TARGET "${target}")
|
|
94
|
+
message(FATAL_ERROR
|
|
95
|
+
"set_target_warnings: target '${target}' does not exist")
|
|
96
|
+
endif()
|
|
97
|
+
|
|
98
|
+
if(NOT visibility MATCHES "^(PRIVATE|INTERFACE)$")
|
|
99
|
+
message(FATAL_ERROR
|
|
100
|
+
"set_target_warnings: visibility must be PRIVATE or INTERFACE")
|
|
101
|
+
endif()
|
|
102
|
+
|
|
103
|
+
if(NOT arg_LEVEL)
|
|
104
|
+
set(arg_LEVEL standard)
|
|
105
|
+
endif()
|
|
106
|
+
|
|
107
|
+
if(MSVC)
|
|
108
|
+
set(basic_warnings /W4)
|
|
109
|
+
set(standard_warnings ${basic_warnings} /permissive- /WX)
|
|
110
|
+
set(strict_warnings
|
|
111
|
+
${standard_warnings}
|
|
112
|
+
/w14242 /w14254 /w14263 /w14265 /w14287 /w14296
|
|
113
|
+
/w14311 /w14545 /w14546 /w14547 /w14549 /w14555
|
|
114
|
+
/w14619 /w14640 /w14826
|
|
115
|
+
)
|
|
116
|
+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
|
|
117
|
+
set(basic_warnings -Wall -Wextra)
|
|
118
|
+
set(standard_warnings
|
|
119
|
+
${basic_warnings}
|
|
120
|
+
-Werror
|
|
121
|
+
-Wshadow
|
|
122
|
+
-Wold-style-cast
|
|
123
|
+
-Wpedantic
|
|
124
|
+
-Wnon-virtual-dtor
|
|
125
|
+
-Woverloaded-virtual
|
|
126
|
+
)
|
|
127
|
+
set(strict_warnings
|
|
128
|
+
${standard_warnings}
|
|
129
|
+
-Wcast-align
|
|
130
|
+
-Wcast-qual
|
|
131
|
+
-Wconversion
|
|
132
|
+
-Wsign-conversion
|
|
133
|
+
-Wdouble-promotion
|
|
134
|
+
-Wformat=2
|
|
135
|
+
-Wimplicit-fallthrough
|
|
136
|
+
-Wnull-dereference
|
|
137
|
+
-Wundef
|
|
138
|
+
)
|
|
139
|
+
else()
|
|
140
|
+
message(FATAL_ERROR
|
|
141
|
+
"set_target_warnings: unsupported compiler "
|
|
142
|
+
"'${CMAKE_CXX_COMPILER_ID}'")
|
|
143
|
+
endif()
|
|
144
|
+
|
|
145
|
+
if(arg_LEVEL STREQUAL "basic")
|
|
146
|
+
set(warnings ${basic_warnings})
|
|
147
|
+
elseif(arg_LEVEL STREQUAL "standard")
|
|
148
|
+
set(warnings ${standard_warnings})
|
|
149
|
+
elseif(arg_LEVEL STREQUAL "strict")
|
|
150
|
+
set(warnings ${strict_warnings})
|
|
151
|
+
else()
|
|
152
|
+
message(FATAL_ERROR
|
|
153
|
+
"set_target_warnings: LEVEL must be basic, standard, or strict")
|
|
154
|
+
endif()
|
|
155
|
+
|
|
156
|
+
target_compile_options("${target}" "${visibility}" ${warnings})
|
|
128
157
|
endfunction()
|
|
@@ -1,48 +1,67 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
#[=======================================================================[.rst:
|
|
2
|
+
LinkSettings
|
|
3
|
+
------------
|
|
4
|
+
|
|
5
|
+
设置构建期运行时路径,并为测试取回 GoogleTest。
|
|
6
|
+
|
|
7
|
+
.. code-block:: cmake
|
|
8
|
+
|
|
9
|
+
include(LinkSettings)
|
|
10
|
+
|
|
11
|
+
Commands
|
|
12
|
+
^^^^^^^^
|
|
13
|
+
|
|
14
|
+
.. command:: set_target_build_rpath
|
|
15
|
+
|
|
16
|
+
.. code-block:: cmake
|
|
17
|
+
|
|
18
|
+
set_target_build_rpath(<target>)
|
|
19
|
+
|
|
20
|
+
让 ``build/bin`` 里的程序找到同级 ``build/lib`` 下的库。
|
|
21
|
+
|
|
22
|
+
只改构建树,不改安装后的 ``RPATH``。
|
|
23
|
+
|
|
24
|
+
.. command:: fetch_googletest
|
|
25
|
+
|
|
26
|
+
.. code-block:: cmake
|
|
27
|
+
|
|
28
|
+
fetch_googletest()
|
|
29
|
+
|
|
30
|
+
取回 GoogleTest 并让其 target 可用。
|
|
31
|
+
|
|
32
|
+
固定 v1.17.0 与 SHA-256;只在测试目录调用,库与程序代码不调用。
|
|
33
|
+
这是本文件唯一会下载的函数。
|
|
34
|
+
#]=======================================================================]
|
|
35
|
+
|
|
7
36
|
include_guard(GLOBAL)
|
|
8
37
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if(APPLE)
|
|
22
|
-
set_target_properties("${target}" PROPERTIES
|
|
23
|
-
BUILD_RPATH "@loader_path/../lib")
|
|
24
|
-
else()
|
|
25
|
-
set_target_properties("${target}" PROPERTIES BUILD_RPATH "$ORIGIN/../lib")
|
|
26
|
-
endif()
|
|
38
|
+
function(set_target_build_rpath target)
|
|
39
|
+
if(NOT TARGET "${target}")
|
|
40
|
+
message(FATAL_ERROR
|
|
41
|
+
"set_target_build_rpath: target '${target}' does not exist")
|
|
42
|
+
endif()
|
|
43
|
+
|
|
44
|
+
if(APPLE)
|
|
45
|
+
set_target_properties("${target}" PROPERTIES
|
|
46
|
+
BUILD_RPATH "@loader_path/../lib")
|
|
47
|
+
else()
|
|
48
|
+
set_target_properties("${target}" PROPERTIES BUILD_RPATH "$ORIGIN/../lib")
|
|
49
|
+
endif()
|
|
27
50
|
endfunction()
|
|
28
51
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
SHA256=65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c
|
|
45
|
-
)
|
|
46
|
-
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
|
47
|
-
FetchContent_MakeAvailable(googletest)
|
|
52
|
+
function(fetch_googletest)
|
|
53
|
+
include(FetchContent)
|
|
54
|
+
|
|
55
|
+
if(POLICY CMP0135)
|
|
56
|
+
cmake_policy(SET CMP0135 NEW)
|
|
57
|
+
endif()
|
|
58
|
+
|
|
59
|
+
FetchContent_Declare(
|
|
60
|
+
googletest
|
|
61
|
+
URL https://codeload.github.com/google/googletest/tar.gz/refs/tags/v1.17.0
|
|
62
|
+
URL_HASH
|
|
63
|
+
SHA256=65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c
|
|
64
|
+
)
|
|
65
|
+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
|
66
|
+
FetchContent_MakeAvailable(googletest)
|
|
48
67
|
endfunction()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#[=======================================================================[.rst:
|
|
2
|
+
OutputSettings
|
|
3
|
+
--------------
|
|
4
|
+
|
|
5
|
+
把构建产物输出到固定目录,便于脚本与文档按固定路径引用。
|
|
6
|
+
|
|
7
|
+
.. code-block:: cmake
|
|
8
|
+
|
|
9
|
+
include(OutputSettings)
|
|
10
|
+
|
|
11
|
+
Commands
|
|
12
|
+
^^^^^^^^
|
|
13
|
+
|
|
14
|
+
.. command:: set_output_directories
|
|
15
|
+
|
|
16
|
+
.. code-block:: cmake
|
|
17
|
+
|
|
18
|
+
set_output_directories()
|
|
19
|
+
|
|
20
|
+
在顶层 ``CMakeLists.txt`` 中调用一次,且要早于目标的创建(通常放在
|
|
21
|
+
``add_subdirectory()`` 之前)。函数把
|
|
22
|
+
``CMAKE_<TYPE>_OUTPUT_DIRECTORY`` 这类变量设为固定的输出目录;之后创建
|
|
23
|
+
目标时,CMake 用这些变量初始化目标的输出目录属性:
|
|
24
|
+
|
|
25
|
+
- ``RUNTIME`` 目标的产物输出到 ``build/bin``。
|
|
26
|
+
- ``LIBRARY`` 目标的产物输出到 ``build/lib``。
|
|
27
|
+
- ``ARCHIVE`` 目标的产物输出到 ``build/lib``。
|
|
28
|
+
#]=======================================================================]
|
|
29
|
+
|
|
30
|
+
include_guard(GLOBAL)
|
|
31
|
+
|
|
32
|
+
function(set_output_directories)
|
|
33
|
+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" PARENT_SCOPE)
|
|
34
|
+
|
|
35
|
+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" PARENT_SCOPE)
|
|
36
|
+
|
|
37
|
+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" PARENT_SCOPE)
|
|
38
|
+
endfunction()
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
- 普通函数的定义放 `.cpp`;在头文件里写非 `inline` 函数定义,多个翻译单元包含时会重复定义。
|
|
23
23
|
- 类定义放头文件;成员函数在类外定义时写 `ClassName::`,定义放 `.cpp`。
|
|
24
24
|
- 只用到声明时不要包含完整定义;前置声明可以减少编译依赖,但不能替代需要完整类型的场景(继承、成员对象、按值返回)。
|
|
25
|
-
- 每个头文件自包含:单独编译一个只包含该头文件的 `.cpp`
|
|
25
|
+
- 每个头文件自包含:单独编译一个只包含该头文件的 `.cpp` 应通过。这是编译单元边界的要求,不是链接要求。用 `#pragma once` 防止重复包含,不写 `#define` 宏守卫;宏名会把头文件绑死在项目路径上。
|
|
26
26
|
|
|
27
27
|
## 静态库与共享库
|
|
28
28
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:配 CMake 或工具链时;读完能回答:CMake 模块与函数;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:CMake 模块与函数;预算:一次只开这一份(118 行),不自动串联。 -->
|
|
2
2
|
# CMake 模块与函数
|
|
3
3
|
|
|
4
4
|
目标:同一段构建逻辑只有一份权威,函数可导入、可校验、可测试,目标目录只表达差异。
|
|
@@ -21,6 +21,7 @@ project/
|
|
|
21
21
|
cmake/
|
|
22
22
|
CompileSettings.cmake
|
|
23
23
|
LinkSettings.cmake
|
|
24
|
+
OutputSettings.cmake
|
|
24
25
|
component/
|
|
25
26
|
CMakeLists.txt
|
|
26
27
|
app/
|
|
@@ -28,9 +29,12 @@ project/
|
|
|
28
29
|
```
|
|
29
30
|
|
|
30
31
|
- 一个文件放一组语义相近的函数,不为每个函数建单独文件;文件过大或出现无关职责时再拆。
|
|
31
|
-
- 文件名用 UpperCamelCase,与 CMake 官方 `Modules/` 一致;按职责命名,例如 `CompileSettings.cmake`、`LinkSettings.cmake`。不用 kebab-case,也不用现在分词。
|
|
32
|
-
- 函数名用
|
|
33
|
-
|
|
32
|
+
- 文件名用 UpperCamelCase,与 CMake 官方 `Modules/` 一致;按职责命名,例如 `CompileSettings.cmake`、`LinkSettings.cmake`、`OutputSettings.cmake`。不用 kebab-case,也不用现在分词。
|
|
33
|
+
- 函数名用 snake_case,按“动词 + 对象”直接描述动作,例如
|
|
34
|
+
`set_target_cxx_standard`、`set_target_warnings`、`set_output_directories`;
|
|
35
|
+
不要机械重复模块名。CMake 没有命名空间,函数全局可见;确有重名风险时
|
|
36
|
+
再加短项目前缀,而不是完整模块名。
|
|
37
|
+
- 工程自己新增、不准备回流的函数同样先取动作名;与外部模块可能冲突时再加短项目前缀,例如 `my_project_do_something`。
|
|
34
38
|
- 只有项目真的需要时才建工程根的 `cmake/` 模块目录;一个函数都没有时不建空目录。
|
|
35
39
|
|
|
36
40
|
## 导入
|
|
@@ -52,31 +56,37 @@ include(CompileSettings)
|
|
|
52
56
|
```cmake
|
|
53
57
|
include_guard(GLOBAL)
|
|
54
58
|
|
|
55
|
-
function(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
function(set_target_cxx_standard target visibility standard)
|
|
60
|
+
if(NOT TARGET "${target}")
|
|
61
|
+
message(FATAL_ERROR "目标 ${target} 尚未创建")
|
|
62
|
+
endif()
|
|
63
|
+
|
|
64
|
+
string(SUBSTRING "${standard}" 3 -1 number)
|
|
65
|
+
|
|
66
|
+
target_compile_features("${target}" "${visibility}" "cxx_std_${number}")
|
|
67
|
+
|
|
68
|
+
set_target_properties("${target}" PROPERTIES CXX_STANDARD "${number}")
|
|
60
69
|
endfunction()
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
function(set_target_warnings target visibility)
|
|
72
|
+
cmake_parse_arguments(PARSE_ARGV 2 arg "" "LEVEL" "")
|
|
73
|
+
|
|
74
|
+
target_compile_options("${target}" "${visibility}" -Wall -Wextra)
|
|
66
75
|
endfunction()
|
|
67
76
|
```
|
|
68
77
|
|
|
69
78
|
规则:
|
|
70
79
|
|
|
71
|
-
-
|
|
80
|
+
- 参数顺序稳定,调用处不靠位置猜;参数多时在文档块里列出类型与取值范围。
|
|
72
81
|
- 先校验目标存在、可见性合法、标准在支持列表内,再调用 `target_*`。
|
|
73
82
|
- 失败用 `FATAL_ERROR`,消息带函数名与非法值;不要 `WARNING` 后继续。
|
|
74
|
-
- 只改传入 target 的属性;不写全局变量,不读环境变量,不下载依赖。`
|
|
83
|
+
- 只改传入 target 的属性;不写全局变量,不读环境变量,不下载依赖。`fetch_googletest` 是唯一例外,调用点限制在测试需要它的时候。
|
|
75
84
|
- 函数体保持幂等:同一目标重复调用不产生两份冲突属性。
|
|
85
|
+
- 函数体内并列的同类调用(如同一组 `set()`)各占一行,之间空一行;校验与执行等不同阶段之间也空一行。
|
|
76
86
|
|
|
77
87
|
## 契约注释
|
|
78
88
|
|
|
79
|
-
|
|
89
|
+
公开函数上方写官方文档块:文件开头一个 `#[=======================================================================[.rst:` 块,模块名下一行画标题线,命令用 `.. command::`、签名用 `.. code-block:: cmake`,块内散文写中文。同一文件不再画 `#---` 分隔线,函数体内不写行注释,说明都写进文档块,不画 `#===...===#` 方框。完整写法见 `../targets/01-target-rules.md`。
|
|
80
90
|
|
|
81
91
|
## 测试
|
|
82
92
|
|
|
@@ -25,8 +25,8 @@ CMake 有两套相关机制,必须同时写:
|
|
|
25
25
|
`../assets/CompileSettings.cmake` 提供:
|
|
26
26
|
|
|
27
27
|
```cmake
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
set_target_cxx_standard(app PRIVATE c++23)
|
|
29
|
+
set_target_cxx_standard(greeting PUBLIC c++20)
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
第一个参数是已由 `add_library` 或 `add_executable` 创建的目标,第二个是 `PUBLIC` / `PRIVATE` / `INTERFACE`,第三个是 `c++11`–`c++23`。函数内部做三步:校验目标与参数,调用 `target_compile_features`,再设置 `CXX_STANDARD` 三属性。
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# c++11:把 target 名换成实际 target;与 environment.json 的 standard 一致。
|
|
2
2
|
target_compile_features(app PRIVATE cxx_std_11)
|
|
3
3
|
set_target_properties(app PROPERTIES
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
CXX_STANDARD 11
|
|
5
|
+
CXX_STANDARD_REQUIRED ON
|
|
6
|
+
CXX_EXTENSIONS OFF
|
|
7
7
|
)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# c++14:把 target 名换成实际 target;与 environment.json 的 standard 一致。
|
|
2
2
|
target_compile_features(app PRIVATE cxx_std_14)
|
|
3
3
|
set_target_properties(app PROPERTIES
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
CXX_STANDARD 14
|
|
5
|
+
CXX_STANDARD_REQUIRED ON
|
|
6
|
+
CXX_EXTENSIONS OFF
|
|
7
7
|
)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# c++17:把 target 名换成实际 target;与 environment.json 的 standard 一致。
|
|
2
2
|
target_compile_features(app PRIVATE cxx_std_17)
|
|
3
3
|
set_target_properties(app PROPERTIES
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
CXX_STANDARD 17
|
|
5
|
+
CXX_STANDARD_REQUIRED ON
|
|
6
|
+
CXX_EXTENSIONS OFF
|
|
7
7
|
)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# c++20:把 target 名换成实际 target;与 environment.json 的 standard 一致。
|
|
2
2
|
target_compile_features(app PRIVATE cxx_std_20)
|
|
3
3
|
set_target_properties(app PROPERTIES
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
CXX_STANDARD 20
|
|
5
|
+
CXX_STANDARD_REQUIRED ON
|
|
6
|
+
CXX_EXTENSIONS OFF
|
|
7
7
|
)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# c++23:把 target 名换成实际 target;与 environment.json 的 standard 一致。
|
|
2
2
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
3
3
|
set_target_properties(app PROPERTIES
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
CXX_STANDARD 23
|
|
5
|
+
CXX_STANDARD_REQUIRED ON
|
|
6
|
+
CXX_EXTENSIONS OFF
|
|
7
7
|
)
|
|
@@ -14,7 +14,7 @@ set_target_properties(app PROPERTIES CXX_STANDARD 20)
|
|
|
14
14
|
**好**:
|
|
15
15
|
|
|
16
16
|
```cmake
|
|
17
|
-
|
|
17
|
+
set_target_cxx_standard(app PRIVATE c++20)
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
两套声明同时写;`CXX_STANDARD_REQUIRED ON` 禁止降级,`CXX_EXTENSIONS OFF` 把 `gnu++20` 收成 `c++20`。
|
|
@@ -46,7 +46,7 @@ Google C++ Style Guide 上游仍写 “target C++20”,本工作流默认 C++2
|
|
|
46
46
|
| --- | --- | --- |
|
|
47
47
|
| `std::string_view`、`std::optional`、`std::variant`、`if constexpr` | C++17 | 随语言核心 |
|
|
48
48
|
| `std::format` | C++20 | libstdc++ 13+;libc++ 14+(特性宏到 19 才稳定) |
|
|
49
|
-
| `std::print`、`std::expected`、`std::stacktrace` | C++23 | `std::print`:libstdc++ 14+、libc++
|
|
49
|
+
| `std::print`、`std::expected`、`std::stacktrace` | C++23 | `std::print`:libstdc++ 14+、libc++ 18+;`std::expected` 随各库 C++23 实现;`std::stacktrace` 还要链接对应库 |
|
|
50
50
|
|
|
51
51
|
Clang/GCC 的 “Partial” 状态意味着 C++23 不是全量实现。用到具体设施前,先查:
|
|
52
52
|
|
|
@@ -77,15 +77,15 @@ set_target_properties(app PROPERTIES
|
|
|
77
77
|
|
|
78
78
|
### clang-format
|
|
79
79
|
|
|
80
|
-
clang-format
|
|
80
|
+
并非所有 clang-format 版本都接受 `c++23`;遇到不识别该值的工具会报 `unknown enumerated scalar`。为兼容这些工具,映射如下:
|
|
81
81
|
|
|
82
82
|
| 声明 | 配置 |
|
|
83
83
|
| --- | --- |
|
|
84
84
|
| `c++11`–`c++20` | 对应 `Standard: c++NN` |
|
|
85
|
-
| `c++23` | `Standard: Latest
|
|
85
|
+
| `c++23` | `Standard: Latest`(由工具取支持的最高标准;工具接受时也可直接写 `c++23`) |
|
|
86
86
|
| `c++26`(未来) | 工具支持时写 `c++26`,否则 `Latest` |
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
写 `c++23` 前先确认工具接受该值;不确定时用 `Latest`。
|
|
89
89
|
|
|
90
90
|
### clangd
|
|
91
91
|
|
|
@@ -105,7 +105,7 @@ CompileFlags:
|
|
|
105
105
|
`.clang-tidy` 不写标准,标准来自编译数据库。检查项保持 Google 基线:
|
|
106
106
|
|
|
107
107
|
```yaml
|
|
108
|
-
Checks: '-*, google-*,
|
|
108
|
+
Checks: '-*, google-*, readability-identifier-naming'
|
|
109
109
|
WarningsAsErrors: '*'
|
|
110
110
|
FormatStyle: file
|
|
111
111
|
```
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
|
|
40
40
|
- 能用:C++20 的全部内容;`std::print`/`std::println`、`std::expected`、`std::stacktrace`、`if consteval`、显式对象参数(deducing this)、`std::mdspan`、`std::flat_map`、ranges 的 C++23 增补(工具链实现为准)。
|
|
41
41
|
- 不能假设:C++23 在 GCC/Clang 上仍是 Partial,具体设施要查编译器状态页;`std::print` 的库门槛是 libstdc++ 14+ / libc++ 19+;`std::stacktrace` 还要链接对应库;`std::expected` 的 monadic 接口、`std::flat_map` 等设施在旧标准库上可能缺失;模板只用 `std::format`,不因为默认 C++23 就强依赖 `print`/`stacktrace`。
|
|
42
|
-
- 降档/升级:从 C++23 降到 C++20 见上一节;升级到 C++26 前先确认 CMake 3.
|
|
42
|
+
- 降档/升级:从 C++23 降到 C++20 见上一节;升级到 C++26 前先确认 CMake 3.30+(首次接受 CXX_STANDARD 26/cxx_std_26)、编译器/标准库支持、clang-format 的 `c++26` 枚举,并按 [index.md](index.md#新增标准) 的流程新增片段与矩阵行。
|
|
43
43
|
- 验证:`compile_commands.json` 的最后一个 `-std=` 必须是 `-std=c++23`;模板在 C++23 下走 `std::format` 分支;若使用 `print`/`stacktrace`,另做链接测试。
|
|
44
44
|
- 片段:[cmake-snippet.cmake](c++23/cmake-snippet.cmake)。
|