gcc -g 是一個常用的編譯器命令伯顶,用于在編譯過程中生成調(diào)試信息李滴。選項 "-g" 會告訴編譯器在生成的可執(zhí)行文件中包含調(diào)試符號觉义,以便進行調(diào)試设拟。
通過在終端中運行 "gcc -g <source_file.c> -o <output_file>" 命令慨仿,你可以編譯一個 C 源文件,并生成一個帶有調(diào)試信息的可執(zhí)行文件纳胧。請將 "<source_file.c>" 替換為你的源文件名镰吆,將 "<output_file>" 替換為你想要生成的可執(zhí)行文件名。
例如跑慕,如果你有一個名為 "main.c" 的源文件万皿,想要生成一個名為 "my_program" 的可執(zhí)行文件,可以使用以下命令:
gcc -g main.c -o my_program
這將在當(dāng)前目錄下生成一個名為 "my_program" 的可執(zhí)行文件核行,并包含調(diào)試信息牢硅,以便你可以使用調(diào)試器進行調(diào)試。
坑:
1钮科、無法調(diào)試是因為這個-g參數(shù)沒有添加
2唤衫、"targetArchitecture": "x86_64"可能不同平臺需要設(shè)置
tasks.json配置
{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"command": "mingw32-make",
"args": [],
"type": "shell",
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
},
]
}
launch.json配置
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名稱,將會在啟動配置的下拉菜單中顯示
"type": "cppdbg", // 配置類型绵脯,這里只能為cppdbg
"request": "launch", // 請求配置類型佳励,可以為launch(啟動)或attach(附加)
"targetArchitecture": "x86_64",
"program": "${workspaceFolder}/testdebug",// 將要進行調(diào)試的程序的路徑
"args": [], // 程序調(diào)試時傳遞給程序的命令行參數(shù),一般設(shè)為空即可
"stopAtEntry": false, // 設(shè)為true時程序?qū)和T诔绦蛉肟谔幥欤话阍O(shè)置為false
"cwd": "${workspaceFolder}", // 調(diào)試程序時的工作目錄赃承,一般為${workspaceFolder}即代碼所在目錄
"environment": [],
"externalConsole": false, // 調(diào)試時是否顯示控制臺窗口,一般設(shè)置為true顯示控制臺
"MIMode": "gdb",
"miDebuggerPath": "D:\\tools\\x86_64-12.2.0-release-win32-seh-msvcrt-rt_v10-rev2\\mingw64\\bin\\gdb.exe", // miDebugger的路徑悴侵,注意這里要與MinGw的路徑對應(yīng)
"preLaunchTask": "test", // 調(diào)試會話開始前執(zhí)行的任務(wù)瞧剖,一般為編譯程序,c++為g++, c為gcc
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "將反匯編風(fēng)格設(shè)置為 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"logging": {
"moduleLoad": true,
"programOutput": true,
"trace": true,
"traceResponse": true,
"engineLogging": true
},
}
]
}
C文件
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char *const argv[]){
printf(" -h, --help help\n");
}
makefile文件[去掉-g將無法調(diào)試]
CC=gcc
all: testdebug.exe
testdebug.exe:
${CC} -g testdebug.c -o testdebug.exe #-g去掉將無法調(diào)試可免,切記
clean:
rm -f testdebug.exe
注意不同系統(tǒng)可能文件格式不一樣
- WIN系統(tǒng)和LINUX系統(tǒng)的CRLF\LF
- 對環(huán)境變量可以在launch.json中配置
{
"configurations": [
{
......
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [{
"name": "PYTHONHOME",
"value": "Python路徑"
}],
......
}
- tasks.json中使用gitbash,例如:
sh build_msys_release.sh
"tasks": [
{
"type": "shell",
"label": "test",
"command": "C:\\Program Files\\Git\\usr\\bin\\bash.exe",
"args": [
"-c",
"\"/d/build_msys_release.sh\""
],
"options": {
"cwd": "${workspaceFolder}",
"env": {
"PATH": "C:\\Program Files\\Git\\usr\\bin;${env:PATH}"
}
},
}
],
"version": "2.0.0"