Cannot configure VSCode to compile C code on MacOS

68 Views Asked by At

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?

1

There are 1 best solutions below

0
Usman Mehmood On

Wrong Launch Configuration

{
    "configurations": [
        {
            "type": "lldb",
            "request": "attach",
            "name": "Attach",
            "program": "${workspaceFolder}/calcfreq.c" <----- WRONG
        },
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/calcfreq.c", <---- WRONG
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        },
        {
            "name": "(lldb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "${workspaceFolder}/calcfreq.c", <---- WRONG
            "MIMode": "lldb"
        }
    ]
}

Explanation

In the program field, you have mentioned the calcfreq.c file.

If you are a new programmer, it does seem that the .c file is the "program" but it's the source code. The program would be the executable file that is compiled from the .c file.

So you can use GCC or some other compiler to compile it.

gcc calcfreq.c -o output_file

And then set the program in the json file as this.

"program": "${workspaceFolder}/output_file",

Sample Launch Config

Here's an example launch.json that I use.

{
    "configurations": [
        {
            "name"            : "c-toolkit launch",
            "type"            : "cppdbg",
            "request"         : "launch",
            "program"         : "your_exe_here", <--- PATH TO YOUR EXE
            "args"            : [],
            "stopAtEntry"     : false,
            "cwd"             : "${workspaceRoot}",
            "environment"     : [],
            "externalConsole" : false,
            "MIMode"          : "lldb",
        }
    ]
}

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. :)