1

I have the following file overflow.c that I'm trying to debug through breakpoints in Visual Studio Code macOS:

#include <stdio.h>

int main(void) {
    int n = 1;
    for (int i = 0; i < 64; i++)
    {
        printf("%i\n", n);
        n = n * 2;
    }
    return 0;
}

I've built it by typing make overflow in the terminal, which returns

cc   overflow.o   -o overflow

And I can do ./overflow in the terminal to run it, which works. I have the C/C++ extension by Microsoft installed. My launch.json looks like the following:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C Run",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/overflow",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

When I debug using the "C Run" configuration, it runs my entire code without hitting any of my breakpoints (found here)

The "C Attach" is for attaching to an already-running app, which isn't applicable here. I've added the following to my PATH:

PATH="/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}"

My debug console after debugging "C Run" config loads bunch of symbols, returns output from my print statements, and ends with

The program '/Users/ahlam/Downloads/workspace/overflow' has exited with code 0 (0x00000000).

EDIT: I've also tried it with C++ and it has the same behavior. Made a hello.cpp, built using g++ hello.cpp and debugging just ran the entire code without hitting any breakpoints.

Any help is appreciated.

2
  • maybe start debug instead of run? Commented Oct 7, 2017 at 18:28
  • I went to the Debug menu, and clicked on the Green play button, hovering over it says, Start Debugging, so I think I'm debugging. Is there a different workflow you had in mind? Commented Oct 7, 2017 at 18:33

1 Answer 1

3

You need to generate source-level debug information, which you can do by using the -g flag in clang:

clang -g overflow.c -o overflow

Do that instead of make overflow. You'll see a folder called overflow.dSYM in your directory. Debugging should now work.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for this answer. I also want to state that the same thing is applicable to g++ when compiling in linux environments. if you use -g arg for g++ or clang, you'll get nice debugging in vscode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.