0


vscode编译多文件复杂项目时tasks.json launch.json 的配置

文章目录


前言

本文介绍了利用vscode编译复杂工程的方法,包括gcc和cmke编译时 tasks.json launch.json c_cpp_properties.json 的具体配置。


一、编译代码

使用例子
c/c++在windows下编译:使用MinGW gcc从零编译项目
在这里插入图片描述

二、g++配置tasks.json launch.json c_cpp_properties.json

tasks.json

编译程序所需要的指令

{"tasks":[//下面两条g++语句编译动态库{"type":"cppbuild","label":"build_shared_step1",//相当于这条g++语句的标识"command":"/usr/bin/g++",//编译器安装的完整路径"args":["-g","*.cpp","-c","-fPIC"//相当于/usr/bin/g++ -g *.cpp -c -fPIC 将当前目录下的所有.cpp文件都生成.o文件 ],"options":{"cwd":"${fileDirname}/../shared"//${fileDirname}为main函数所在目录,意思是进入到shared目录},"group":"build"},{"type":"cppbuild","label":"build_shared_step2",//该条g++语句的标识"command":"/usr/bin/g++","args":["-g","-shared","*.o","-o","../../lib/libmulti.so"//g++ -g -shared *.o -o ../../lib/libmuti.so 将当前路径的.o文件生成.so动态库放入lib下],"options":{"cwd":"${fileDirname}/../shared"},"group":"build","dependsOn":["build_shared_step1"//这条g++语句的执行依赖于上一条g++的执行]},//下面两条语句编译静态库{"type":"cppbuild","label":"build_static_step1","command":"/usr/bin/gcc","args":["-g","*.cpp","-c"// /usr/bin/gcc -g *.cpp -c 当前目录下的所有.cpp文件生成.o文件],"options":{"cwd":"${fileDirname}/../static"//进入static目录},"group":"build"},{"type":"cppbuild","label":"build_static_step2","command":"/usr/bin/ar","args":["rcs","../../lib/libadd.a","*.o"// /usr/bin/ar rcs ../../lib/libadd.a *.o  将所有.o文件打包成静态库],"options":{"cwd":"${fileDirname}/../static"},"group":"build","dependsOn":["build_static_step1"//该条g++语句依赖于上一条g++语句生成的.o文件]},//生成可执行文件{"type":"cppbuild","label":"build_main",//生成可执行程序命令名称,launch.json会使用"command":"/usr/bin/g++","args":["-g","*.cpp","-o","../../bin/app","-I../../include","-L../../lib","-lmulti","-ladd","-Wl,-rpath=../lib"],"options":{"cwd":"${fileDirname}"},"group":"build","dependsOn":["build_shared_step2",//可执行文件生成依赖生成静态库和动态库的g++语句"build_static_step2"]}],"version":"2.0.0"}

上述tasks.json相当于在脚本中执行以下几条命令

#生成动态库cd${fileDirname}/../shared
/usr/bin/g++ -g *.cpp -c-fPIC
g++ -g-shared *.o -o../../lib/libmuti.so
#生成静态库cd${fileDirname}/../static
/usr/bin/gcc -g *.cpp -c
/usr/bin/ar rcs ../../lib/libadd.a *.o
#生成可执行文件cd${fileDirname}
/usr/bin/g++ -g *.cpp -o../../bin/app -I../../include -L../../lib -lmulti-ladd -Wl,-rpath=../lib

每一条命令都要在tasks.json中的tasks中进行配置,如果工程过大,就会显得tasks.json非常臃肿和复杂,所以需要减少命令,可以通过编写makefile 和cmake。

launch.json

配置可执行程序的调试信息

{"version":"0.2.0","configurations":[{"name":"aaaaa","type":"cppdbg","request":"launch","program":"${fileDirname}/../../bin/app",//tasks.json生成可执行文件的路径"args":[],//可执行文件需要的参数"stopAtEntry":false,//是否在main函数处停住"cwd":"${fileDirname}/../../bin",//进入可执行文件目录"environment":[],"externalConsole":false,"MIMode":"gdb","miDebuggerPath":"/usr/bin/gdb",//gdb的完整路径"setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"miDebuggerArgs":"-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi","preLaunchTask":"build_main"//在调试前需要完成的任务,这里是生成可执行程序那条g++语句}]}

tasks.json和launch.json中的依赖关系如下图:
在这里插入图片描述

c_cpp_properties.json

只在程序进行展示的时候有用

{"configurations":[{"name":"linux-gcc-x64","includePath":["${workspaceFolder}/**"//设置头文件所在路径,防止在打开程序是头文件飘红],"compilerPath":"/usr/bin/gcc","cStandard":"${default}","cppStandard":"${default}","intelliSenseMode":"linux-gcc-x64","compilerArgs":[""]}],"version":4}

三 cmake配置tasks.json

{"tasks":[{"type":"cppbuild","label":"build_cmake","command":"cmake","args":["-DCMAKE_INSTALL_PREFIX=${fileDirname}/../../install"," -DCMAKE_BUILD_TYPE=Release"," -DCMAKE_OS_ARCH=x86","-DCMAKE_OS_TYPE=linux","."],"options":{"cwd":"${fileDirname}/../../"},"group":"build",},{"type":"cppbuild","label":"build_make","command":"make","args":[],"options":{"cwd":"${fileDirname}/../../"},"group":"build","dependsOn":["build_cmake"]},{"type":"cppbuild","label":"build_install","command":"make","args":["install"],"options":{"cwd":"${fileDirname}/../../"},"group":"build","dependsOn":["build_cmake"]}],"version":"2.0.0"}

上面tasks.json相当于如下三句指令:

cmake .-DCMAKE_INSTALL_PREFIX=${fileDirname}/../../install  -DCMAKE_BUILD_TYPE=Release  -DCMAKE_OS_ARCH=x86 -DCMAKE_OS_TYPE=linux
makemakeinstall

四 调试代码

在main.cpp界面点击右上角如图,出现如下界面:其中绿色的compilerun 点击过后会报编译错误,因为绿色的是编译器默认的tasks.json;如果我们是一个文件的程序,可以直接点三角形默认生成tasks.json,这时就选择ComlieRun进行编译。红色的就会执行我们修改后的tasks.json,Debug c/c++ File表示编译运行,但是不会在断点处停留;Run C/C++ File 会编译调试可执行程序,并且会在断点处停留。
在这里插入图片描述
点击Debug C/C++出现如下界面,会展示tasks.json中的每一个任务,红色的就是我们tasks.json中的所有任务,点击对应任务我们就能值编译这一个任务。绿色的就是编辑器默认的tasks.json这里并不存在,如果选择就会编译出错。
在这里插入图片描述
我们将可执行程序通过launch.json配置了调试信息,其中这条调试在launch.json中命名为aaaaa,我们点击这一条就能对可执行程序进行调试了。
在这里插入图片描述
选择 Debug c/c++ File -----> aaaaa 就能进行断点调试。
在这里插入图片描述

总结

vscode一键配置C/C++多个C及CPP文件编译与tasks.json和launch.json原理

标签: vscode 编辑器 c++

本文转载自: https://blog.csdn.net/qq_36472340/article/details/132656792
版权归原作者 随便写写。 所有, 如有侵权,请联系我们删除。

“vscode编译多文件复杂项目时tasks.json launch.json 的配置”的评论:

还没有评论