简体   繁体   中英

Cannot consistently disable Visual Studio 2019 warnings for headers with #pragma or project warning level

I am using SDL2/GLAD and stb_image.h with OpenGL, but AFAIK my problem is independent of that fact. I am editing properties under all configuration and I am not using precompiled headers.

I want to up my warning level to /W4 or even /Wall. However, /Wall gives me up to 1273 errors, most from the math library glm . So, I wrapped my external includes in a #pragma push/pop directive, but it seems to do absolutely nothing. Specifically disabling warnings with #pragma warning(disable : n) does nothing either.

When I started writing this question, no matter how or where I placed my #pragma directives (around headers, within headers, around functions, around calls), or whether I had my project warning level set anywhere from /W0 to /W4, about 80 errors would sneak through: one from SDL2, and the rest from stb_image.h . However, randomly during testing, my error list can jump between ~7 errors from stb_image.h back up to 70+.

I'm struggling to find any consistency in how Visual Studio handles errors. I just want to turn off errors from external headers and libraries so I can only see errors from code I have written. What am I doing wrong?

#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 };
}

// ...

This is probably because of IntelliSense, it seems to ignore "#pragma warning(push, 0)"

IntelliSense is very unstable (and we are in year 2022 already!)

What solved the problem for me is to select "Build Only" in the listbox, as the print bellow:

错误列表 - 仅构建

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM