如何使用 vscode 在 c 中调试 'extern'
how to debug 'extern' in c with vscode
我不熟悉c编译器,我知道如何在终端中使用gcc或g++
我有
main.c
#include <stdio.h>
int count;
extern void write_extern();
int main()
{
count = 5;
write_extern();
}
support.c
#include <stdio.h>
extern int count;
void write_extern(void)
{
printf("count is %d\n", count);
}
gcc main.c support.c
并且输出文件 a.out 工作正常
但如果我使用 vscode 或 code-runner 插件进行调试
错误显示
/"main
Undefined symbols for architecture x86_64:
"_write_extern", referenced from:
_main in main-217186.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的 launch.json 和 task.json 看起来像这样:
"configurations": [
{
"name": "clang build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang build active file"
}
]
{
"tasks": [
{
"type": "shell",
"label": "clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
}
],
"version": "2.0.0"
}
如何配置?
默认情况下,该任务仅编译当前打开的文件,因此您需要更改预启动任务以编译您需要的所有内容。您可以为此创建自定义任务,如下所示:
{
"tasks": [
{
"type": "shell",
"label": "clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
},
{
"type": "shell",
"label": "clang build custom",
"command": "/usr/bin/clang",
"args": [
"-g",
"${fileDirname}/main.c",
"${fileDirname}/support.c",
"-o",
"${fileDirname}/main"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
],
"version": "2.0.0"
}
然后更新您的 launch.json 以使用新任务:
"configurations": [
{
"name": "clang build and debug custom project",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang build custom"
}
]
我不熟悉c编译器,我知道如何在终端中使用gcc或g++
我有
main.c
#include <stdio.h>
int count;
extern void write_extern();
int main()
{
count = 5;
write_extern();
}
support.c
#include <stdio.h>
extern int count;
void write_extern(void)
{
printf("count is %d\n", count);
}
gcc main.c support.c
并且输出文件 a.out 工作正常
但如果我使用 vscode 或 code-runner 插件进行调试 错误显示
/"main Undefined symbols for architecture x86_64: "_write_extern", referenced from: _main in main-217186.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的 launch.json 和 task.json 看起来像这样:
"configurations": [
{
"name": "clang build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang build active file"
}
]
{
"tasks": [
{
"type": "shell",
"label": "clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
}
],
"version": "2.0.0"
}
如何配置?
默认情况下,该任务仅编译当前打开的文件,因此您需要更改预启动任务以编译您需要的所有内容。您可以为此创建自定义任务,如下所示:
{
"tasks": [
{
"type": "shell",
"label": "clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
},
{
"type": "shell",
"label": "clang build custom",
"command": "/usr/bin/clang",
"args": [
"-g",
"${fileDirname}/main.c",
"${fileDirname}/support.c",
"-o",
"${fileDirname}/main"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
],
"version": "2.0.0"
}
然后更新您的 launch.json 以使用新任务:
"configurations": [
{
"name": "clang build and debug custom project",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang build custom"
}
]