@chengzhao-dev/dsh-cpp-workflow 0.1.1 → 0.2.0
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 +18 -15
- package/README.md +18 -15
- package/lib/index.js +7 -8
- package/lib/workspace.js +14 -14
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +5 -4
- package/skills/building-cpp/SKILL.md +6 -6
- package/skills/building-cpp/assets/config/.clang-format +3 -38
- package/skills/building-cpp/assets/config/.clang-tidy +8 -39
- package/skills/building-cpp/assets/config/.clangd +2 -5
- package/skills/building-cpp/assets/config/.vscode/extensions.json +2 -1
- package/skills/building-cpp/assets/config/.vscode/settings.json +16 -1
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +29 -18
- package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +9 -5
- package/skills/building-cpp/assets/templates/projects/multi/include/greeting.h +5 -3
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cc +18 -0
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cc +8 -0
- package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +8 -3
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +15 -5
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h +5 -3
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cc +18 -0
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cc +8 -0
- package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +7 -3
- package/skills/building-cpp/assets/templates/projects/single/src/main.cc +15 -0
- package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +8 -3
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +15 -5
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h +5 -3
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cc +18 -0
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cc +8 -0
- package/skills/building-cpp/examples/cmake.md +17 -17
- package/skills/building-cpp/examples/environment.md +4 -3
- package/skills/building-cpp/examples/tooling.md +31 -16
- package/skills/building-cpp/references/build-script.md +2 -2
- package/skills/building-cpp/references/cmake.md +35 -17
- package/skills/building-cpp/references/environment.md +17 -13
- package/skills/building-cpp/references/linking.md +6 -6
- package/skills/building-cpp/references/standards/README.md +165 -0
- package/skills/building-cpp/references/standards/c++11/README.md +50 -0
- package/skills/building-cpp/references/standards/c++11/cmake-snippet.cmake +7 -0
- package/skills/building-cpp/references/standards/c++14/README.md +48 -0
- package/skills/building-cpp/references/standards/c++14/cmake-snippet.cmake +7 -0
- package/skills/building-cpp/references/standards/c++17/README.md +49 -0
- package/skills/building-cpp/references/standards/c++17/cmake-snippet.cmake +7 -0
- package/skills/building-cpp/references/standards/c++20/README.md +49 -0
- package/skills/building-cpp/references/standards/c++20/cmake-snippet.cmake +7 -0
- package/skills/building-cpp/references/standards/c++23/README.md +50 -0
- package/skills/building-cpp/references/standards/c++23/cmake-snippet.cmake +7 -0
- package/skills/building-cpp/references/static-analysis.md +27 -15
- package/skills/building-cpp/scripts/checking-environment.mjs +21 -21
- package/skills/building-cpp/steps/01-build.md +10 -8
- package/skills/building-cpp/steps/02-tooling.md +9 -7
- package/skills/building-cpp/steps/03-check.md +4 -4
- package/skills/developing-cpp/SKILL.md +6 -6
- package/skills/developing-cpp/examples/comments.md +10 -8
- package/skills/developing-cpp/examples/exceptions.md +64 -45
- package/skills/developing-cpp/examples/naming.md +2 -2
- package/skills/developing-cpp/examples/structure.md +12 -9
- package/skills/developing-cpp/references/comments.md +10 -8
- package/skills/developing-cpp/references/declaration-order.md +7 -4
- package/skills/developing-cpp/references/exceptions.md +83 -46
- package/skills/developing-cpp/references/glossary.md +6 -6
- package/skills/developing-cpp/references/google-cpp-style.md +27 -37
- package/skills/developing-cpp/references/language.md +4 -4
- package/skills/developing-cpp/references/layout.md +14 -14
- package/skills/developing-cpp/references/naming.md +7 -6
- package/skills/developing-cpp/references/project-design.md +3 -3
- package/skills/developing-cpp/references/standard-library.md +1 -1
- package/skills/developing-cpp/steps/02-boundary.md +7 -7
- package/skills/developing-cpp/steps/03-declaration.md +9 -8
- package/skills/developing-cpp/steps/04-implementation.md +11 -7
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/templates/cpp-workflow-project/environment.json +2 -1
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cpp +0 -13
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cpp +0 -8
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp +0 -13
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cpp +0 -8
- package/skills/building-cpp/assets/templates/projects/single/src/main.cpp +0 -9
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp +0 -13
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cpp +0 -8
|
@@ -7,12 +7,16 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
|
7
7
|
enable_testing()
|
|
8
8
|
|
|
9
9
|
add_executable(app
|
|
10
|
-
src/main.
|
|
11
|
-
src/greeting.
|
|
10
|
+
src/main.cc
|
|
11
|
+
src/greeting.cc
|
|
12
|
+
)
|
|
13
|
+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
|
14
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
15
|
+
set_target_properties(app PROPERTIES
|
|
16
|
+
CXX_STANDARD 23
|
|
17
|
+
CXX_STANDARD_REQUIRED ON
|
|
18
|
+
CXX_EXTENSIONS OFF
|
|
12
19
|
)
|
|
13
|
-
target_include_directories(app PRIVATE include)
|
|
14
|
-
target_compile_features(app PRIVATE cxx_std_20)
|
|
15
|
-
set_target_properties(app PROPERTIES CXX_EXTENSIONS OFF)
|
|
16
20
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
17
21
|
|
|
18
22
|
add_test(NAME app_smoke COMMAND app)
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
#ifndef CPP_WORKFLOW_TEMPLATE_MULTI_INCLUDE_GREETING_H_
|
|
2
|
+
#define CPP_WORKFLOW_TEMPLATE_MULTI_INCLUDE_GREETING_H_
|
|
2
3
|
|
|
3
4
|
#include <string>
|
|
4
|
-
#include <string_view>
|
|
5
5
|
|
|
6
6
|
namespace greeting {
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
std::string MakeGreeting(const std::string& name);
|
|
9
9
|
|
|
10
10
|
} // namespace greeting
|
|
11
|
+
|
|
12
|
+
#endif // CPP_WORKFLOW_TEMPLATE_MULTI_INCLUDE_GREETING_H_
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#include "include/greeting.h"
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#if __cplusplus >= 202002L
|
|
5
|
+
#include <format>
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
namespace greeting {
|
|
9
|
+
|
|
10
|
+
std::string MakeGreeting(const std::string& name) {
|
|
11
|
+
#if __cplusplus >= 202002L
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
|
+
#else
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
|
+
#endif
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
} // namespace greeting
|
|
@@ -9,9 +9,14 @@ enable_testing()
|
|
|
9
9
|
|
|
10
10
|
add_subdirectory(greeting)
|
|
11
11
|
|
|
12
|
-
add_executable(app src/main.
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
add_executable(app src/main.cc)
|
|
13
|
+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
|
14
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
15
|
+
set_target_properties(app PROPERTIES
|
|
16
|
+
CXX_STANDARD 23
|
|
17
|
+
CXX_STANDARD_REQUIRED ON
|
|
18
|
+
CXX_EXTENSIONS OFF
|
|
19
|
+
)
|
|
15
20
|
target_link_libraries(app PRIVATE greeting)
|
|
16
21
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
17
22
|
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
add_library(greeting SHARED src/greeting.
|
|
2
|
-
target_include_directories(greeting PUBLIC
|
|
3
|
-
target_compile_features(greeting PUBLIC
|
|
4
|
-
set_target_properties(greeting PROPERTIES
|
|
5
|
-
|
|
1
|
+
add_library(greeting SHARED src/greeting.cc)
|
|
2
|
+
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
|
3
|
+
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
4
|
+
set_target_properties(greeting PROPERTIES
|
|
5
|
+
CXX_STANDARD 23
|
|
6
|
+
CXX_STANDARD_REQUIRED ON
|
|
7
|
+
CXX_EXTENSIONS OFF
|
|
8
|
+
)
|
|
9
|
+
target_compile_options(greeting PRIVATE
|
|
10
|
+
-Wall
|
|
11
|
+
-Wextra
|
|
12
|
+
-Werror
|
|
13
|
+
-Wold-style-cast
|
|
14
|
+
-Wshadow
|
|
15
|
+
)
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
#ifndef CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
2
|
+
#define CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
2
3
|
|
|
3
4
|
#include <string>
|
|
4
|
-
#include <string_view>
|
|
5
5
|
|
|
6
6
|
namespace greeting {
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
std::string MakeGreeting(const std::string& name);
|
|
9
9
|
|
|
10
10
|
} // namespace greeting
|
|
11
|
+
|
|
12
|
+
#endif // CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#include "greeting/include/greeting.h"
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#if __cplusplus >= 202002L
|
|
5
|
+
#include <format>
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
namespace greeting {
|
|
9
|
+
|
|
10
|
+
std::string MakeGreeting(const std::string& name) {
|
|
11
|
+
#if __cplusplus >= 202002L
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
|
+
#else
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
|
+
#endif
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
} // namespace greeting
|
|
@@ -6,9 +6,13 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
|
6
6
|
|
|
7
7
|
enable_testing()
|
|
8
8
|
|
|
9
|
-
add_executable(app src/main.
|
|
10
|
-
target_compile_features(app PRIVATE
|
|
11
|
-
set_target_properties(app PROPERTIES
|
|
9
|
+
add_executable(app src/main.cc)
|
|
10
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
11
|
+
set_target_properties(app PROPERTIES
|
|
12
|
+
CXX_STANDARD 23
|
|
13
|
+
CXX_STANDARD_REQUIRED ON
|
|
14
|
+
CXX_EXTENSIONS OFF
|
|
15
|
+
)
|
|
12
16
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
13
17
|
|
|
14
18
|
add_test(NAME app_smoke COMMAND app)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#include <iostream>
|
|
2
|
+
#include <string>
|
|
3
|
+
#if __cplusplus >= 202002L
|
|
4
|
+
#include <format>
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
int main() {
|
|
8
|
+
const std::string name = "C++";
|
|
9
|
+
#if __cplusplus >= 202002L
|
|
10
|
+
std::cout << std::format("Hello, {}!\n", name);
|
|
11
|
+
#else
|
|
12
|
+
std::cout << "Hello, " << name << "!\n";
|
|
13
|
+
#endif
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
@@ -9,9 +9,14 @@ enable_testing()
|
|
|
9
9
|
|
|
10
10
|
add_subdirectory(greeting)
|
|
11
11
|
|
|
12
|
-
add_executable(app src/main.
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
add_executable(app src/main.cc)
|
|
13
|
+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
|
14
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
15
|
+
set_target_properties(app PROPERTIES
|
|
16
|
+
CXX_STANDARD 23
|
|
17
|
+
CXX_STANDARD_REQUIRED ON
|
|
18
|
+
CXX_EXTENSIONS OFF
|
|
19
|
+
)
|
|
15
20
|
target_link_libraries(app PRIVATE greeting)
|
|
16
21
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
17
22
|
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
add_library(greeting STATIC src/greeting.
|
|
2
|
-
target_include_directories(greeting PUBLIC
|
|
3
|
-
target_compile_features(greeting PUBLIC
|
|
4
|
-
set_target_properties(greeting PROPERTIES
|
|
5
|
-
|
|
1
|
+
add_library(greeting STATIC src/greeting.cc)
|
|
2
|
+
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
|
3
|
+
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
4
|
+
set_target_properties(greeting PROPERTIES
|
|
5
|
+
CXX_STANDARD 23
|
|
6
|
+
CXX_STANDARD_REQUIRED ON
|
|
7
|
+
CXX_EXTENSIONS OFF
|
|
8
|
+
)
|
|
9
|
+
target_compile_options(greeting PRIVATE
|
|
10
|
+
-Wall
|
|
11
|
+
-Wextra
|
|
12
|
+
-Werror
|
|
13
|
+
-Wold-style-cast
|
|
14
|
+
-Wshadow
|
|
15
|
+
)
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
#ifndef CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
2
|
+
#define CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
2
3
|
|
|
3
4
|
#include <string>
|
|
4
|
-
#include <string_view>
|
|
5
5
|
|
|
6
6
|
namespace greeting {
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
std::string MakeGreeting(const std::string& name);
|
|
9
9
|
|
|
10
10
|
} // namespace greeting
|
|
11
|
+
|
|
12
|
+
#endif // CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#include "greeting/include/greeting.h"
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#if __cplusplus >= 202002L
|
|
5
|
+
#include <format>
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
namespace greeting {
|
|
9
|
+
|
|
10
|
+
std::string MakeGreeting(const std::string& name) {
|
|
11
|
+
#if __cplusplus >= 202002L
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
|
+
#else
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
|
+
#endif
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
} // namespace greeting
|
|
@@ -10,14 +10,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
10
10
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
11
11
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
12
12
|
|
|
13
|
-
add_library(greeting STATIC greeting/src/greeting.
|
|
13
|
+
add_library(greeting STATIC greeting/src/greeting.cc)
|
|
14
14
|
target_include_directories(greeting PUBLIC greeting/include)
|
|
15
|
-
target_compile_features(greeting PUBLIC
|
|
15
|
+
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
16
16
|
set_target_properties(greeting PROPERTIES CXX_EXTENSIONS OFF)
|
|
17
17
|
target_compile_options(greeting PRIVATE -Wall -Wextra -Werror)
|
|
18
18
|
|
|
19
|
-
add_executable(app src/main.
|
|
20
|
-
target_compile_features(app PRIVATE
|
|
19
|
+
add_executable(app src/main.cc)
|
|
20
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
21
21
|
set_target_properties(app PROPERTIES CXX_EXTENSIONS OFF)
|
|
22
22
|
target_link_libraries(app PRIVATE greeting)
|
|
23
23
|
```
|
|
@@ -30,7 +30,7 @@ target_link_libraries(app PRIVATE greeting)
|
|
|
30
30
|
|
|
31
31
|
```cmake
|
|
32
32
|
include_directories(include greeting/include)
|
|
33
|
-
add_executable(app src/main.
|
|
33
|
+
add_executable(app src/main.cc greeting/src/greeting.cc)
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
问题:所有 target 共享 include,边界消失;程序直接编译库源码,测试无法只链接库。
|
|
@@ -47,7 +47,7 @@ target_link_libraries(app PRIVATE greeting)
|
|
|
47
47
|
**差**:
|
|
48
48
|
|
|
49
49
|
```cmake
|
|
50
|
-
file(GLOB SOURCES "src/*.
|
|
50
|
+
file(GLOB SOURCES "src/*.cc")
|
|
51
51
|
add_executable(app ${SOURCES})
|
|
52
52
|
```
|
|
53
53
|
|
|
@@ -57,9 +57,9 @@ add_executable(app ${SOURCES})
|
|
|
57
57
|
|
|
58
58
|
```cmake
|
|
59
59
|
add_executable(app
|
|
60
|
-
src/main.
|
|
61
|
-
src/task_list.
|
|
62
|
-
src/task_parser.
|
|
60
|
+
src/main.cc
|
|
61
|
+
src/task_list.cc
|
|
62
|
+
src/task_parser.cc
|
|
63
63
|
)
|
|
64
64
|
```
|
|
65
65
|
|
|
@@ -70,15 +70,15 @@ add_executable(app
|
|
|
70
70
|
**差**:
|
|
71
71
|
|
|
72
72
|
```cmake
|
|
73
|
-
add_library(core STATIC src/main.
|
|
74
|
-
add_executable(app src/main.
|
|
73
|
+
add_library(core STATIC src/main.cc src/core.cc)
|
|
74
|
+
add_executable(app src/main.cc) # 重复定义
|
|
75
75
|
```
|
|
76
76
|
|
|
77
77
|
**好**:
|
|
78
78
|
|
|
79
79
|
```cmake
|
|
80
|
-
add_library(core STATIC src/core.
|
|
81
|
-
add_executable(app src/main.
|
|
80
|
+
add_library(core STATIC src/core.cc)
|
|
81
|
+
add_executable(app src/main.cc)
|
|
82
82
|
target_link_libraries(app PRIVATE core)
|
|
83
83
|
```
|
|
84
84
|
|
|
@@ -93,7 +93,7 @@ target_link_libraries(app PRIVATE core)
|
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
```bash
|
|
96
|
-
clang-tidy src/main.
|
|
96
|
+
clang-tidy src/main.cc # 没有编译参数,头路径和标准都不对
|
|
97
97
|
```
|
|
98
98
|
|
|
99
99
|
**好**:
|
|
@@ -106,7 +106,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
106
106
|
cmake -S . -B build/clang -G Ninja -DCMAKE_CXX_COMPILER=clang++
|
|
107
107
|
rm -f build/compile_commands.json
|
|
108
108
|
ln -s clang/compile_commands.json build/compile_commands.json
|
|
109
|
-
clang-tidy -p build/clang src/main.
|
|
109
|
+
clang-tidy -p build/clang src/main.cc
|
|
110
110
|
```
|
|
111
111
|
|
|
112
112
|
## 共享库运行时路径
|
|
@@ -120,11 +120,11 @@ LD_LIBRARY_PATH=build/clang/lib ./build/clang/bin/app # 每次运行都要包
|
|
|
120
120
|
**好**:
|
|
121
121
|
|
|
122
122
|
```cmake
|
|
123
|
-
add_library(greeting SHARED greeting/src/greeting.
|
|
123
|
+
add_library(greeting SHARED greeting/src/greeting.cc)
|
|
124
124
|
set_target_properties(greeting PROPERTIES
|
|
125
125
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
126
126
|
|
|
127
|
-
add_executable(app src/main.
|
|
127
|
+
add_executable(app src/main.cc)
|
|
128
128
|
target_link_libraries(app PRIVATE greeting)
|
|
129
129
|
if(APPLE)
|
|
130
130
|
set_target_properties(app PROPERTIES BUILD_RPATH "@loader_path/../lib")
|
|
@@ -70,13 +70,14 @@ WSL 只对 `host=windows` 合法;不要通过改 `host` 来绕过,先确认
|
|
|
70
70
|
}
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
这不是「合法但暂时不能构建」,而是本版本直接拒绝的组合。声明校验返回:
|
|
74
74
|
|
|
75
75
|
```text
|
|
76
|
-
|
|
76
|
+
ok: false
|
|
77
|
+
issues: ["本版本不能在 Windows 原生壳里构建"]
|
|
77
78
|
```
|
|
78
79
|
|
|
79
|
-
|
|
80
|
+
停在这里。不要建议改 `build_host` 为 `wsl` 来绕过,也不要补 PowerShell 脚本。
|
|
80
81
|
|
|
81
82
|
## GCC 模式不比较 GCC 主版本
|
|
82
83
|
|
|
@@ -33,6 +33,27 @@ CompileFlags:
|
|
|
33
33
|
}
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
## `.clangd` 写死标准
|
|
37
|
+
|
|
38
|
+
**差**:
|
|
39
|
+
|
|
40
|
+
```yaml
|
|
41
|
+
# 工程已经在 CMake 里声明 C++23
|
|
42
|
+
CompileFlags:
|
|
43
|
+
CompilationDatabase: build/clang
|
|
44
|
+
Add:
|
|
45
|
+
- -std=c++20 # 覆盖 compile_commands.json,C++23 补全/诊断全部降级
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**好**:
|
|
49
|
+
|
|
50
|
+
```yaml
|
|
51
|
+
CompileFlags:
|
|
52
|
+
CompilationDatabase: build/clang
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
标准只来自 `compile_commands.json`。没有数据库时先回到定构建生成,不要在 `.clangd` 里猜一个 `-std`。
|
|
56
|
+
|
|
36
57
|
## 编译数据库指错目录
|
|
37
58
|
|
|
38
59
|
**差**:
|
|
@@ -64,11 +85,13 @@ ColumnLimit: 100
|
|
|
64
85
|
**好**:
|
|
65
86
|
|
|
66
87
|
```yaml
|
|
67
|
-
#
|
|
68
|
-
|
|
88
|
+
# 全部取 Google 预设:80 列、2 空格、不用 tab、包含顺序。
|
|
89
|
+
Language: Cpp
|
|
90
|
+
BasedOnStyle: Google
|
|
91
|
+
Standard: Latest
|
|
69
92
|
```
|
|
70
93
|
|
|
71
|
-
|
|
94
|
+
配置只选预设和语言版本,不重复策略;列宽、缩进、包含顺序都由 Google 预设决定。需要新规则时先改技能文档并说明理由,不要在手写配置里堆本地约定。
|
|
72
95
|
|
|
73
96
|
## 整族关闭检查
|
|
74
97
|
|
|
@@ -87,26 +110,18 @@ Checks: >
|
|
|
87
110
|
```yaml
|
|
88
111
|
Checks: >
|
|
89
112
|
-*,
|
|
90
|
-
clang-analyzer-*,
|
|
91
|
-
bugprone-*,
|
|
92
|
-
cppcoreguidelines-*,
|
|
93
113
|
google-*,
|
|
94
|
-
|
|
95
|
-
performance-*,
|
|
96
|
-
readability-*
|
|
97
|
-
CheckOptions:
|
|
98
|
-
# 团队用 100 列,tidy 的列宽判断跟随 clang-format。
|
|
99
|
-
readability-identifier-length.MinimumVariableNameLength: 2
|
|
114
|
+
readability-identifier-naming
|
|
100
115
|
```
|
|
101
116
|
|
|
102
|
-
|
|
117
|
+
保留 Google 检查主体和项目命名规则;关闭项逐条写理由,不整族关闭。不设 `FunctionCase`,Google 允许 `count()` / `set_count()` 这类访问器。
|
|
103
118
|
|
|
104
119
|
## tidy 扫描系统头
|
|
105
120
|
|
|
106
121
|
**差**:
|
|
107
122
|
|
|
108
123
|
```bash
|
|
109
|
-
clang-tidy -p build/clang --system-headers src/main.
|
|
124
|
+
clang-tidy -p build/clang --system-headers src/main.cc
|
|
110
125
|
```
|
|
111
126
|
|
|
112
127
|
系统头也参与诊断,出现大量第三方告警。
|
|
@@ -115,7 +130,7 @@ clang-tidy -p build/clang --system-headers src/main.cpp
|
|
|
115
130
|
|
|
116
131
|
```bash
|
|
117
132
|
clang-tidy -p build/clang --header-filter="^$(pwd -P)/(include|src)/" \
|
|
118
|
-
--warnings-as-errors='*' src/main.
|
|
133
|
+
--warnings-as-errors='*' src/main.cc
|
|
119
134
|
```
|
|
120
135
|
|
|
121
136
|
`--header-filter` 只列项目顶层目录,避免把第三方头纳入;系统头默认排除。工程有别的顶层目录时按实际替换。
|
|
@@ -125,7 +140,7 @@ clang-tidy -p build/clang --header-filter="^$(pwd -P)/(include|src)/" \
|
|
|
125
140
|
**差**:
|
|
126
141
|
|
|
127
142
|
```bash
|
|
128
|
-
clang-format -i src/*.
|
|
143
|
+
clang-format -i src/*.cc # 构建时悄悄改源码
|
|
129
144
|
```
|
|
130
145
|
|
|
131
146
|
**好**:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 构建脚本
|
|
2
2
|
|
|
3
|
-
`assets/templates/projects/common/running-build.sh` 是 POSIX build-host 脚本,适用于 Linux、macOS 和 Windows + WSL
|
|
3
|
+
`assets/templates/projects/common/running-build.sh` 是 POSIX build-host 脚本,适用于 Linux、macOS 和 Windows + WSL。`host=windows` + `build_host=native` 不是本版本支持的组合:声明校验会直接拒绝,脚本也不会在原生 Windows 壳里运行。
|
|
4
4
|
|
|
5
5
|
## 用法
|
|
6
6
|
|
|
@@ -60,4 +60,4 @@
|
|
|
60
60
|
- 从工程根直接 `./build/clang/bin/app` 能启动(共享库场景由 RPATH 保证)。
|
|
61
61
|
- 切换编译器后 `build/compile_commands.json` 指向新数据库。
|
|
62
62
|
- 调用者的 `LD_LIBRARY_PATH` 在脚本结束后不变。
|
|
63
|
-
- Windows
|
|
63
|
+
- Windows 原生:声明校验已拒绝该组合;不要伪造 PowerShell 脚本,也不要绕过声明。
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
目标:一个编译器、Ninja、target-based 规则、可用的编译数据库。下面是最小形状和常见边界;模板在 `assets/templates/projects/`。
|
|
4
4
|
|
|
5
|
+
## 排版
|
|
6
|
+
|
|
7
|
+
CMake 没有官方风格指南。这里跟随 Google 自家项目的 `cmake-format` 配置:缩进 2 个空格、行宽 80、不用 tab。来源是 [or-tools 的 .cmake-format.py](https://github.com/google/or-tools/blob/stable/.cmake-format.py) 与 [draco 的 .cmake-format.py](https://github.com/google/draco/blob/master/.cmake-format.py),两处都设 `tab_size=2`、`line_width=80`。命令行、列表和注释都按这个宽度换行;`CMakeLists.txt` 与 `*.cmake` 一视同仁。
|
|
8
|
+
|
|
9
|
+
Shell 脚本用 2 个空格缩进、不用 tab,来源是 [Google Shell Style Guide](https://google.github.io/styleguide/shellguide.html#indentation)。
|
|
10
|
+
|
|
5
11
|
## 最小可用形状
|
|
6
12
|
|
|
7
13
|
### 单文件
|
|
@@ -13,9 +19,13 @@ project({{PROJECT_NAME}} VERSION 0.1.0 LANGUAGES CXX)
|
|
|
13
19
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
14
20
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
15
21
|
|
|
16
|
-
add_executable(app src/main.
|
|
17
|
-
target_compile_features(app PRIVATE
|
|
18
|
-
set_target_properties(app PROPERTIES
|
|
22
|
+
add_executable(app src/main.cc)
|
|
23
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
24
|
+
set_target_properties(app PROPERTIES
|
|
25
|
+
CXX_STANDARD 23
|
|
26
|
+
CXX_STANDARD_REQUIRED ON
|
|
27
|
+
CXX_EXTENSIONS OFF
|
|
28
|
+
)
|
|
19
29
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
20
30
|
```
|
|
21
31
|
|
|
@@ -32,32 +42,40 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
|
32
42
|
|
|
33
43
|
add_subdirectory(greeting)
|
|
34
44
|
|
|
35
|
-
add_executable(app src/main.
|
|
36
|
-
target_compile_features(app PRIVATE
|
|
37
|
-
set_target_properties(app PROPERTIES
|
|
45
|
+
add_executable(app src/main.cc)
|
|
46
|
+
target_compile_features(app PRIVATE cxx_std_23)
|
|
47
|
+
set_target_properties(app PROPERTIES
|
|
48
|
+
CXX_STANDARD 23
|
|
49
|
+
CXX_STANDARD_REQUIRED ON
|
|
50
|
+
CXX_EXTENSIONS OFF
|
|
51
|
+
)
|
|
38
52
|
target_link_libraries(app PRIVATE greeting)
|
|
39
53
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
40
54
|
```
|
|
41
55
|
|
|
42
56
|
```cmake
|
|
43
57
|
# greeting/CMakeLists.txt
|
|
44
|
-
add_library(greeting STATIC src/greeting.
|
|
45
|
-
target_include_directories(greeting PUBLIC
|
|
46
|
-
target_compile_features(greeting PUBLIC
|
|
47
|
-
set_target_properties(greeting PROPERTIES
|
|
58
|
+
add_library(greeting STATIC src/greeting.cc)
|
|
59
|
+
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
|
60
|
+
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
61
|
+
set_target_properties(greeting PROPERTIES
|
|
62
|
+
CXX_STANDARD 23
|
|
63
|
+
CXX_STANDARD_REQUIRED ON
|
|
64
|
+
CXX_EXTENSIONS OFF
|
|
65
|
+
)
|
|
48
66
|
target_compile_options(greeting PRIVATE -Wall -Wextra -Werror -Wold-style-cast -Wshadow)
|
|
49
67
|
```
|
|
50
68
|
|
|
51
|
-
- 库的 `PUBLIC
|
|
52
|
-
- `
|
|
53
|
-
- `CXX_EXTENSIONS OFF` 把 `-std=gnu++
|
|
69
|
+
- 库的 `PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..` 让程序能从仓库根写 `#include "greeting/include/greeting.h"`,不需要程序再写 include 路径。
|
|
70
|
+
- `cxx_std_23` 用 `PUBLIC` 从库传播;程序自己再加一次也无害。`CXX_STANDARD_REQUIRED ON` 防止 CMake 在编译器不支持时静默 decay;显式 `CXX_STANDARD` 与 `standard` 保持一致。
|
|
71
|
+
- `CXX_EXTENSIONS OFF` 把 `-std=gnu++23` 收成 `-std=c++23`,与 Google 的「不用非标准扩展」一致;每个 target 都要设。configure 后检查 `compile_commands.json` 的最后一条 `-std=`,不能是 `gnu++NN`。
|
|
54
72
|
- 程序 target 固定 `app`;库 target 用组件名。
|
|
55
73
|
|
|
56
74
|
## target-based 规则
|
|
57
75
|
|
|
58
76
|
- 每个 target 自己声明 include、标准、编译选项、链接库;不要用全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS` 给所有 target 加料。
|
|
59
77
|
- `target_include_directories(<target> PUBLIC|PRIVATE <dir>)`:公共头给 `PUBLIC`,私有实现目录给 `PRIVATE`。
|
|
60
|
-
- `target_compile_features(<target> PUBLIC
|
|
78
|
+
- `target_compile_features(<target> PUBLIC cxx_std_23)`:标准是 target 属性,不是全局开关。
|
|
61
79
|
- `target_link_libraries(<target> PRIVATE|PUBLIC <lib>)`:实现依赖用 `PRIVATE`,接口依赖用 `PUBLIC`。
|
|
62
80
|
- 不要 `file(GLOB ...)`;显式列源文件,新增文件时改 CMake,构建系统才知道。`CONFIGURE_DEPENDS` 依赖构建系统每次检查目录,跨生成器行为也不完全一致,不作为工程规则。
|
|
63
81
|
- 不把 `main()` 编进库;程序只做装配和 I/O。
|
|
@@ -117,7 +135,7 @@ endif()
|
|
|
117
135
|
- 一个构建树只用一个 C++ 标准库实现。Clang 可以配 libstdc++(Linux 默认)或 libc++,但 `clang++`、`clang-tidy`、`clangd` 和链接进同一个程序的库必须一致。
|
|
118
136
|
- `-stdlib=libc++` 是编译和链接都要的选项,不要只加在编译选项上;也不要全局 `add_compile_options`,按 target 传,或由工具链文件统一设置。
|
|
119
137
|
- 混用 libstdc++ 与 libc++ 的典型症状是链接期大量未定义符号、`std::string` ABI 不匹配,或运行期崩溃;先查 `clang++ -v` 与 `ldd`,不要靠加 `-lstdc++` 掩盖。
|
|
120
|
-
- 项目想固定 libc++ 时,在 CMake 工具链文件或项目自己的编译选项里写清,不要只写在某个人的脚本里;`environment.json` 只保留 `host`、`build_host`、`compiler`
|
|
138
|
+
- 项目想固定 libc++ 时,在 CMake 工具链文件或项目自己的编译选项里写清,不要只写在某个人的脚本里;`environment.json` 只保留 `host`、`build_host`、`compiler`、`standard` 四个轴,不放 libc++。换机器后必须能复现同一组合。
|
|
121
139
|
- 本插件默认使用目标平台默认标准库;只有项目明确要求 libc++ 时才切换,并保证整套工具一致。
|
|
122
140
|
|
|
123
141
|
## 警告与静态检查
|
|
@@ -150,7 +168,7 @@ FetchContent_MakeAvailable(googletest)
|
|
|
150
168
|
|
|
151
169
|
| 模板 | 何时用 | 关键文件 |
|
|
152
170
|
| --- | --- | --- |
|
|
153
|
-
| `single` | 一个 `main.
|
|
171
|
+
| `single` | 一个 `main.cc`,辅助函数很少 | `CMakeLists.txt`, `src/main.cc` |
|
|
154
172
|
| `multi` | 多个源文件,一个程序 | `CMakeLists.txt`, `include/`, `src/` |
|
|
155
173
|
| `static-library` | 库需要被测试或第二个程序链接 | 根 + `greeting/` 子目录 |
|
|
156
174
|
| `shared-library` | 需要运行时加载/替换 | 同上,`SHARED` + RPATH |
|
|
@@ -160,7 +178,7 @@ FetchContent_MakeAvailable(googletest)
|
|
|
160
178
|
## 反模式
|
|
161
179
|
|
|
162
180
|
- 全局 `include_directories(include)`:所有 target 共享,边界消失。
|
|
163
|
-
- `file(GLOB sources "src/*.
|
|
181
|
+
- `file(GLOB sources "src/*.cc")`:新增文件不触发重新配置。
|
|
164
182
|
- 把库和 `main()` 编进同一个 target:测试无法只链接库。
|
|
165
183
|
- 在源码目录生成 `compile_commands.json` 或可执行文件。
|
|
166
184
|
- 同时设 `CMAKE_CXX_COMPILER` 和 `CXX` 环境变量,互相覆盖。
|