Cannot create a simple colored window in main()

I have been trying for days to create a simple window with a blue background, but for some unknown reason, I only get a window with black background. I am compiling a small program using the following 3 files.

  • main.cpp
#include "wind.hpp"

int main()
{
    wind display;
    display.test();
}
  • wind.hpp
#include <GL/glew.h>
#include <GLFW/glfw3.h>

class wind
{
public:

	void test();

};
  • wind.cpp
#include "wind.hpp"

void wind::test()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);

    glfwMakeContextCurrent(window);

    glewInit();

    std::cout << "Version: " << glGetString(GL_VERSION) << std::endl;

    static unsigned frameCount = 0;

    while (true)
    {
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);


        std::cout << ++frameCount << '\r';

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
}

The code looks flawless. I compile a program containing these 3 files and the output is a window filled with black.

System: Ubuntu 18.04.5 LTS.
Tools used: OpenGL (color), GLFW (for the window), GLEW (for OpenGL calls), CMake (for the building).

Some facts:

  • glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL) != NULL
  • glewInit() == GLEW_OK
  • In the console, the only output I get is Version: . Any subsequent std::cout seems to be not executed (no output).
  • If I delete the line std::cout << "Version: " << glGetString(GL_VERSION) << std::endl; I get the frameCount values in the console (so, main loop is running). But the window is still black.
  • If I create a static library with “wind.hpp” and “wind.cpp” (say “libwind.a”), and then I link it to my program (“main.cpp”), and compile… I get the blue window. But I can’t understand why it works this way and not the other…

What drivers are you using?
I did some simple vulkan programs, and the default nouveau drivers failed terribly.
Of course OpenGL is much longer around, so this seems less likely, but maybe you still want to check your drivers?

My drivers are fully updated. In fact, as I said, I can use OpenGL effectively when I do it through a static library.

If I create a static library with “wind.hpp” and “wind.cpp” (say “libwind.a”), and then I link it to my program (“main.cpp”), and compile… I get the blue window. But I can’t understand why it works this way and not the other…

There is clearly an issue. Can you check for errors at each of your lines ? I don’t use either glew or glfw, but I’m pretty sure most of these calls can return some error codes. You should never assume a function will never fail. My guess is that you don’t have a valid window.

I have checked for error at three key lines: GLFW initialization, GLFW window creation, and GLEW initialization.

    if(glfwInit() == GLFW_FALSE) std::cout << "GLFW initialization error" << std::endl;
    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
    if(window == nullptr) std::cout << "GLFW window creation error" << std::endl;
    unsigned result = glewInit();
    if(result != GLEW_OK) std::cout << "GLEW error" << std::endl;

However, none of these warnings were output, which tells me that apparently there’re no errors…
This is really frustrating, I cannot understand why I can’t do this, and I don’t know where to look to solve this because everything looks correct.

OK. So I tried your program and it simply does not compile. And for sure, you never included iostream. So the question is how did you make it compile ?

You’re right, #include <iostream> doesn’t appear in the code I provided, but I put it in my “wind.cpp” to make it compile. I would have edited my question to include it, but I was not allowed to do that.
Originally, I had no std::cout statements in my code, so iostream was not needed. Later I included two of these statements, but I forgot to include the #include <iostream line in the code that I provided. Anyway, the problem concerning the black window remains.

Given that you seem to have different results depending upon how you link it, I’d suspect GLEW.

On Windows, note that you need to define GLEW_STATIC if you’re linking against a static version of GLEW but not if you’re linking against a DLL. Also, if you use GLEW as a static library and your code consists of multiple binaries (EXEs/DLLs), it needs to be initialised in each.

Given that you don’t seem to be using any 3.3-specific features, I’d remove the version selection code. If you specifically want a 3.3 context, you should also specify a profile.

As I said, I’m using Ubuntu. So I don’t have to define GLEW_STATIC, but I do so in Windows. I am planning to use the core-profile mode, but the code I provide is simplified.
On the other hand, I have new information: I have compiled my program using GLAD, instead of GLEW, and this time I got the blue window. So, for some unknown reason, my program works with GLAD, but not with GLEW.

With including iostream, your program works on my machine.
You might be running a non-stable or buggy version of glew. You can try to report it to the maintainers of glew at Ubuntu.

Unless things have changed recently, you typically don’t need GLEW on Linux. The OpenGL library normally exports the entire API (for the OpenGL version for which it was compiled). So you just need to #define GL_GLEXT_PROTOTYPES to enable the function declarations.

About the only reason to use a loader is if you want to use features from the latest version if they’re available but produce binaries which are compatible with older versions of the OpenGL library.

On Windows, opengl32.dll only exports the OpenGL 1.1 API and you have to dynamically load any functions which were added in later revisions or extensions.