预期 'const GLchar * const*' {aka 'const char * const*'} 但参数类型为 'char **'

expected 'const GLchar * const*' {aka 'const char * const*'} but argument is of type 'char **'

我试图将我的旧 openGl 代码(用 c++ 编写)转换为 c,当我尝试使用 glCompileShader() 时,我总是遇到关于不兼容指针类型的奇怪警告

char *buffer = 0;
long length;
FILE *f = fopen(vertexPath, "rb");

if(f) {
    fseek(f, 0, SEEK_END);
    length = ftell(f);
    fseek(f, 0, SEEK_SET);
    buffer = malloc(length);
    if(buffer) {
        fread(buffer, 1, length, f);
    }
    fclose(f);
}

if(buffer) {
    unsigned int vertex;
    int success;
    
    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &buffer, NULL);
    glCompileShader(vertex);
    glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
    
    if(!success) {
        char *infoLog;
        infoLog = malloc(512);
        glGetShaderInfoLog(vertex, 512, NULL, infoLog);
        printf("%s%s", "Error Fragment\n", infoLog);
        free(infoLog);
    }
}

这是我收到的警告消息:

..\src.\shader.c: In function 'compileShader':
..\src.\shader.c:29:35: warning: passing argument 3 of 'glad_glShaderSource' from 
incompatible pointer type [-Wincompatible-pointer-types]
   29 |         glShaderSource(vertex, 1, &buffer, NULL);
      |                                   ^~~~~~~
      |                                   |
      |                                   char **
..\src.\shader.c:29:35: note: expected 'const GLchar * const*' {aka 'const char * 
const*'} but argument is of type 'char **'

我不知道这个警告是什么意思,是否重要,或者我是否可以忽略它。 谢谢 4 帮助。

I don't know what this warning means, if it is important, or if I can ignore it.

这意味着 &buffer 是一个 char **(指向 char 指针的指针)但是该函数需要一个 char const * const *(指向 const 指向 const char 的指针)。

基本上就是说glShaderSource不能修改指针或它指向的缓冲区。

您可以通过强制转换来匹配签名来解决它:

(char const * const *)&buffer