VSCode is just a kind of visualizer. When you want to debug C program(from3) with vscode, you should make a debuggable execution file with your own build system yourself(참고2:using g flag), and let vscode know the execution file.

.
├── Makefile
├── other.c
├── other.h
├── main.c
└── main.o

Let’s see and end-to-end example. First, create a Makefile to make a debuggable & executable output file.

Code(from1)

CC = gcc
CFLAGS = -g

main: main.o other.o
	$(CC) ${CFLAGS} -o main main.o other.o

main.o: main.c main.h
	$(CC) ${CFLAGS} -c main.c

other.o: other.c other.h
	$(CC) ${CFLAGS} -c other.c

clean:
	rm -f main main.o other.o

The debuggable output file name is main in this example. Next, select main file (”program”: ${workspaceFolder}/main) to start debugging when you click the debug button in the VSCode. I’m using macbook, so my debugger backend is lldb.

CFLAGS = -g

Note that the debuggable file had to be built with the -g compiler flag(참고2). That’s all you need.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "My debug",
            "program": "${workspaceFolder}/main",
            "args": [],
            "cwd": "${workspaceFolder}",
        }
    ]
}

<aside> 💡 Change your debugger to proper type (lldb or something).

</aside>

For your convenience, it’s common to make a tasks.json file. It can be just working as a shortcut instead of typing a boring same command in your terminal. The task is just typing a shell command to run the Makefile. The name of the task is "run make using Makefile" as you can see below.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "run make using Makefile",
            "type": "shell",
            "command": "make",
        }
    ]
}

You can link the task on the debug button as below. It means when you click the debug button, the make task will be start automatically so you can run debugging mode with a up-to-dated code.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "My debug",
            "program": "${workspaceFolder}/main",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "run make using Makefile",
        }
    ]
}
.
├── .vscode
│   ├── launch.json
│   └── tasks.json
├── Makefile
├── other.c
├── other.h
├── main.c
└── main.o

parse me : 언젠가 이 글에 쓰이면 좋을 것 같은 재료들.

  1. None

from : 과거의 어떤 생각이 이 생각을 만들었는가?