天天看點

vs code,從編輯器到編譯器

鏂囩珷鐩綍

    • 鎵撳紑 vsc 涔嬪墠
    • 閫夋嫨鎻掍歡
    • json 鏂囦歡
        • `c_cpp_properties.json`
        • `launch.json`
            • win 鐗?/li>
            • Mac 鐗?/li>
        • `tasks.json`
            • win
            • Mac
    • 涓€浜涜緟鍔╂€х殑璁劇疆
        • 濂藉悍鐨勬墿灞?/li>
        • `settings.json`

寰堝浜洪兘鎶?vs code(vsc) 浣滀負涓€涓ソ搴風殑缂栬緫鍣ㄦ潵鐢紙yysy纭疄姣攙im銆乪macs濂界湅澶氫簡锛変笉杩囦竴涓?寮€婧愮殑銆佽法骞沖彴鐨?鐨剉s code锛屾洿搴旇鍙戞尌鍑衡€滀笉鏄疘DE浼樹簬IDE鈥濈殑鍔涢噺銆?

涓嬮潰浠?cpp 寮€鍙戠幆澧冧負渚嬶紝浠嬬粛涓€涓?vsc 鐨勯厤缃繃绋嬨€?

鎵撳紑 vsc 涔嬪墠

棣栧厛鏄幆澧冨彉閲忥紝win 涓嬫槸鐢?

MinGW-w64 - for 32 and 64 bit Windows

锛岄€夋柊鐗堟湰鐨?

x86_64-posix-seh

銆?

macOS 鐩稿绠€鍗曪紝Xcode 澶ф硶瀹屼簨路路路

閫夋嫨鎻掍歡

vsc 鐨?extension 鍟嗗煄鐞崇悈婊$洰鐨勬彃浠訛紝涓嶈繃鍏跺疄灏變袱涓凍鐭c€?

vs code,從編輯器到編譯器
vs code,從編輯器到編譯器

鏈変簡杩欎袱涓彃浠訛紝灏卞彲浠ュ噯澶囩紪璇戣繍琛屼簡锛?

json 鏂囦歡

鏈?3 涓?

json

鏂囦歡鏄笌缂栬瘧杩愯鐩稿叧鐨勶紝锛堝熀鏈笂澶嶅埗绮樿創灏辮锛夊垎鍒槸锛?

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ], // 杩欎釜璺緞鏀規垚鑷繁鐨勶紙浣嗗ソ鍍忎笉鏄緢閲嶈锛熸垜鐜闆湪娌℃湁Xcode浜嗕絾鏄繕鑳芥甯哥紪璇?            "compilerPath": "/usr/bin/clang", // 杩欎釜寰堥噸瑕侊紝鏇挎崲鎴愯嚜宸辯殑
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
           

濡傛灉浣犱笉鐭ラ亾浣犵殑

clang

鍦ㄥ摢閲岋紝鍙互

$ whereis clang
           

win 鏄綘鑷繁涓嬭澆鐨?mingw 鐨勮礬寰勩€傘€傘€?

launch.json

win 鐗?

