1. 下载VSCode
- VSCode下载地址
- 在mac终端里输入xcode- select --install命令,根据提示安装xcode工具。
2. 安装插件(4个)
- 打开VScode,点击应用右侧菜单栏
- C/C++(必装)
- Code Runner(必装)
- CodeLLDB(代码调试),不安装这个插件程序调试时,无法在vscode自带的终端里输入参数
3. 配置环境
- 新建文件夹
- 打开文件夹
- 在code下新建001_EvnTest.cpp
- 点击右上角的小齿轮
- 选择点击C/C++:gcc或者C/C++:clang
- 会自动添加launch.json和tasks.json文件,并自动打开launch.json文件
- 配置launch.json和tasks.json文件
-
launch.json文件配置
{"configurations": [{"name": "C/C++: g++ build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "lldb","preLaunchTask": "C/C++: g++ build active file"}],"version": "2.0.0" }
-
task.json文件配置
{"tasks": [{"type": "cppbuild","label": "C/C++: g++ build active file","command": "/usr/bin/g++","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": "build","detail": "Task generated by Debugger."}],"version": "2.0.0" }
4. 运行程序
- 运行程序,点击Run Code运行程序,程序会在终端中运行
- 调试程序,不要使用右上角的debug,使用左侧菜单栏中的调试
5. 遇到的问题
- 如果在运行程序的时候,遇到问题Undefined symbols for architecture x86_64:
原因:cpp文件的文件名不合规,比如用了特殊符合,空格等。Undefined symbols for architecture x86_64:"_main", referenced from:implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)