Visual Studio Code (VS code)設定C/C++的組態問題

Microsoft Windows 相關知識
回覆文章
dtchang
Site Admin
文章: 84
註冊時間: 2017-01-22, 16:54

Visual Studio Code (VS code)設定C/C++的組態問題

文章 dtchang » 2021-08-09, 12:44

launch.json: 一般放置於專案目錄下, 不可和不同程式語言混用同一個貝錄,以免發生載入錯亂
例如: python 檔和 C/C++ 檔需使用不同的 launch.json (或許可以同一個launch.json裡設定二種語言?)
--------------

代碼: 選擇全部

{
    // 使用 IntelliSense 以得知可用的屬性。
    // 暫留以檢視現有屬性的描述。
    // 如需詳細資訊,請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        


        {
            "name": "g++.exe - 建置及偵錯使用中的檔案",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:\\MinGW\\bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "啟用 gdb 的美化顯示",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 建置使用中檔案"
        }
    ]
}
task.json: 用於組建程式(build task)
使用 MinGW時需將 libgcc_s_dw2-1.dll 和 libstdc++-6.dll 複製到工作目錄下,否則無法執行 .exe 檔;
或者 修改 task.json 加入 compiler flag, 即加入 -static-libgcc 和 -static-libstdc++
----------

代碼: 選擇全部

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++.exe 建置使用中檔案",
			"command": "C:\\MinGW\\bin\\g++.exe",
			"args": [
				"-g",
				"-static-libgcc",
				"-static-libstdc++",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "C:\\MinGW\\bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "編譯器: C:\\MinGW\\bin\\g++.exe"
		}
	]
}

回覆文章