C++问题汇总(自用)

1.vs中const char *”类型的值不能用于初始化“char *”类型的实体错误

解决方法:

项目->属性->C/C++->语言->符合模式,将符合模式由是改为否

原因:

2.关于vscode 多文件编译的问题

2.1.vscode如果想编译这样的结构:

 那么他的task.json中必须涉及到main.cpp  和 .cpp文件(.h可以不用) 

 只编译main.cpp 和 .h是不行的,  main.cpp  会自动寻找.h文件 所以再加上.cpp就可以了  不用单独引入.h文件(前提系统也能include到, 如果引入不到,  就要 g++编译时候  加上参数 -I)

像这样:g++ main3.cpp -I./enc  -lpthread -o main     (一个是  大写的i  一个是小写的l)

vscode 报错:

 1. This may occur if the process’s executable was changed after the process wasstarted, such as when installing an update. Try re-launching the application orrestarting the machine.
 

 解决方法:

这个不是由vscode造成的,这个是mingwgdb本身不支持中文路径名造成的,如果你要用gdb调试的话就不能有中文字符出现在路径名里

文件名不能以中文命名,改成英文就OK!

2 .include errors detected. Please update your includePath.

或者看c_cpp_properties.json 的 includePath选项

其实主要是intelliSenseMode模式下.c文件系统自动匹配的头文件没有 iostream,如果把下面两个注释取消掉, using 又会报错误 :variable "using" is not a type name  所以直接改成cpp文件吧

3.  [1] + Done   "/usr/bin/gdb" --interpreter=mi如何取消这个输出?

描述:

我自己的vscode配置:(更新于2022-12-10)

Winodws:


c_cpp_properties.json 

{
    "configurations": [
        {
            "name": "zhangdaxian-2022-11-11",
            "includePath": [
                "${workspaceFolder}/**" //此处会匹配工作文件下的所有文件
                                                //添加"compilerPath"后,系统include路径可不写明
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "_CRT_SECURE_NO_WARNINGS"

            ],
            "windowsSdkVersion": "10.0.19041.0",
            "cStandard": "c17",                                          //C标准的版本
            "cppStandard": "c++17"   ,                                  //C++标准的版本
            "compilerPath": "D:\\mingw64\\bin\\g++.exe"                //编译器的路径
        }
    ],
    "version": 4
}
//c_cpp_properties.json 主要用来设置包含头文件的路径,设置 C/C++ 支持的版本号等等  https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

settings.json

{
  "window.zoomLevel": -1,
  "files.exclude": {
    "**/.exe": true
  },
  "C_Cpp.default.intelliSenseMode": "gcc-x64",
  "C_Cpp.default.compilerPath": "D:\\mingw64\\bin\\g++.exe",
  "C_Cpp.default.cppStandard": "c++17",
  "debug.onTaskErrors": "abort",
  "files.associations": {
    "iostream": "cpp",
    "ostream": "cpp",
    "numeric": "cpp",
    "array": "cpp",
    "atomic": "cpp",
    "*.tcc": "cpp",
    "cctype": "cpp",
    "clocale": "cpp",
    "cmath": "cpp",
    "cstdarg": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "ctime": "cpp",
    "cwchar": "cpp",
    "cwctype": "cpp",
    "deque": "cpp",
    "unordered_map": "cpp",
    "vector": "cpp",
    "exception": "cpp",
    "algorithm": "cpp",
    "functional": "cpp",
    "iterator": "cpp",
    "memory": "cpp",
    "memory_resource": "cpp",
    "optional": "cpp",
    "random": "cpp",
    "string": "cpp",
    "string_view": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "utility": "cpp",
    "fstream": "cpp",
    "initializer_list": "cpp",
    "iosfwd": "cpp",
    "istream": "cpp",
    "limits": "cpp",
    "new": "cpp",
    "sstream": "cpp",
    "stdexcept": "cpp",
    "streambuf": "cpp",
    "typeinfo": "cpp",
    "chrono": "cpp",
    "valarray": "cpp",
    "iomanip": "cpp",
    "bitset": "cpp",
    "cfenv": "cpp",
    "charconv": "cpp",
    "cinttypes": "cpp",
    "codecvt": "cpp",
    "complex": "cpp",
    "condition_variable": "cpp",
    "csetjmp": "cpp",
    "csignal": "cpp",
    "cstring": "cpp",
    "cuchar": "cpp",
    "forward_list": "cpp",
    "list": "cpp",
    "unordered_set": "cpp",
    "map": "cpp",
    "ratio": "cpp",
    "regex": "cpp",
    "set": "cpp",
    "future": "cpp",
    "mutex": "cpp",
    "scoped_allocator": "cpp",
    "shared_mutex": "cpp",
    "thread": "cpp",
    "typeindex": "cpp",
    "stack": "cpp",
    "cassert": "cpp",
    "ccomplex": "cpp",
    "cerrno": "cpp",
    "cfloat": "cpp",
    "ciso646": "cpp",
    "climits": "cpp",
    "cstdalign": "cpp",
    "cstdbool": "cpp",
    "ctgmath": "cpp",
    "filesystem": "cpp",
    "ios": "cpp",
    "locale": "cpp",
    "queue": "cpp",
    "*.rh": "cpp"
  },
 
}

 task.json  负责编译

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "D:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "-Wall",
                "-std=c++17",
              
                "${fileDirname}\\*.cpp",
                // "${fileDirname}\\*.h",
                // "-lpthread",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
              
            ],
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "dedicated",
                "showReuseMessage": false,
                "clear": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        },
       
    ],
    "options": {
        "shell": {
            "executable": "${env:SystemRoot}\\System32\\cmd.exe",
            "args": [
                "/c"
            ]
        },
        "env": {
            "Path": "D:\\mingw64\\bin;${env:Path}"
        }
    }
}

${workspaceFolder} :表示当前workspace文件夹路径,也即/home/Coding/Test
${workspaceRootFolderName}:表示workspace的文件夹名,也即Test
${file}:文件自身的绝对路径,也即/home/Coding/Test/.vscode/tasks.json
${relativeFile}:文件在workspace中的路径,也即.vscode/tasks.json
${fileBasenameNoExtension}:当前文件的文件名,不带后缀,也即tasks
${fileBasename}:当前文件的文件名,tasks.json
${fileDirname}:文件所在的文件夹路径,也即/home/Coding/Test/.vscode
${fileExtname}:当前文件的后缀,也即.json
${lineNumber}:当前文件光标所在的行号
${env:PATH}:系统中的环境变量

launch.json

{
  "version": "0.2.0",
  "configurations": [
    
    {
      "name": "g++.exe build and debug active file", // 配置名称,将会在启动配置的下拉菜单中显示 
      "type": "cppdbg", // 配置类型,这里只能为cppdbg  
      "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径  注意\\还是//就行(windows是\\)
      "args": [],  // 程序调试时传递给程序的命令行参数,一般设为空即可 
      "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false 
      "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
      "environment": [],
      "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台 
      "MIMode": "gdb",
      "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",  // miDebugger的路径,注意这里要与MinGw的路径对应 
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc 
      "internalConsoleOptions": "neverOpen",
    }
  ]
}

