0

I started to learn OpenGL. In the training material that I use they run code on windows. I tried to run the same C++ code in VS Code on macOS Catalina 10.15.7:

#include <stdio.h>

#include </usr/local/Cellar/glew/2.2.0_1/include/GL/glew.h> 
/* The original code from the training here was **#include <GL\glew.h>** 
but it didnt work for me so I changed the path and the error gone.*/

#include </usr/local/Cellar/glfw/3.3.6/include/GLFW/glfw3.h>
/* The original code from the training here was **#include <GLFW\glfw3.h>** 
but it didnt work for me so I changed the path and the error gone. */

// Window dimensions
const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
    // Initialise GLFW
    if (!glfwInit())
    {
        printf("GLFW initialisation failed!");
        glfwTerminate();
        return 1;
    }

    // Setup GLFW window properties
    // OpenGL version
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    // Core Profile = No Backwards Compatibility
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    // Allow Forward Compatbility
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    // Create the window
    GLFWwindow *mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "Test Window", NULL, NULL);
    if (!mainWindow)
    {
        printf("GLFW window creation failed!");
        glfwTerminate();
        return 1;
    }

    // Get Buffer Size information
    int bufferWidth, bufferHeight;
    glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);

    // Set context for GLEW to use
    glfwMakeContextCurrent(mainWindow);

    // Allow modern extension features
    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK)
    {
        printf("GLEW initialisation failed!");
        glfwDestroyWindow(mainWindow);
        glfwTerminate();
        return 1;
    }

    // Setup Viewport size
    glViewport(0, 0, bufferWidth, bufferHeight);

    // Loop until window closed
    while (!glfwWindowShouldClose(mainWindow))
    {
        // Get + Handle user input events
        glfwPollEvents();

        // Clear window
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(mainWindow);
    }

    return 0;
}

When I run the code above, I got this error:

    Undefined symbols for architecture x86_64:
  "_glClear", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glClearColor", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glViewport", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glewExperimental", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glewInit", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwCreateWindow", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwDestroyWindow", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwGetFramebufferSize", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwInit", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwMakeContextCurrent", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwPollEvents", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwSwapBuffers", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwTerminate", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwWindowHint", referenced from:
      _main in Test_2_C++-82ae26.o
  "_glfwWindowShouldClose", referenced from:
      _main in Test_2_C++-82ae26.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I run this code in Xcode on a Mac M1 (macOS Monterey), the program works and the application starts, although it throws many other errors. But I need to be able to run this code in VS Code.

I searched for the solution in the internet but I couldn't implement any of the advices. Maybe because I am new to this and just didn't understand what needs to be done. Could someone please explain in details step by step what should I do to fix this issue. Thanks!

Gelios
  • 15
  • 7
  • This are linker errors, not compiler errors. Which libraries have you included? What's the command you use to compile/link your code? – BDL Jan 12 '22 at 11:51
  • Are you sure, you build library for x86_64? – Deumaudit Jan 12 '22 at 11:52
  • 2
    You're not linking with the OpenGL libraries. What does your `tasks.json` file look like? What options and arguments are you passign to the compiler front-end program? – Some programmer dude Jan 12 '22 at 11:53
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Rabbid76 Jan 12 '22 at 11:55
  • @BDL, I downloaded glew and glfw, then I created .cpp file in VS Code, pasted the code and run it. Thats all I did. I missed something? – Gelios Jan 12 '22 at 12:01
  • 1
    You have to link against the appropriate libraries. Including the header files is not enough. I'm sure you also added some "Additional Dependencies" in Visual Studio. – BDL Jan 12 '22 at 12:09
  • ***I missed something?*** Yes. You were supposed to edit your tasks.json and add the linker settings to link to the opengl, glew and glfw libraries. – drescherjm Jan 12 '22 at 12:55
  • @drescherjm, I built the program again according to this tutorial https://code.visualstudio.com/docs/cpp/config-clang-mac#_build-helloworldcpp. I did everything as written there. The problem still exists. I couldn't find information on how to link the opengl, glew and glfw libraries. I think this needs to be done in the tasks.json file. Could you tell me please what code needs to be added there. – Gelios Jan 13 '22 at 06:03
  • The documentation does not explain how to link to external libraries. Yes, it does need to be done in `tasks.json` you edit the `"args":` adding your additional linker settings. I believe this question can help with determining the settings on macOS: [https://stackoverflow.com/questions/18550265/compiling-a-c-program-that-uses-opengl-in-mac-os-x](https://stackoverflow.com/questions/18550265/compiling-a-c-program-that-uses-opengl-in-mac-os-x) – drescherjm Jan 13 '22 at 12:57

0 Answers0