简体   繁体   中英

Why is glGenVertexArrays defined for a C program but not a C++ program on Linux?

Consider the following file:

#include <SDL.h>
#include <GLES2/gl2.h>

int main() {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("Test", 0, 0, 200, 200, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    SDL_GLContext context = SDL_GL_CreateContext(window);
    GLuint vao;
    glGenVertexArrays(1, &vao);
}

If the above is test.c, then the following line (compiling as C code) works perfectly:

gcc test.c -I/usr/include/SDL2 -lGLESv2 -lSDL2

This next one (compiling as C++ code), however, does not:

gcc -x c++ test.c -I/usr/include/SDL2 -lGLESv2 -lSDL2

The error that I get is:

test.c: In function ‘int main()’:
test.c:9:27: error: ‘glGenVertexArrays’ was not declared in this scope
  glGenVertexArrays(1, &vao);

I am compiling this on x86 Linux with gcc 4.8.2, using SDL 2.0 and OpenGL ES 2.0.

What is going on? Many of the other OpenGL ES 2.0 calls that I make (glDrawArrays, glGenBuffers, etc.) work perfectly well with both C and C++. Additionally, isn't C++ supposed to be able to call C libraries, especially system ones that (should) be designed to prevent function name mangling?

What is wrong, and how can I fix this?

https://www.khronos.org/opengles/sdk/docs/man3/html/glGenVertexArrays.xhtml

The previous answer is also correct but doesn't really gives a solution. Nor mine actually. It's just that glGenVertexArrays is not supported by OpenGLES 2.

Solution : Use OpenGL ES 3.

Looking a the gl2.h header , it seems that the aformentioned function is not defined there. C++ has stricter rules with regard to function declaration and definition. In C, you may perfectly use a function which asn't been declared before hand, and the compiler will assume a certain prototype. In C++, any function must be at least declared before any use.

The section 5.2.2 - Function Call of the C++ specification, sub paragraph 2 indicates that:

Note: If a function or member function name is used, and name lookup (3.4) does not find a declaration of that name, the program is ill-formed. No function is implicitly declared by such a call. — end note

That is where the difference lies with C. This may however indicate a deeper problem: either you are using the wrong header, and linking with an OpenGL implementation which support that function, or somehow, the header should contain that declaration and does not. My first guess would be that you should probably double check which OpenGL implementation you are effectively linking against, and whether the header you are using is the correct one.

However, it is also possible that this OpenGL standard keeps an definition of that function as an extension, or that the function is visible by the linker because the driver supports a higher standard or extension. Relying on that feature is of course not recommended without first checking that the corresponding extension is actually supported by the driver.

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