首页 > 解决方案 > 无法始终为具有 #pragma 或项目警告级别的标头禁用 Visual Studio 2019 警告

问题描述

我将 SDL2/GLAD 和stb_image.h与 OpenGL 一起使用,但我的问题与该事实无关。我正在编辑所有配置下的属性,并且我没有使用预编译的头文件。

我想将警告级别提高到 /W4 甚至 /Wall。但是,/Wall 给我最多 1273 个错误,大部分来自数学库glm。所以,我将我的外部包含包装在一个#pragmapush/pop 指令中,但它似乎什么也没做。专门禁用警告#pragma warning(disable : n)也无济于事。

当我开始写这个问题时,无论我如何或在何处放置我的#pragma 指令(围绕标题、在标题内、围绕函数、围绕调用),或者我是否将我的项目警告级别设置为从 /W0 到 /W4 的任何位置,大约80 个错误会潜入:一个来自 SDL2,其余来自stb_image.h. 但是,在测试期间随机,我的错误列表可以在大约 7 个错误之间跳转,从stb_image.h备份到 70+。

我正在努力寻找 Visual Studio 如何处理错误的一致性。我只想关闭来自外部头文件和库的错误,所以我只能看到我编写的代码中的错误。我究竟做错了什么?

#pragma warning(push, 0)
#include <glad/glad.h>
#include <SDL.h>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image/stb_image.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#pragma warning(pop)

#include <iostream>
#include <vector>
#include <fstream>

// ...

Image LoadTexture(const std::string& path, GLenum format) {
    int width, height, channels;
    unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, STBI_default);

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    if (data) {
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else {
        std::cerr << "Failed to load texture " << path << std::endl;
        texture = -1; // error
    }

    stbi_image_free(data);
    return { texture, width, height, channels };
}

// ...

标签: c++visual-studiovisual-studio-2019warningspragma

解决方案


推荐阅读