windows .vscode的json文件配置 CMake 构建项目 调试窗口中文设置等

发布时间:2024年01月23日

一、CMake 和 mingw64的安装和环境配置?

二、tasks.json和launch.json文件配置

  • tasks.json
{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group": "build",
            "command": "make",
            "args": [],
            "problemMatcher": []
        },
        {
            "label": "Build",
            "dependsOrder": "sequence",
            "dependsOn": [
                "cmake",
                "make"
            ]
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            // "command": "/usr/bin/g++",
            "command": "D://mingw64//bin//g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-o",
                "${workspaceFolder}/bin/app.exe",
                "-fexec-charset=GBK"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "D://mingw64//bin//g++.exe"
        }
    ]
}
  • launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/app.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
            ],
            "preLaunchTask": "Build",
            "miDebuggerPath": "D://mingw64//bin//gdb.exe", // 修改为你的 gdb 路径
        },
    ]
}

?三、CMakeLists.txt文件

cmake_minimum_required(VERSION 3.15)
project(Cat)

# C++ 的解决办法
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")

include_directories(${PROJECT_SOURCE_DIR}/include)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST)
# 添加可执行文件
add_executable(app main.cpp ${SRC_LIST})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

四、头文件和源文件

  • cat.h
#ifndef CAT_H
#define CAT_H
#include <iostream>
#include <string>
class Cat{
public:
    Cat(std::string name, int age); // 构造函数,初始化姓名和年龄
    // Getter
    std::string getName () const;
    int getAge() const;
    // Setter
    void setName(std::string name);
    void setAge(int age);
    // print
    void printObj() const;
    ~Cat();
private:
    std::string m_name; // 姓名
    int m_age; // 年龄
};
#endif
  • cat.cpp
#include "cat.h"
Cat::Cat(std::string name, int age) {
    this->m_name = name;
    this->m_age = age;
}

std::string Cat::getName() const{
    return this->m_name;
}

int Cat::getAge() const{
    return this->m_age;
}

void Cat::setName(std::string name) {
    this->m_name = name;
}

void Cat::setAge(int age) {
    this->m_age = age;
}

void Cat::printObj() const{
    std::cout << "Cat(" << this << ")" << ": Name : " << this->m_name << ": Age : " << this->m_age << std::endl;
}

Cat::~Cat() {
    std::cout<<"析构函数~"<<std::endl;
}
  • main.cpp
#include <iostream>
#include "cat.h"
using namespace std;

int main(int argc,char *argv[]) {
    // 值
    Cat cat1("moon",3);         
    // const Cat cat1("moon",3);
    // const Obj 只能调用 const Method
    cout<<cat1.getName()<<endl;
    cout<<cat1.getAge()<<endl;
    cat1.printObj();
    cat1.setName("黑猫警长");    
    cout<<cat1.getName()<<endl;

    // 指针
    const Cat *catPoint = &cat1;
    cout<<catPoint->getName()<<endl;
    cout<<catPoint->getAge()<<endl;
    catPoint->printObj();

    // 引用
    const Cat &catRef = cat1;
    cout<<catRef.getName()<<endl;
    cout<<catRef.getAge()<<endl;
    catRef.printObj();
    system("pause");
    return 0;
}

四、中文乱码问题解决

?CMake C/C++程序输出乱码
Clion CMake C/C++程序输出乱码_cmake message 乱码-CSDN博客https://blog.csdn.net/qq_37274323/article/details/120674592

# C的解决办法
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fexec-charset=GBK")
# C++ 的解决办法
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")

在此基础上,进一步来解决?VScode控制台中文乱码问题?(文章推荐)

(4 封私信 / 25 条消息) 如何解决VScode控制台中文乱码问题? - 知乎 (zhihu.com)https://www.zhihu.com/question/65557618/answer/2950382369?utm_id=0

  • 在tasks.json文件中添加:"-fexec-charset=GBK"

  • 接着,在设置中搜索"encoding",启用Auto Guess Encoding?

五、运行方式

方式一:1.先点击生成,可以生成所选目标

2.点击运行按钮,在终端窗口中启动所选目标?

方式二:按住F5运行,会出现调试窗口和终端窗口,也可以看变量等

文章来源:https://blog.csdn.net/weixin_41987016/article/details/135774788
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。