I am trying to set up VS Code on macOS to compile and debug C code.
My environment is:
- OS: macOS
14.2 - VS Code:
1.86.0 - Project Folder:
~/Projects/CProjects - C header files:
~/Projects/CProjects/includes - C Program file:
hello.c - Path includes:
/Users/Dave/Projects/CProjects/includes
This is the hello.c source, located in /Users/dave/Projects/CProjects:
#include <stdio.h>
int main() {
printf("Hello");
}
This is my launch.json file:
{
"configurations": [
{
"type": "lldb",
"request": "attach",
"name": "Attach",
"program": "${workspaceFolder}/calcfreq.c"
},
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/calcfreq.c",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
},
{
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/calcfreq.c",
"MIMode": "lldb"
}
]
}
When I attempt to use the debugger, I get an error that says:
> Unable to start debugging. Program path
> '/Users/dave/Projects/CProjects/calcfreq.c' is missing or invalid.
>
> LLDB failed with message: Command 'file-exec-and-symbols'. Target
> binary '/Users/dave/Projects/CProjects/calcfreq.c' is invalid. error:
> '/Users/dave/Projects/CProjects/calcfreq.c' doesn't contain any 'host'
> platform architectures: arm64, armv7, armv7f, armv7k, armv7s, armv7m,
> armv7em, armv6m, armv6, armv5, armv4, arm, thumbv7, thumbv7k,
> thumbv7s, thumbv7f, thumbv7m, thumbv7em, thumbv6m, thumbv6, thumbv5,
> thumbv4t, thumb, x86_64, x86_64, arm64, arm64e, arm64, arm64e
>
> This may occur if the process's executable was changed after the
> process was started, such as when installing an update. Try
> re-launching the application or restarting the machine.
How can I fix the launch.json file and make the debugger work?
Wrong Launch Configuration
Explanation
In the
programfield, you have mentioned thecalcfreq.cfile.If you are a new programmer, it does seem that the
.cfile is the "program" but it's the source code. The program would be the executable file that is compiled from the.cfile.So you can use GCC or some other compiler to compile it.
And then set the program in the json file as this.
Sample Launch Config
Here's an example
launch.jsonthat I use.You can also try my VSCode extension: C Toolkit to create multi-file C projects with all the files and configurations correctly set up, and also build, run and debug them. It works perfectly on MacOS, Windows and Linux, and more importantly it automatically installs all the required development tools if any are missing. :)