I'm not familiar with c compiler,I know how to use gcc or g++ in terminal
I have
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
and the output file a.out works fine
but if I debug with vscode or code-runner plugin error shows
/"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)
my launch.json and task.json look like this:
"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"
}
how to config this?
By default, the task only compiles the currently open file, so you need to change your prelaunch task to compile everything you need. You can create a custom task for this like the following:
{
"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"
}
And then update your launch.json to use the new task:
"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"
}
]