一、预备知识

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

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++