linux:

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "daxian-2023-12-24",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/rust/**",
                "${workspaceFolder}/src/**",
                
                //系统级别
                "/usr/include/**",   //  g++ -v -E -x c++ - 
                "/usr/local/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    //"${workspaceFolder}"
                    // "${workspaceFolder}/rust",
                    // "${workspaceFolder}/src"
                   
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}


// c_cpp_properties.json 主要用来设置包含头文件的路径,设置 C/C++ 支持的版本号等等 
// https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

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": "g++ - Build and debug active file", // 配置名称,将会在启动配置的下拉菜单中显示 
            "type": "cppdbg",  // 配置类型,这里只能为cppdbg  
            "request": "launch",  // 请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}/${fileBasenameNoExtension}",// 将要进行调试的程序 注意linux和windows的区别就行
            "args": [],// 程序调试时传递给程序的命令行参数,一般设为空即可 
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口
            "cwd": "${workspaceFolder}",// 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
            "environment": [],
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb", // miDebugger的路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",  // 调试会话开始前执行的任务,一般为 编译程序task.json中的label
        }
    ]
}

settings.json

{
  "window.zoomLevel": 0.5,
  "files.exclude": {
    "**/.exe": true  //排除尾缀为.exe的文件不在编辑器左边的目录菜单中显示
  },
  // "C_Cpp.intelliSenseEngine": "Tag Parser",/*或者是default模式(默认也是这个)*/
  "C_Cpp.exclusionPolicy": "checkFilesAndFolders",

  "C_Cpp.default.intelliSenseMode": "gcc-x64",
  "C_Cpp.default.compilerPath": "/usr/bin/g++",
  "C_Cpp.default.cppStandard": "c++17",
 
  "debug.onTaskErrors": "abort",
  "files.associations": {
    "iostream": "cpp",
    "suricata-common.h": "c",
    "cstdlib": "c",
    "array": "c",
    "istream": "c",
    "ostream": "c",
    "tuple": "c",
    "type_traits": "c",
    "utility": "c",
    "fstream": "c",
    "streambuf": "c",
    "queue.h": "c",
    "deque": "c",
    "string": "c",
    "unordered_map": "c",
    "vector": "c",
    "string_view": "c",
    "initializer_list": "c",
    "functional": "c",
    "threads.h": "c",
    "threadvars.h": "c"
  },
  "C_Cpp.errorSquiggles": "enabled",
  "workbench.editor.wrapTabs": true,/*选项卡是否换行*/
  "diffEditor.renderSideBySide": true,
  // "editor.renderWhitespace": "all"/*让空格显示为点  selection*/
 
}

 tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",// 可以为shell或process,前者相当于先打开shell再输入命令,后者是直接运行命令
        "label": "C/C++: g++ build active file",// 任务名称,与launch.json的preLaunchTask相对应
        "command": "/usr/bin/g++",  //使用的编译器
        "args": [
          "-g",// 生成和调试有关的信息
          "-Wall", // 开启额外警告pp
          "-std=c++17",
          "${fileDirname}/*.cpp", 
          //"${fileDirname}\\*.h",
          //"${file}", //当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名
         
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}",
         
        ],
        "detail": "compiler: /usr/bin/g++", //提供额外的信息,一般是编译器信息等
        "group": {
          "kind": "build",
          "isDefault": true// 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件
        },
        "presentation": {
          "echo": true,  //控制执行的命令是否在终端中回显。默认为true。
          "reveal": "always",// 在“终端”中显示编译信息的策略,可以为always,silent,never。具体参见VSC的文档:https://code.visualstudio.com/docs/editor/tasks#vscode
          "focus": true, // 设为true后可以使执行task时焦点聚集在终端
          "panel": "dedicated",// 不同的文件的编译信息共享一个终端面板
          "showReuseMessage": false,
          "clear": true  //控制在运行此任务之前是否清除终端。默认为false。
        },
        "problemMatcher": [
          "$gcc"
        ]
      }
    ],
    "options": {
        "shell": {   
          "executable": "/bin/bash",   //指定使用 Bash 作为 shell
          "args": ["-c"]  //使用 -c 参数执行命令
        },
        "env": {
          "PATH": "/usr/bin:${env:PATH}" //将 /usr/bin 添加到系统的 PATH 环境变量中
        }
      }
      
}
// vscode 特殊变量参考表 : https://code.visualstudio.com/docs/editor/variables-reference

 c_cpp_properties.json 中includePath已经包含了我的头文件路径,为什么还是报错?

  • 可能是因为被settings.json 中files.exclude忽略了  (如果你认为其他配置没有错误),如果不知道file.exclude的功能请百度....

比如头文件就在这其中 的某一项

 就会出现下面的提示

  • 还有可能的原因

可以看到settings.json中注释掉了

// "C_Cpp.intelliSenseEngine": "Tag Parser",
  // "C_Cpp.intelliSenseEngine": "default",
  // "C_Cpp.intelliSenseEngineFallback": "enabled",

默认情况下intelliSense 是default模式 ,然后他与c_cpp_properties.json 中browse->path相关

官方解释: https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

default时候 会用上面的includePath内容作为默认,并且请你注意*代表非递归,和includePath不太一样

直接看视频吧 算了,打字太麻烦了

VsCode中includePath已经包含了头文件路径,为什么还是报错?

VsCode中includePath已经包含了头文件路径,为什么还是报错?_哔哩哔哩_bilibili

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值