{
    "version": "0.2.0",
    "configurations": [{
        "name": "(gdb) Launch", // 閰嶇疆鍚嶇О锛屽皢浼氬湪鍚姩閰嶇疆鐨勪笅鎷夎彍鍗曚腑鏄劇ず
        "type": "cppdbg", // 閰嶇疆绫誨瀷锛宑ppdbg瀵瑰簲cpptools鎻愪緵鐨勮皟璇曞姛鑳斤紱鍙互璁や負姝ゅ鍙兘鏄痗ppdbg
        "request": "launch", // 璇鋒眰閰嶇疆绫誨瀷锛屽彲浠ヤ負launch锛堝惎鍔級鎴朼ttach锛堥檮鍔狅級
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 灏嗚杩涜璋冭瘯鐨勭▼搴忕殑璺緞
        "args": [], // 绋嬪簭璋冭瘯鏃朵紶閫掔粰绋嬪簭鐨勫懡浠よ鍙傛暟锛屼竴鑸涓虹┖鍗沖彲
        "stopAtEntry": false, // 璁句負true鏃剁▼搴忓皢鏆傚仠鍦ㄧ▼搴忓叆鍙e锛岀浉褰撲簬鍦╩ain涓婃墦鏂偣
        "cwd": "${workspaceFolder}", // 璋冭瘯绋嬪簭鏃剁殑宸ヤ綔鐩綍锛屾涓哄伐浣滃尯鏂囦歡澶癸紱鏀規垚${fileDirname}鍙彉涓烘枃浠舵墍鍦ㄧ洰褰?        "environment": [], // 鐜鍙橀噺
        "externalConsole": true, // 涓簍rue鏃朵嬌鐢ㄥ崟鐙殑cmd绐楀彛锛屼笌鍏跺畠IDE涓€鑷達紱18骞?0鏈堝悗璁句負false鍙皟鐢╒SC鍐呯疆缁堢
        "internalConsoleOptions": "neverOpen", // 濡傛灉涓嶈涓簄everOpen锛岃皟璇曟椂浼氳煩鍒扳€滆皟璇曟帶鍒跺彴鈥濋€夐」鍗★紝浣犲簲璇ヤ笉闇€瑕佸gdb鎵嬪姩杈撳懡浠ゅ惂锛?        "MIMode": "gdb", // 鎸囧畾杩炴帴鐨勮皟璇曞櫒锛屽彲浠ヤ負gdb鎴杔ldb銆備絾鎴戞病璇曡繃lldb
        "miDebuggerPath": "gdb.exe", // 璋冭瘯鍣ㄨ礬寰勶紝Windows涓嬪悗缂€涓嶈兘鐪佺暐锛孡inux涓嬪垯涓嶈
        "setupCommands": [
            { // 妯℃澘鑷甫锛屽ソ鍍忓彲浠ユ洿濂藉湴鏄劇ずSTL瀹瑰櫒鐨勫唴瀹癸紝鍏蜂綋浣滅敤鑷Google
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": false
            }
        ],
        "preLaunchTask": "Compile" // 璋冭瘯浼氳瘽寮€濮嬪墠鎵ц鐨勪換鍔★紝涓€鑸負缂栬瘧绋嬪簭銆備笌tasks.json鐨刲abel鐩稿搴?    }]
}
           

Mac 鐗?

{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"name": "(lldb) 鍚姩",
			"type": "cppdbg",
			"request": "launch",
			"program": "${fileDirname}/${fileBasenameNoExtension}",
			"args": [],
			"stopAtEntry": false, // 璁句負true鏃剁▼搴忓皢鏆傚仠鍦ㄧ▼搴忓叆鍙e锛岀浉褰撲簬鍦╩ain涓婃墦鏂偣
			"cwd": "${workspaceFolder}",
			"environment": [],
			"externalConsole": false,
			"MIMode": "lldb",
			"miDebuggerPath": "/usr/bin/clang",
			"setupCommands": [
				{
					"description": "Enable pretty-printing for gdb",
					"text": "-enable-pretty-printing",
					"ignoreFailures": true
				}
			],
			// "preLaunchTask": "Compile with g++",
		}
	]
}
           

tasks.json

win

