無廢話--Mac OS, VS Code 搭建c/c++基本開發環境


無廢話,直接上步驟。

1) 安裝 xcode。

打開App Store,搜索xcode,進行下載安裝。

image

2)執行命令:

xcode-select --install

image

安裝命令行工具。

3)安裝VS Code

https://code.visualstudio.com/

image

4) 打開vs code。打開左側擴展欄,

image

搜索“c++”。

cpp-extension.png

安裝該擴展。

5)打開一個保護.cpp文件的文件夾(沒有就自己創建)

“command+shift+p”打開命令行工具窗口,輸入或者選擇“

Edit Configurations”命令。

QQ20181101-152136@2x.png

此時會在當前工作空間目錄生成.vscode配置目錄,同時在配置目錄會生成一個c_cpp_properties.json文件。

配置include目錄:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/Library/Developer/CommandLineTools/usr/include/c++/v1",
            "/usr/local/include",
            "/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include",
            "/Library/Developer/CommandLineTools/usr/include",
            "/usr/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

6)

“command+shift+p”打開命令行工具窗口,輸入或者選擇“Tasks: Configure Task”

QQ20181101-161752@2x.png

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "c++",
            "command": "clang++",
            "type": "shell",
            "args": [
                "./c++/hello.cpp",
                "-std=c++11",
                "-g"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        }
    ]
}

7)配置launch.json。

“command+shift+p”打開命令行工具窗口,輸入或者

選擇Debug: Open launch.json命令。

image

修改內容如下:

{
    // 使用 IntelliSense 了解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "c/c++ Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/a.out",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "lldb",
        "preLaunchTask":"c++"
        }
    ]
}

8)開啟調試

image

image

中途可能會提醒控制終端,需要賦予權限,允許即可。

image

最后如果終端有類似的提示,輸入回車結束終端調用。

可能遇到的問題

可能遇到“xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun”

這樣的報錯,終端輸入命令:

sudo xcode-select --switch /Applications/Xcode.app

更多精彩內容,關注微信訂閱號“玄魂工作室”(xuanhun521)

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM