vscode配置c++多目录多文件cmake编译(win linux通用)

2022.7.3:复杂项目有待验证!!待更新

在.vscode目录中添加以下三个文件

  • launch.json
  • settings.json
  • tasks.json
    这个文件配置也和linux win系统无关,你只需要在编译时告诉vscode使用哪个编译器即可。复杂项目还未测试,请悉知!

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": "(gdb) Launch",//配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",//配置类型
            "request": "launch",//请求配置类型,有launch(启动)或attch(附加)两种
            "program": "${workspaceFolder}/build/main",  //调试时需要指定的程序路径
            "args": [],//调试时给程序输入的参数
            "stopAtEntry": false,//为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceFolder}",//调试程序时的工作目录,${workspaceFolder}表示代码所在目录
            "environment": [],
            "externalConsole": true,//调试时是否显示控制台窗口,true为在终端显示
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make"  //这个重要,task中的make
        }
    ]
}

settings.json内容

{
    "files.associations": {
        "array": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "optional": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "type_traits": "cpp",
        "tuple": "cpp",
        "typeinfo": "cpp",
        "utility": "cpp"
    }
}

tesks.json

{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceRoot}/build"
    },
    "tasks": [
        {
            "label": "cmake",
            "type": "shell",
            "command": "cmake",
            "args": [
                "-G",
                "Unix Makefiles",
                "-DCMAKE_BUILD_TYPE=Debug",
                ".."
            ]
        },
        {
            "label": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "type": "shell",
            "command": "make",
            "args": []
        }
        
    ]
}

怎么使用?

安装插件:

在这里插入图片描述

配置CMakeLists.txt

首先,主目录需要写一个CMakeLists.txt,然后每添加一个包含源码的子目录,都需要在子目录下写一个CMakeLists.txt,然后在主目录的CMakeLists.txt文件中包含该子目录。
例如,我的测试项目的整体结构是这样的:

---.vscode
---build
---func//子目录1
	---CMakeLists.txt
	---func.cpp
	---func.h
---lib//生成的库文件存放路径
---test//子目录2
	---CMakeLists.txt
	---add.cpp
	---add.h
	---hello.cpp
	---hello.h
---CMakeLists.txt
---main.cpp

在这里插入图片描述

cmake文件怎么写就不展示了,这就取决于你的项目是如何配置的,当然你也可以把所有源文件放在src目录下,头文件放在include目录下,这都是可以的,关键还是你如何配置你的CMakeLists.txt,因为vscode的配置已经在.vscode中配置好了,你需要告诉cmake编译你主目录下的所有文件!!,如果不知道cmake怎么写的话就学一下吧,基本的东西比较简单,学完之后写个简单的测试项目还是比较有成就感的,这也是学习的乐趣所在!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值