C++问题汇总(自用)

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

解决方法:

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

C++问题汇总(自用)_第1张图片

原因:

C++问题汇总(自用)_第2张图片

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

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

C++问题汇总(自用)_第3张图片

 那么他的task.json中编译必须编译涉及到main.cpp  和 .cpp文件就可以 

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

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

C++问题汇总(自用)_第4张图片

vscode 报错:

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.
 

C++问题汇总(自用)_第5张图片

 解决方法:

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

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

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

Winodws:


c_cpp_properties.json 

//Visual Studio Code 中 C/C++ 扩展使用的配置文件,用于指定 C/C++ 项目的一些配置信息,
//包括编译器路径、标准版本、头文件路径等
{
    "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

{
  "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"
  },
  "window.zoomLevel": -1
}

 task.json  负责编译


{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",// 可以为shell或process,前者相当于先打开shell再输入命令,后者是直接运行命令
      "label": "g++.exe build active file",// 任务名称,与launch.json的preLaunchTask相对应
      "command": "D:\\mingw64\\bin\\g++.exe", // 要使用的编译器
      "args": [
        "-g",// 生成和调试有关的信息
        "-Wall", // 开启额外警告pp
        "-std=c++17",
        "${fileDirname}\\*.cpp", 
        //"${fileDirname}\\*.h",
        //"${file}", //当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名
       
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        //"-lws2_32",
      ],
      "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": "${env:SystemRoot}\\System32\\cmd.exe",
      "args": [
        "/c"
      ]
    },
    "env": {
      "Path": "D:\\mingw64\\bin;${env:Path}"
    }
  }
}



C++问题汇总(自用)_第6张图片

C++问题汇总(自用)_第7张图片

${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

//Visual Studio Code 中 C/C++ 扩展使用的配置文件,用于指定 C/C++ 项目的一些配置信息,包括编译器路径、标准版本、头文件路径等
//这是本地的项目  跟编译和运行调试没什么关系
{
    "configurations": [
        {
            "name": "zhangdaxian-2023-12-24",
            "includePath": [
                "${workspaceFolder}/**"  //https://mp.csdn.net/mp_blog/creation/editor/127338387
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "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": true, // 设为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
        }
    ]
}

setting.json

//配置用户首选项和扩展设置的文件
{
    "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",
        "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"
      },
    "window.zoomLevel": -1,
    "files.exclude": {
        "**/.exe": true
    }

}
  

 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 环境变量中
        }
      }
      
}

你可能感兴趣的:(c++,for_myself,c++)