{
    "version": "2.0.0",
    "tasks": [{
        "label": "Compile", // 浠誨姟鍚嶇О锛屼笌launch.json鐨刾reLaunchTask鐩稿搴?        "command": "gcc",   // 瑕佷嬌鐢ㄧ殑缂栬瘧鍣紝C++鐢╣++
        "args": [
            "${file}",
            "-o",    // 鎸囧畾杈撳嚭鏂囦歡鍚嶏紝涓嶅姞璇ュ弬鏁闆垯榛樿杈撳嚭a.exe锛孡inux涓嬮粯璁.out
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "-g",    // 鐢熸垚鍜岃皟璇曟湁鍏崇殑淇℃伅
            "-Wall", // 寮€鍚澶栬鍛?            "-static-libgcc",     // 闈欐€侀摼鎺ibgcc锛屼竴鑸兘浼氬姞涓?            "-fexec-charset=GBK", // 鐢熸垚鐨勭▼搴忎嬌鐢℅BK缂栫爜锛屼笉鍔犺繖涓€鏉′細瀵艱嚧Win涓嬭緭鍑轟腑鏂囦貢鐮?            // "-std=c11", // C++鏈€鏂版爣鍑嗕負c++17锛屾垨鏍規嵁鑷繁鐨勯渶瑕佽繘琛屼慨鏀?        ], // 缂栬瘧鐨勫懡浠わ紝鍏跺疄鐩稿綋浜嶸SC甯綘鍦ㄧ粓绔腑杈撲簡杩欎簺涓滆タ
        "type": "process", // process鏄痸sc鎶婇瀹氫箟鍙橀噺鍜岃漿涔夎В鏋愬悗鐩存帴鍏ㄩ儴浼犵粰command锛泂hell鐩稿綋浜庡厛鎵撳紑shell鍐嶈緭鍏ュ懡浠わ紝鎵€浠rgs杩樹細缁忚繃shell鍐嶈В鏋愪竴閬?        "group": {
            "kind": "build",
            "isDefault": true // 涓嶄負true鏃禼trl shift B灏辮鎵嬪姩閫夋嫨浜?        },
        "presentation": {
            "echo": true,
            "reveal": "always", // 鎵ц浠誨姟鏃舵槸鍚﹁煩杞埌缁堢闈㈡澘锛屽彲浠ヤ負always锛宻ilent锛宯ever銆傚叿浣撳弬瑙乂SC鐨勬枃妗?            "focus": false,     // 璁句負true鍚庡彲浠ヤ嬌鎵цtask鏃剁劍鐐矽仛闆嗗湪缁堢锛屼絾瀵圭紪璇慍/C++鏉ヨ锛岃涓簍rue娌℃湁鎰忎箟
            "panel": "shared"   // 涓嶅悓鐨勬枃浠剁殑缂栬瘧淇℃伅鍏變韓涓€涓粓绔潰鏉?        },
        // "problemMatcher":"$gcc" // 姝ら€夐」鍙互鎹曟崏缂栬瘧鏃剁粓绔噷鐨勬姤閿欎俊鎭紱浣嗗洜涓烘湁Lint锛屽啀寮€杩欎釜鍙兘鏈夊弻閲嶆姤閿?    }]
}
           

Mac

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"label": "build",
			"type": "shell",
			"command": "msbuild",
			"args": [
				// Ask msbuild to generate full paths for file names.
				"/property:GenerateFullPaths=true",
				"/t:build",
				// Do not generate summary otherwise it leads to duplicate errors in Problems panel
				"/consoleloggerparameters:NoSummary",
				// "${file}",
				// "-g", // 鐢熸垚鍜岃皟璇曟湁鍏崇殑淇℃伅
				// "-Wall", // 寮€鍚澶栬鍛?				"-o", // 鎸囧畾杈撳嚭鏂囦歡鍚嶏紝涓嶅姞璇ュ弬鏁闆垯榛樿杈撳嚭a.exe锛孡inux涓嬮粯璁.out
				"${fileDirname}/${fileBasenameNoExtension}.out",
				"-static-libgcc" // 闈欐€侀摼鎺ibgcc锛屼竴鑸兘浼氬姞涓?			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"presentation": {
				// Reveal the output only if unrecognized errors occur.
				"reveal": "silent"
			},
			// Use the standard MS compiler pattern to detect errors, warnings and infos
			"problemMatcher": "$msCompile"
		}
	]
}
           

涓€浜涜緟鍔╂€х殑璁劇疆

濂藉悍鐨勬墿灞?/h4>

杩欎釜浼氳浣犳湁鏄庝寒鐨勬嫭鍙?

vs code,從編輯器到編譯器
杩欎釜鏄竴涓瘮杈冪渷鐪肩潧鐨勪富棰?
vs code,從編輯器到編譯器
杩樻湁灏辨槸锛?

settings.json

  1. 鍦ㄥ乏涓嬭鎵捐缃?
    vs code,從編輯器到編譯器
  2. 鍙充笂瑙掓墦寮€ json锛堝氨鏄笁瑙掑艦鍙寵竟閭d釜鎸夐挳锛?
    vs code,從編輯器到編譯器
  3. 鐒跺悗閫夋嫨浣犻渶瑕佺殑閮ㄥ垎杈撳叆杩涘幓
{


    "files.defaultLanguage": "cpp", // ctrl+N鏂闆緩鏂囦歡鍚庨粯璁ょ殑璇█
    "files.trimTrailingWhitespace": true, // 淇濆瓨鏃訛紝鍒犻櫎姣忎竴琛屾湯灏劇殑绌烘牸
    "files.insertFinalNewline": true,// 淇濆瓨鍚庢枃浠舵渶鏈熬鍔犱竴鏁磋绌鴻锛孡inux涓嬬殑涔犳儻

    "C_Cpp.updateChannel": "Insiders",
    "http.proxySupport": "off",
    "vsicons.dontShowNewVersionMessage": true,
    "window.zoomLevel": -1,
    "workbench.iconTheme": "vscode-icons",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "workbench.colorTheme": "One Dark Pro",

    // editor
    "editor.fontSize": 21,
    "editor.largeFileOptimizations": false,
    "editor.dragAndDrop": true,
    "editor.formatOnType": true, // 杈撳叆鏃跺氨杩涜鏍煎紡鍖栵紝榛樿瑙﹀彂瀛楃杈冨皯锛屽垎鍙峰彲浠ヨЕ鍙?    "editor.snippetSuggestions": "top", // snippets浠g爜浼樺厛鏄劇ず琛ュ叏
    "editor.suggestSelection": "first",
    "editor.insertSpaces": false,
    "editor.cursorStyle": "line-thin",
    "editor.formatOnPaste": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.fontLigatures": true, // 杩炰綋瀛楋紝鏁堟灉涓嶅お濂藉艦瀹癸紝瑙?https://typeof.net/Iosevka 鏈€鍚庝竴閮ㄥ垎
    "editor.cursorSmoothCaretAnimation": true, // 绉誨姩鍏夋爣鏃跺彉寰楀鈎婊?    "editor.smoothScrolling": true, // 婊氬姩骞蟲粦锛屼笉杩囨晥鏋滃緢寰急
    // "editor.fontFamily": "Menlo, Monaco, 'Courier New', monospace",
    "editor.fontFamily": "Consolas, 'Courier New', monospace",
    "editor.fontWeight": "normal",

    // code-runner
    "code-runner.runInTerminal": true, // 璁劇疆鎴恌alse浼氬湪鈥滆緭鍑衡€濅腑杈撳嚭锛屾棤娉曡緭鍏?    "code-runner.preserveFocus": true, // 鑻ヤ負false锛宺un code鍚庡厜鏍囦細鑱氱劍鍒扮粓绔笂銆傚鏋滈渶瑕侀绻佽緭鍏ユ暟鎹彲璁句負false
    "code-runner.clearPreviousOutput": false,
    "terminal.integrated.cursorBlinking": true,
    "terminal.integrated.cursorStyle": "line",
    "terminal.integrated.fontSize": 20,
    // "code-runner.executorMap": {
    //     // "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.out' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
    //     // "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.out' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
    //     "c": "cd $dir; clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11; ./$fileNameWithoutExt",
    //     "cpp": "cd $dir; clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c++17; ./$fileNameWithoutExt"
    // }, // 璁劇疆code runner鐨勫懡浠よ

    // python
    "python.pythonPath": "/usr/local/bin/python3",
    "diffEditor.ignoreTrimWhitespace": true,
    "debug.allowBreakpointsEverywhere": true,

    // latex
    "latex-workshop.latex.recipes": [
        {
            "name": "xelatex",
            "tools": [
                "xelatex"
            ]
        },
        {
            "name": "latexmk",
            "tools": [
                "latexmk"
            ]
        },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        }
    ],
    "latex-workshop.latex.tools": [
        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-pdf",
                "%DOC%"
            ]
        },
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOC%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOC%"
            ]
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ]
        }
    ],
    "latex-workshop.view.pdf.viewer": "tab",
    "latex-workshop.latex.clean.fileTypes": [
        "*.aux",
        "*.bbl",
        "*.blg",
        "*.idx",
        "*.ind",
        "*.lof",
        "*.lot",
        "*.out",
        "*.toc",
        "*.acn",
        "*.acr",
        "*.alg",
        "*.glg",
        "*.glo",
        "*.gls",
        "*.ist",
        "*.fls",
        "*.log",
        "*.fdb_latexmk"
    ],
}
           
濂戒簡鍒版缁撴潫锛屼韓鍙椾綘鐨?visual studio code 鍚э紒
鍝﹀浜嗭紝濡傛灉鎯蟲洿娣變簡瑙o紝鍙互鍙傝€冭繖鏉?