I am trying to run a program that uses OpenGL libraries but whenever I run the code, I get a list of errors that say similar things about failing to find the symbol definitions in the library. I have checked the VC++ Directories and the Linker in the Properties Window and it looks like I have the correct path for OpenGL folders, such as GLFW and GLAD. Do these folders have to be in the same directory as my project? Help would be appreciated. Here is my code:
Source.cpp
#include <iostream> // cout, cerr
#include <cstdlib> // EXIT_FAILURE
#include <GL/glew.h> // GLEW library
#include <GLFW/glfw3.h> // GLFW library
using namespace std; // Uses the standard namespace
// Unnamed namespace
namespace
{
const char* const WINDOW_TITLE = "Tutorial 2.8"; // Macro for window title
// Variables for window width and height
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
// Stores the GL data relative to a given mesh
struct GLMesh
{
GLuint vao; // Handle for the vertex array object
GLuint vbos[2]; // Handles for the vertex buffer objects
GLuint nIndices; // Number of indices of the mesh
};
// Main GLFW window
GLFWwindow* gWindow = nullptr;
// Triangle mesh data
GLMesh gMesh;
// Shader program
GLuint gProgramId;
}
/* User-defined Function prototypes to:
* initialize the program, set the window size,
* redraw graphics on the window when resized,
* and render graphics on the screen
*/
bool UInitialize(int, char* [], GLFWwindow** window);
void UResizeWindow(GLFWwindow* window, int width, int height);
void UProcessInput(GLFWwindow* window);
void UCreateMesh(GLMesh& mesh);
void UDestroyMesh(GLMesh& mesh);
void URender();
bool UCreateShaderProgram(const char* vtxShaderSource, const char* fragShaderSource, GLuint& programId);
void UDestroyShaderProgram(GLuint programId);
// Vertex Shader Program Source Code
const char* vertexShaderSource = "#version 440 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec4 colorFromVBO;\n"
"out vec4 colorFromVS;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
" colorFromVS = colorFromVBO;\n"
"}\n\0";
// Fragment Shader Program Source Code
const char* fragmentShaderSource = "#version 440 core\n"
"in vec4 colorFromVS;\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = colorFromVS;\n"
"}\n\0";
// main function. Entry point to the OpenGL program
int main(int argc, char* argv[])
{
if (!UInitialize(argc, argv, &gWindow))
return EXIT_FAILURE;
// Create the mesh
UCreateMesh(gMesh); // Calls the function to create the Vertex Buffer Object
// Create the shader program
if (!UCreateShaderProgram(vertexShaderSource, fragmentShaderSource, gProgramId))
return EXIT_FAILURE;
// Sets the background color of the window to black (it will be implicitely used by glClear)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// render loop
// -----------
while (!glfwWindowShouldClose(gWindow))
{
// input
// -----
UProcessInput(gWindow);
// Render this frame
URender();
glfwPollEvents();
}
// Release mesh data
UDestroyMesh(gMesh);
// Release shader program
UDestroyShaderProgram(gProgramId);
exit(EXIT_SUCCESS); // Terminates the program successfully
}
// Initialize GLFW, GLEW, and create a window
bool UInitialize(int argc, char* argv[], GLFWwindow** window)
{
// GLFW: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// GLFW: window creation
// ---------------------
* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
if (*window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return false;
}
glfwMakeContextCurrent(*window);
glfwSetFramebufferSizeCallback(*window, UResizeWindow);
// GLEW: initialize
// ----------------
// Note: if using GLEW version 1.13 or earlier
glewExperimental = GL_TRUE;
GLenum GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult)
{
std::cerr << glewGetErrorString(GlewInitResult) << std::endl;
return false;
}
// Displays GPU OpenGL version
cout << "INFO: OpenGL Version: " << glGetString(GL_VERSION) << endl;
return true;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
void UProcessInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
void UResizeWindow(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
// Functioned called to render a frame
void URender()
{
// Clear the background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Set the shader to be used
glUseProgram(gProgramId);
// Activate the VBOs contained within the mesh's VAO
glBindVertexArray(gMesh.vao);
// Draws the triangle
glDrawElements(GL_TRIANGLES, gMesh.nIndices, GL_UNSIGNED_SHORT, NULL); // Draws the triangle
// Deactivate the VAO
glBindVertexArray(0);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(gWindow); // Flips the the back buffer with the front buffer every frame.
}
// Implements the UCreateMesh function
void UCreateMesh(GLMesh& mesh)
{
// Specifies Normalized Device Coordinates (x,y,z) and color (r,g,b,a) for triangle vertices
GLfloat verts[] =
{
// The two triangles will be drawn using indices
// Left triangle indices: 0, 1, 2
// Right triangle indices: 3, 2, 4
// index 0
-0.5f, 0.0f, 0.0f, // top-first_third of the screen
1.0f, 0.0f, 0.0f, 1.0f, // red
// index 1
-1.0f, -1.0f, 0.0f, // bottom-left of the screen
0.0f, 0.0f, 1.0f, 1.0f, // blue
// index 2
0.0f, -1.0f, 0.0f, // bottom-center of the screen
0.0f, 1.0f, 0.0f, 1.0f, // green
// index 3
0.5f, 0.0f, 0.0f, // top-second_third of the screen
1.0f, 0.0f, 0.0f, 1.0f, // red
// index 4
1.0f, -1.0f, 0.0f, // bottom-right of the screen
0.0f, 1.0f, 0.0f, 1.0f // green
};
glGenVertexArrays(1, &mesh.vao); // we can also generate multiple VAOs or buffers at the same time
glBindVertexArray(mesh.vao);
// Create 2 buffers: first one for the vertex data; second one for the indices
glGenBuffers(2, mesh.vbos);
glBindBuffer(GL_ARRAY_BUFFER, mesh.vbos[0]); // Activates the buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // Sends vertex or coordinate data to the GPU
// Creates a buffer object for the indices
GLushort indices[] = { 0, 1, 2, 3, 2, 4 }; // Using index 2 twice
mesh.nIndices = sizeof(indices) / sizeof(indices[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vbos[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Creates the Vertex Attribute Pointer for the screen coordinates
const GLuint floatsPerVertex = 3; // Number of coordinates per vertex
const GLuint floatsPerColor = 4; // (r, g, b, a)
// Strides between vertex coordinates is 6 (x, y, r, g, b, a). A tightly packed stride is 0.
GLint stride = sizeof(float) * (floatsPerVertex + floatsPerColor);// The number of floats before each
// Creates the Vertex Attribute Pointer
glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, floatsPerColor, GL_FLOAT, GL_FALSE, stride, (char*)(sizeof(float) * floatsPerVertex));
glEnableVertexAttribArray(1);
}
void UDestroyMesh(GLMesh& mesh)
{
glDeleteVertexArrays(1, &mesh.vao);
glDeleteBuffers(2, mesh.vbos);
}
// Implements the UCreateShaders function
bool UCreateShaderProgram(const char* vtxShaderSource, const char* fragShaderSource, GLuint& programId)
{
// Compilation and linkage error reporting
int success = 0;
char infoLog[512];
// Create a Shader program object.
programId = glCreateProgram();
// Create the vertex and fragment shader objects
GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
// Retrive the shader source
glShaderSource(vertexShaderId, 1, &vtxShaderSource, NULL);
glShaderSource(fragmentShaderId, 1, &fragShaderSource, NULL);
// Compile the vertex shader, and print compilation errors (if any)
glCompileShader(vertexShaderId); // compile the vertex shader
// check for shader compile errors
glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShaderId, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
return false;
}
glCompileShader(fragmentShaderId); // compile the fragment shader
// check for shader compile errors
glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShaderId, sizeof(infoLog), NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
return false;
}
// Attached compiled shaders to the shader program
glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
glLinkProgram(programId); // links the shader program
// check for linking errors
glGetProgramiv(programId, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(programId, sizeof(infoLog), NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
return false;
}
glUseProgram(programId); // Uses the shader program
return true;
}
void UDestroyShaderProgram(GLuint programId)
{
glDeleteProgram(programId);
}
Error
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp_glewInit referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp_glewGetErrorString referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwInit referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwTerminate referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwWindowHint referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwCreateWindow referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwWindowShouldClose referenced in function main 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwSetWindowShouldClose referenced in function "void __cdecl UProcessInput(struct GLFWwindow *)" (?UProcessInput@@YAXPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwSetFramebufferSizeCallback referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwPollEvents referenced in function main 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwGetKey referenced in function "void __cdecl UProcessInput(struct GLFWwindow *)" (?UProcessInput@@YAXPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwMakeContextCurrent referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol glfwSwapBuffers referenced in function "void __cdecl URender(void)" (?URender@@YAXXZ) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewBindBuffer referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewBufferData referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewDeleteBuffers referenced in function "void __cdecl UDestroyMesh(struct `anonymous namespace'::GLMesh &)" (?UDestroyMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewGenBuffers referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewAttachShader referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewCompileShader referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewCreateProgram referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewCreateShader referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewDeleteProgram referenced in function "void __cdecl UDestroyShaderProgram(unsigned int)" (?UDestroyShaderProgram@@YAXI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewEnableVertexAttribArray referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewGetProgramInfoLog referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewGetProgramiv referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewGetShaderInfoLog referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewGetShaderiv referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewLinkProgram referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewShaderSource referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewUseProgram referenced in function "bool __cdecl UCreateShaderProgram(char const *,char const *,unsigned int &)" (?UCreateShaderProgram@@YA_NPEBD0AEAI@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewVertexAttribPointer referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewBindVertexArray referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewDeleteVertexArrays referenced in function "void __cdecl UDestroyMesh(struct `anonymous namespace'::GLMesh &)" (?UDestroyMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp___glewGenVertexArrays referenced in function "void __cdecl UCreateMesh(struct `anonymous namespace'::GLMesh &)" (?UCreateMesh@@YAXAEAUGLMesh@?A0x22e96cf7@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Error LNK2019 unresolved external symbol __imp_glewExperimental referenced in function "bool __cdecl UInitialize(int,char * * const,struct GLFWwindow * *)" (?UInitialize@@YA_NHQEAPEADPEAPEAUGLFWwindow@@@Z) 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\Source.obj 1
Warning LNK4272 library machine type 'x86' conflicts with target machine type 'x64' 2D_Triangles C:\Users\marce\Downloads\OpenGL\OpenGL\GLEW\lib\Release\Win32\glew32.lib 1
Warning LNK4272 library machine type 'x86' conflicts with target machine type 'x64' 2D_Triangles C:\Users\marce\Downloads\OpenGL\OpenGL\GLFW\lib-vc2019\glfw3.lib 1
Error LNK1120 35 unresolved externals 2D_Triangles C:\Users\marce\source\repos\2D_Triangles\x64\Debug\2D_Triangles.exe 1