环境
os:macOS Catalina
clang(clang -v 结果):
Apple clang version 11.0.0 (clang-1100.0.33.16)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
lldb(lldb -v 结果):
lldb-1100.0.30.11
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
vscode插件安装
-
C/C++
author:Microsoft
-
C/C++ Clang Command Adapter
author:Yasuaki MITANI
-
CodeLLDB
author: Vadim Chugunov
在macOS Catalina这个版本中,取消了对lldb的支持。所以如果需要debug,必须安装这个插件。
task.json
task.json配置C程序构建任务
例如:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "common-build",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}/build/${fileBasenameNoExtension}",
],
"options": {
"cwd": "/usr/bin"
}
}
]
}
label选项指定当前任务的代号,这个必须和launch.json中的preLaunchTask选项一致
command选项指定编译的程序,在macOS中使用/usr/bin/clang命令进行编译
args选项指定command命令执行时传递的参数,$ {xxx} 是vscode提供的变量,$ {file} 表示当前文件绝对路径,$ {workspaceFolder} 表示当前工作空间目录绝对路径,$ {fileBasenameNoExtension}表示当前文件不带扩展名的文件名
launch.json
launch.json配置C程序debug
例如:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "common-debug",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "common-build"
}
]
}
name:当前debug任务的代号
type:在成功安装CodeLLDB插件后,才可以使用"lldb"这个type
program:需要debug的程序的绝对路径,必须和task.json中args里的-o参数指定的路径一致
preLaunchTask:必须和task.json的label一致
开始debug
选择要debug的C源文件(必须)
Run -> Start Debugging 或 F5
下面有一个简单的读取标准输入,并将标准输入的字符串反转的程序。我们用它来进行debug演示
创建reverse.c文件,内容是下面的代码,保存。
#include<stdio.h>
#include<stdlib.h>
#define LINE_MAX 100
int getLine(char line[], int maxLine);
void reverse(char line[]);
int main() {
char line[LINE_MAX];
while (getLine(line, LINE_MAX) != 0) {
printf("origin string: %s\n", line);
reverse(line);
printf("reverse string: %s\n\n", line);
}
return EXIT_SUCCESS;
}
int getLine(char line[], int maxLen) {
char c;
int strLen = 0;
printf("input: ");
while ((c = getchar()) != EOF) {
if (c == '\n'){
line[strLen] = '\0';
break;
} else if (strLen >= (maxLen - 1)){
continue;
}else {
line[strLen++] = c;
}
}
return strLen;
}
void reverse(char line[]) {
int i = 0, j = 0;
char t;
while(line[j+1] != '\0') {
j++;
}
while(i < j) {
t = line[i];
line[i] = line[j];
line[j] = t;
i++;
j--;
}
}

4256

被折叠的 条评论
为什么被折叠?



