preface
Get up early in the morning to debug CS2 C source code. However, the textbook source code downloaded from the official website is not well formatted and is very difficult to read. So try to find a tool about formatting and debugging c language in vscode on the Internet. The following tools were finally selected and implemented.
Environment of 0.
- OS: MacOS
- IDE: vscode
- Format plug-in: clang-format
Step 1.
1.1 BREW Install clang-format
# brew install clang-format
Copy the code
After the installation is complete, check the location of the clang-format executable. Needed for use by vscode plug-ins.
➜ bin PWD /usr/local/cellar /clang-format/10.0.0/bin ➜ bin ls clang-format git-clang-formatCopy the code
Brew is installed in /usr/local/cellar by default.
1.2 Vscode Install the clang-format plug-in
The Readme documentation for this plug-in states that the clang-format execution directory needs to be specified.
Specifying the location of clang-format
This extension will attempt to find clang-format on your PATH. Alternatively, the clang-format executable can be specified in your vscode settings.json file:
{
"clang-format.executable": "/absolute/path/to/clang-format"
}
Copy the code
At this point, you can configure the path obtained in step 1.1 into vscode’s plug-in.
1.3 Verification
int main(int argc, char *argv[])
{
int val = 12345;
if (argc > 1) {
if (argc > 1) {
val = strtol(argv[1].NULL.0);
}
printf("calling test_show_bytes\n");
test_show_bytes(val);
} else {
printf("calling show_twocomp\n");
show_twocomp();
printf("Calling simple_show_a\n");
simple_show_a();
printf("Calling simple_show_b\n");
simple_show_b();
printf("Calling float_eg\n");
float_eg();
printf("Calling string_ueg\n");
string_ueg();
printf("Calling string_leg\n");
string_leg();
}
return 0;
}
Copy the code
Select a c file whose format is not readable and use the shortcut key Shift + option + F to format the file to obtain the following format.
int main(int argc, char *argv[]) {
int val = 12345;
if (argc > 1) {
if (argc > 1) {
val = strtol(argv[1].NULL.0);
}
printf("calling test_show_bytes\n");
test_show_bytes(val);
}
else {
printf("calling show_twocomp\n");
show_twocomp();
printf("Calling simple_show_a\n");
simple_show_a();
printf("Calling simple_show_b\n");
simple_show_b();
printf("Calling float_eg\n");
float_eg();
printf("Calling string_ueg\n");
string_ueg();
printf("Calling string_leg\n");
string_leg();
}
return 0;
}
Copy the code
2 Customized Configuration
Clang-format also allows developers to customize their own formats
2.1 Add a.clang-format file to the project directory
2.2 Adding Configurations
BasedOnStyle: LLVM
AccessModifierOffset: -4
IndentWidth: 4
ContinuationIndentWidth: 4
TabWidth: 4
#UseTab: ForIndentation
UseTab: Never
AllowShortIfStatementsOnASingleLine: false
ColumnLimit: 140
BreakBeforeBraces: Attach
AllowShortFunctionsOnASingleLine: Empty
BreakBeforeBraces: Custom
BraceWrapping:
BeforeElse: true
Copy the code
2.3 validation
Repeat Step 1.3. I felt more comfortable using the custom configuration myself.
reference
- Obtain the Homebrew installation directory in Mac
- Install clang-format on Mac OSX
- Macbook vscode configates clang-format and sets automatic formatting code when saving
- Clang-format Official document