在MacOS上使用VS Code进行C++开发
在MacOS上使用VS Code进行C++开发
基础库安装
基于之前的经验,安装VS Code本不会遇到什么困难,但是在MacOS上遇到过几次奇怪的问题。总结发现,都是因为进行了系统版本的升级,例如升级到了Big Sur,或者小版本的升级,例如从11.0升级到11.2等。
问题的具体表现是,对于一个简单的C++源码,无法找到iostream
等基础库文件,后来发现并不是VSCode软件的includePath设置错了,而是系统升级后,需要重新运行xcode-select --install
进行基本工具链的安装。
参考链接:Configure VS Code for Clang/LLVM on macOS
准备知识
VS Code工作区 (Workspace)
因为接下来的设置都需要用到工作区workspace
的概念,所以先介绍下工作区。简单讲,你用VS Code打开一个文件夹,那么这个文件夹及文件夹中包含的所有子文件和子文件夹就组成一个工作区。VS Code会自动记录你打开了这个工作区的哪些文件,等你下次打开这个工作区时,VS Code会自动帮你恢复到上次到布局。
当然,引入工作区的概念,还有一些好处,比如一些设置是可以专门针对某个工作区而非全局的。具体实现,即VS Code会自动在工作区中创建一个.vscode
的文件夹,保存例如settings.json
, launch.json
, tasks.json
等配置文件。
最后还有一点,工作区是可以有多个根路径的,即组成Multi-root workspace. 更多资料请参考Workspaces in Visual Studio Code
VS Code中的变量 (Variable)
在调试Debugging的配置文件(launch.json
)和任务Task的配置文件tasks.json
中,有很多可用的变量,统一使用${variableName}
的语法进行替换。同时,VS Code还支持环境变量,使用${env:Name}
的语法进行替换,例如{env:USERNAME}
预定义变量如下:
- ${workspaceFolder} - the path of the folder opened in VS Code
- ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
- ${file} - the current opened file
- ${fileWorkspaceFolder} - the current opened file’s workspace folder
- ${relativeFile} - the current opened file relative to workspaceFolder
- ${relativeFileDirname} - the current opened file’s dirname relative to workspaceFolder
- ${fileBasename} - the current opened file’s basename
- ${fileBasenameNoExtension} - the current opened file’s basename with no file extension
- ${fileDirname} - the current opened file’s dirname
- ${fileExtname} - the current opened file’s extension
- ${cwd} - the task runner’s current working directory on startup
- ${lineNumber} - the current selected line number in the active file
- ${selectedText} - the current selected text in the active file
- ${execPath} - the path to the running VS Code executable
- ${defaultBuildTask} - the name of the default build task
- ${pathSeparator} - the character used by the operating system to separate components in file paths
更具体的例子可以参考 Visual Studio Code Variables Reference
VS Code中的任务 (Task)
在VS Code中可以利用任务(Task)和很多现存的工具结合起来,自动实现编译,打包,测试等流程。在工作区中配置任务,可以选择Terminal
-> Configure Tasks...
来配置,该操作会在.vscode
文件夹中创建一个tasks.json
的文件。
关于任务中预定义的一些关键字如下: Tasks in Visual Studio Code
- label: The task’s label used in the user interface.
- type: The task’s type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute.
- command: The actual command to execute.
- windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
- group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
- presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.
- options: Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.
- runOptions: Defines when and how a task is run.
macOS平台工具链
在macOS中,默认的C/C++工具链是Clang / LLVM (Low Level Virtual Machine). 在业界,有三种主流的编译器, Visual C++, GNU Compiler Collection (GCC).
GCC遵循GPL (GNU General Public License), 是Linux系统的默认编译器,而LLVM包含了一系列的模块化的编译模块和工具链。而Clang是 C, C++, Objective-C 和 Objective-C++的编译器,大部分情况下,Clang比GCC的编译效率要更高一些。
dSYM文件
dSYM文件全称Debug Symbol file, 是macOS平台下使用g++
或者clang++
编译开启生成调试信息-g
选项后生成的文件,在Linux平台下则不会产生。
进入正题:调试C++代码
以前在Big Sur的第一个版本中遇到过无法调试的问题,具体讲是 -无法设置断点。- 曾经参考一个分享设置好了用CodeLLDB这个插件来调试。 -这次发现,在11.2版本中,可以直接用系统自带的clang++来调试了。按照正常流程操作即可。-
先说效果,利用目前的这套配置,可以实现以下特性:
- 将输入输出流绑定在VS Code内置terminal上
- 可以设置断点,方便调试
- 可以在调试结束后,自动删除dSYM文件
- 易迁移,直接拷贝至其他工作区
.vscode
文件夹中即可
下面来逐个说一下具体的实现,因为VS Code默认的C/C++插件是无法在内置terminal上绑定程序的输入输出流的,所以我们需要借助CodeLLDB
这个插件来实现。安装好插件后,选择调试按钮,新建调试环境选择LLDB
而非默认的C++(GDB/LLDB)
,如下图所示。
默认的调试launch.json
文件如下:
1 |
|
如果直接用这个配置文件,是跑不通的,因为
<your program>
这部分是需要自己来补齐的。- 需要先编译生成二进制文件和调试文件,之后才可以进行调试
那么我们要
- 补齐调试前用到的自动编译脚本。
- 前面提到macOS平台下g++编译并开启生成调试信息后,会产生dSYM文件,如果我们能在调试结束后自动删除dSYM文件,那么就可以保持整洁。
而launch.json文件中,preLaunchTask
关键字用以调试前自动编译文件,postDebugTask
关键字用以调试后删除dSYM文件。
配置结果
先说最终文件效果,如下所示,拷贝至对应的工作区即可完成迁移:
.vscode/launch.json
调试文件:
1 |
|
调试的目标文件是文件当前目录下生成的不含文件扩展名的同名文件,调试结束后,自动运行删除dSYM文件的任务。
.vscode/tasks.json
任务文件:
1 |
|
每个task中的label
标签描述,即对应着launch.json文件中preLaunchTask
和postDebugTask
描述的不同任务。
对于删除dSYM文件的任务,任务类型为shell,使用rm -rf
命令进行删除。
总结
本文详细描述了在macOS平台下使用VS Code进行C++开发的具体流程,学习了工作区,任务,launch文件等背景知识,并给出了一种解决方案。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!