参考:https://blog.csdn.net/qq_33973359/article/details/105720511
vscode c++配置指南:;https://blog.csdn.net/qq_35793285/article/details/100546205
文档:https://code.visualstudio.com/docs/cpp/config-clang-mac
command + shift +p 打开命令窗口;
c_cpp_properties.json
(编译路径和智能提示设置)
***************以下是mac上的操作******************
***常规的多文件编译配置
lauch.json 用于设置调试、运行时的东西;
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/最上层目录和文件之间的文件夹/Seconds",//运行的文件路径,如果是多文件的话,要修改成${workspaceFolder}/文件夹/多文件编译后的文件.
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",//相当于文件最上层目录.
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"//与task.json中label一致,运行这一句代码表示已经调用了task.json的编译功能.这里也可以注释掉,在命令行用命令编译.
}
]
tasks.json 用于设置编译时的东西。
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file", 这个是和launch.json中的prelaunch一致的.
"command": "/usr/bin/clang++", 命令行命令
"args": [
"-g",
"${fileDirname}/first.cpp", 需要编译的文件
"${fileDirname}/add.cpp", 需要编译的文件
"-o",
"${fileDirname}/Seconds" 编译后输出的文件
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
command和args两条命令相当于命令行的:/usr/bin/clang++ -g first.cpp add.cpp -o /Users/HaokeMaster/Desktop/VSCODECPP/Seconds
setting.json是工作区设置;
查看includePath路径:clang -v -E -x c++ - ,得到/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
${workspaceFolder},只当前的项目所在总的文件夹,即最上层目录的绝对路径;
${fileDirname}当前目录,当前文件所在的目录;
${fileBasenameNoExtension}当前目录中的文件,在launch.json中指的是编译后的文件.
***cmake进行多文件编译
cmake下载:https://cmake.org/download/
在菜单栏Tools选项里选择How to install For Command Line Use,按照提示任选一种配置方法即可.
下载cmake,运行;
配置:sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install
检查cmake --version
步骤一:CMakeLists.txt中:
project(MYFIRST),MYFIRST表示项目的名称
add_excutable(myc first.cpp add.cpp) ,myc表示编译后可执行文件的名称,后面的.cpp文件是要编译的文件.
注意:CMakeLists.txt要和编译的cpp文件在同一级目录,并且build文件夹要和CMakeLists.txt在同一个文件夹中,否则不生成makefile文件.
步骤二:command+shift+p -》选择cmake:configure,会生成build文件夹.
步骤三:cd 到 build文件夹,.执行cmake命令后会配置完成会生成相关的文件makefiles;
步骤四: 在终端执行make命令.
步骤三和步骤四相当于task.json中的配置;
F5相当于launch.json中的配置.(如果launch.json中配置了prelaunch,就相当于launch.json和task.json一起配置了);
***以下的配置,直接F5就可以完成多文件编译了,若果要添加文件,直接在add——executable()中添加要编译的cpp文件就可以了.
CMakeLists.txt
project(MYF)
add_executable(fir first.cpp add.cpp)
task.json
{
"version": "2.0.0",
"options":{
"cwd":"${workspaceFolder}/build" //这个是build所在的路径
},
"tasks": [
{
"type":"shell",
"label":"cmake",
"command":"cmake",
"args":[
]
}, {
"type":"shell",
"label":"make",
"command":"make",
"args":[
]
},
{
"label":"build",
"dependsOn":[
"cmake",
"make"
]
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/fir",//因为这是cmake构建的,所以最后的可执行文件在build文件夹中,fir是编译后的可执行文件;
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",//相当于文件最上层目录.
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "build"//这里执行build,实际上是在task.json中执行了cmake 和make两个指令
// "preLaunchTask": "C/C++: clang++ build active file"
}
]
}
****************以下是windows上的操作****************
VS打开工程,右键工程名称—>属性—>c/c++—>命令行—> /d1 reportAllClassLayout
***命令行编译指令
1.mkdir build 创建build文件夹,build文件必须要和CMakeLists.txt在同一个目录;
2.cd /Users/HaokeMaster/Desktop/VSCODECPP/CMakeLists.txt所在的文件夹/build
3.cmake .. 配置
4.make 编译成可执行文件
****vscode c++中的快捷键
shift+alt+f 文档格式化