Visual Studio Code &C++ on macOS

一、预备知识

1、理解mac环境的开发环境

名称在开发环境中作用举例
文本编辑器编写代码sublime,atom,visual stdio code,nodepad++,vi/vim,emacs
编译器文本编辑器写的代码翻译成机器代码 
链接器代码经编译二进制机器代码后,与系统提供的二进制库进行组合,生成一个可执行文件通常编译器和连接器一起的。vc/vc++,gcc/g++,clang+llvm
项目管理工具管理程序项目的程序,比如源文件放哪儿,用什么语法级别,链接器可用的链接库放在哪儿等cmake,qmake,make
集成开发环境(ide)把上面所有类型的工具选一些出来组合,然后再自己增加一些语法检查等人性化的功能,直接的效果就是有这个工具就能直接开发eclipse,visual stdio,qt creater,clion,code block,dev c++

 

 

 

 

 

 

 

 

 

2、mac上一般采用Xcode,是一个完整的集成开发环境(ide),但是比较重

3、vscode则是个比较友好的编辑器,可以通过插件扩展,搭建成简单的c++开发环境

 

二、使用vscode搭建C++开发环境

1.下载安装vscode

Install Visual Studio Code and follow the setup instructions in Visual Studio Code on macOS.

2.安装C++插件: C++ extension for VS Code.

3.设置用户环境变量:

方法一:Open the Command Palette (⇧⌘P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.

方法二:命令行中添加

cat << EOF >> ~/.bash_profile

# Add Visual Studio Code (code)

export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"

EOF

4.创建文件夹,在文件夹处进入工作空间

mkdir helloword    创建文件夹

code .   以该文件夹为工作空间打开vs code

5.三个配置文件

1c_cpp_properties.json:具体说明编译地址

第一步:“command+shift+p”打开命令行工具窗口,输入或者选择“

Edit Configurations”命令。

第二步(暂时不理解未做处理):Find the compilerPath setting and paste in the path to the bin folder. For Clang, this is typically /usr/bin/clang.

模版:

{

    "configurations": [

        {

            "name": "macOS",

            "includePath": [

                "${workspaceFolder}/**"

            ],

            "defines": [],

            "macFrameworkPath": [

                "/System/Library/Frameworks",

                "/Library/Frameworks"

            ],

            "compilerPath": "/usr/bin/clang",

            "cStandard": "c11",

            "cppStandard": "c++17",

            "intelliSenseMode": "clang-x64"

        }

    ],

    "version": 4

}

Notes:

includePath查看方法:clang -v -E -x c++ -

compilerPath查看方法:whitch clang

参考:https://www.jianshu.com/p/06fadb253941

https://www.cnblogs.com/flipped/p/9961468.html

2tasks.json:告诉vscode怎样构建程序(通过调用clang++编译器将源码构建为一个可执行文件)

第一步:“command+shift+p”打开命令行工具窗口,输入或者选择“Tasks: Configure Task

模版:

{

    "version": "2.0.0",

    "tasks": [

        {

            "label": "Build with Clang",

            "type": "shell",

            "command": "clang++",

            "args": [

                "-std=c++17",

                "-stdlib=libc++",

                "helloworld.cpp",

                "-o",

                "helloworld.out",

                "--debug"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            }

        }

    ]

}

 

3)launch.json:使用F5调试程序

Note that the program name helloworld.out matches what we specified in tasks.json.

第一步:“command+shift+p”打开命令行工具窗口,输入或者

选择Debug: Open launch.json命令。

模板:

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "(lldb) Launch",

            "type": "cppdbg",

            "request": "launch",

            "program": "${workspaceFolder}/helloworld.out",

            "args": [],

            "stopAtEntry": true,

            "cwd": "${workspaceFolder}",

            "environment": [],

            "externalConsole": true,

            "MIMode": "lldb",

            "logging": {

                "trace": true,

                "traceResponse": true,

                "engineLogging": true

            }

        }

    ]

}

6.创建helloworld.cpp文件

7.编译

8.debug

参考:https://code.visualstudio.com/docs/cpp/config-clang-mac

 

 

注意事项:

1.一定要在所在文件夹下的打开工作区

2.cpp文件一定要和.vscode在同一目录下,not cpp文件在.vscode文件夹中!!!

3.控制台不能输入问题:

在vscode中编写C++,不能使用vscode的控制台(目前的知识),需要使用外部的终端。

将lanch.json文件中的"externalConsole”修改为true

参考:https://stackoverflow.com/questions/41074170/unable-to-perform-this-action-because-the-process-is-running

4.C/C++编译器:

gcc和g++分别为gnu开发的c和c++的编译器

clang和clang++是LLVM编译器工具集的一个用于编译C、C++、Objective-C的前端。LLVM项目的目标是提供一个GNU编译器套装(gcc)的替代品。clang用于c,clang++用于c++

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Visual Studio Code 中配置 C 环境,你需要安装一些扩展和配置一些设置。下面是一个简单的步骤: 1. 安装 Visual Studio Code:从官方网站下载并安装最新版本的 Visual Studio Code。 2. 安装 C/C++ 扩展:在 Visual Studio Code 中点击左侧的扩展图标,搜索并安装 "C/C++" 扩展,由 Microsoft 提供。 3. 安装 C 编译器:安装一个 C 编译器,比如 GCC 或者 Clang。在 Windows 上,你可以安装 MinGW-w64,并将其路径添加到系统的环境变量中。在 macOS 上,你可以使用 Xcode Command Line Tools 或者安装 Clang。在 Linux 上,你可以使用系统包管理器安装 GCC 或者 Clang。 4. 配置编译任务:在 Visual Studio Code 中按下 `Ctrl + Shift + B`(或者通过菜单 "终端" -> "运行生成任务")来打开任务面板。选择 "C/C++: gcc build active file"(或者其他类似的选项),这将创建一个 `tasks.json` 文件。 5. 配置调试器:在 Visual Studio Code 中点击左侧的调试图标,然后点击顶部的齿轮图标以创建一个 `launch.json` 文件。选择 "C++ (GDB/LLDB)" 或者 "C++ (Windows)"(取决于你的平台),这将创建一个 `launch.json` 文件。 6. 编写和调试代码:在 Visual Studio Code 中创建一个新的 C 文件,或者打开一个已有的 C 文件。通过按下 `F5` 键(或者点击调试面板上的绿色箭头)来开始调试。 这些步骤应该能够帮助你在 Visual Studio Code 中配置 C 环境。你还可以根据自己的需要进一步定制和优化配置。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值