0

I'm trying to build both an opengl script and a qt GUI window at the same time for a small project of mine. Even after linking the glad, glfw header files and the glfw3.lib file to my compiler settings and importing glad.c to my project. Here is the code where the error occurs

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "shader.h"
#include <iostream>

#include <QtWidgets/QApplication>
#include "login.h"

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main(int argc, char* argv[]) {
    // initialize GLFW
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    #ifdef __APPLE__
        glfwWindowHint(GLFW_OPENGL_FOWARD_COMPAT, GL_TRUE);
    #endif

    // creating the glfw window
        GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "aprende opengl", NULL, NULL);
        
        // if the window does not initialize
        if (window == NULL) {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

        // glad: load all openGL pointers
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
            std::cout << "Failed to initialize GLAD" << std::endl;
            return -1;
        }

        //builds the shader with our header file
        // you can name your shader files however you like
        shader ourShader("3.3.shader.vs", "3.3.shader.fs");

        // set up vertex data (and buffer(s)) and configure vertex attributes
        // ------------------------------------------------------------------
        float vertices[] = {
            // positions         // colors
             0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,  // bottom right
            -0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  // bottom left
             0.0f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f   // top 
        };

        unsigned int VBO, VAO;
        // here are the identifiers that fail to be recognized
        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);

        // bind the Vertex Array Object first, then bind and set vertex
        // buffer(s), and then configure vertex attributes(s).
        glBindVertexArray(VAO);

        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        // position attributes
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);

        // color attributes
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);

        // You can unbind the VAO afterwards so other VAO calls won't accidentally 
        // modify this VAO, but this rarely happens. Modifying other VAOs 
        // requires a call to glBindVertexArray anyways so we generally 
        // don't unbind VAOs (nor VBOs) when it's not directly necessary.
        // glBindVertexArray(0);

        // render loop
        while (!glfwWindowShouldClose(window)) {
            // input
            processInput(window);

            // render
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            // renderiza el triangulo
            ourShader.use();
            glBindVertexArray(VAO);
            glDrawArrays(GL_TRIANGLES, 0, 3);

            // GLFW: swap buffers and poll IO events (mouse pressed, etc)
            glfwSwapBuffers(window);
            glfwPollEvents();
        }

        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);
        // glfw: terminate, clearing all previously allocated GLFW resources.
        // ------------------------------------------------------------------
        glfwTerminate();

        // QT constructor stuff
        QApplication a(argc, argv);
        login w;
        w.show();
        return a.exec();

}

void processInput(GLFWwindow* window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    }
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}

Here is the output (my project is called "simulador_7"):

>------ Build started: Project: simulador_7, Configuration: Debug x64 ------
1>shader_class.cpp
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader.h(42,35): warning C4101: 'e': unreferenced local variable
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\minwindef.h(130,1): warning C4005: 'APIENTRY': macro redefinition
1>C:\Users\Jose Miguel\Documents\simulador\librerias\include\glad\glad.h(32): message : see previous definition of 'APIENTRY'
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(60,3): error C3861: 'glGenVertexArrays': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(61,3): error C3861: 'glGenBuffers': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(65,3): error C3861: 'glBindVertexArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(67,3): error C3861: 'glBindBuffer': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(68,3): error C3861: 'glBufferData': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(71,3): error C3861: 'glVertexAttribPointer': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(72,3): error C3861: 'glEnableVertexAttribArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(75,3): error C3861: 'glVertexAttribPointer': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(76,3): error C3861: 'glEnableVertexAttribArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(90,4): error C3861: 'glClearColor': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(91,4): error C3861: 'glClear': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(95,4): error C3861: 'glBindVertexArray': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(96,4): error C3861: 'glDrawArrays': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(104,3): error C3861: 'glDeleteVertexArrays': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(105,3): error C3861: 'glDeleteBuffers': identifier not found
1>C:\Users\Jose Miguel\Documents\simulador\simulador_7\simulador_7\simulador_7\shader_class.cpp(126,2): error C3861: 'glViewport': identifier not found
1>Done building project "simulador_7.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Why is this happening? I've linked the same libraries again and again with no avail, cleaned the project. My other practice projects of openGL link to their corresponding libraries/includes without problem. I have included a photo of both my libraries and include folders here[1]enter image description here. As you can see, I have included both glad/glfw3 directories as well as QT corresponding directories. Could there be an overlap between these folders? Before I added the include/QTWidgets path the glad headers worked just fine.

Link to images here(libraries)1 and here(includes)2

https://imgur.com/a/8GG995y[![here][1]][1]

ayy_chemixd
  • 113
  • 10
  • These are compiler errors not linking errors. – john Jul 04 '20 at 04:48
  • thank you @john, what can I do? – ayy_chemixd Jul 04 '20 at 04:51
  • Here's a tip since you seem to be using Visual Studio. By default Visual Studio presents errors in the 'Error List' tab. What's brain dead about this is that it presents the errors out of order, and almost always what you need to see are the first errors, since subsequent errors are a often a knock on effect of the first errors. So instead of looking at the Error List tab switch to the Output tab, those errors are in order. Take the first three or four of those errors and paste them into the question. – john Jul 04 '20 at 04:52
  • Are you sure you're looking at the correct cpp file? The error message states something about shader_class.cpp but the code here seems to be you main.cpp? In addition, have you checked that glad.h contains these definitions? – BDL Jul 04 '20 at 07:18
  • thank you @john I have edited the question to include the original output instead of that horrible list of errors I had previously. – ayy_chemixd Jul 04 '20 at 14:34
  • Sorry about not being clear enough @BDL, the file that I included in the question (that does include the main function) is the main file, so to speak. And yes glad.h does include these definitions. I have noticed that glad.h works as intended when I delete "#include login.h". perhaps the compiler gets confused because of so many include folders? – ayy_chemixd Jul 04 '20 at 14:34
  • 1
    OK so there' something strange going on, because many of your errors refer to `shader_class.cpp` even though you say that's not the file you've posted above. So that would indicate that one of your header file must be including `shader_class.cpp`. That's a real red flag, you should never do that. But I don't think that's the most serious issue. – john Jul 04 '20 at 14:45
  • The real issue is the redefinition of macro `APIENTRY`. We can see that two header files are defining that macro and in different ways. One file is `glad.h`, the other is `minwindef.h` which is part of the Windows runtime. I can't say for sure what's going on here, but it's clear these two header file are not compatible. I would look to remove the Windows Runtime header file, if you can. From what you say it seems to be something in "login.h" that directly or (more likely) indirectly is including this file.. – john Jul 04 '20 at 14:50
  • 1
    All in all it seems you header files are messed up in at least a couple of different ways. Hard for me to tell at this distance exactly what the problem is, but try and simplify your header files so that they include the minimum that they need to. And try to separate out any header files that are incompatible (or remove them entirely if possible). – john Jul 04 '20 at 14:52
  • @john thank you very much. it seems the library QtWidgets is incompatible with glad.h and viceversa (since thats the only thing I'm including inside login.h). I'll try to look up another compatible library. So far it seems QOpenGLWidget is what I'm looking for. Bummer, but it it what it is. Thank you again. – ayy_chemixd Jul 04 '20 at 14:56
  • It may be possible to use both OpenGL and QtWdigets in the same program, as long as you keep the header files apart. Awkward but possible. – john Jul 04 '20 at 15:25

0 Answers0