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!