I installed GLWF succesfully. I can prove that since this program compiles:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Then i used these commands in order to install glad:
git clone https://github.com/Dav1dde/glad.git
cd glad
cmake ./
make
sudo cp -a include /usr/local/
When i tried to execute this program:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
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;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
// glfw: initialize and configure
// ------------------------------
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_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
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 function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// ------------------------------------
// vertex shader
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// 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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// 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);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(shaderProgram);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(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 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);
}
I got these errors:
/usr/bin/ld: /tmp/ccDMX5H7.o: in function `main':
triangle.c:(.text+0xe9): undefined reference to `gladLoadGLLoader'
/usr/bin/ld: triangle.c:(.text+0x12b): undefined reference to `glad_glCreateShader'
/usr/bin/ld: triangle.c:(.text+0x13f): undefined reference to `glad_glShaderSource'
/usr/bin/ld: triangle.c:(.text+0x162): undefined reference to `glad_glCompileShader'
/usr/bin/ld: triangle.c:(.text+0x173): undefined reference to `glad_glGetShaderiv'
/usr/bin/ld: triangle.c:(.text+0x19a): undefined reference to `glad_glGetShaderInfoLog'
/usr/bin/ld: triangle.c:(.text+0x1fd): undefined reference to `glad_glCreateShader'
/usr/bin/ld: triangle.c:(.text+0x211): undefined reference to `glad_glShaderSource'
/usr/bin/ld: triangle.c:(.text+0x234): undefined reference to `glad_glCompileShader'
/usr/bin/ld: triangle.c:(.text+0x245): undefined reference to `glad_glGetShaderiv'
/usr/bin/ld: triangle.c:(.text+0x26c): undefined reference to `glad_glGetShaderInfoLog'
/usr/bin/ld: triangle.c:(.text+0x2cf): undefined reference to `glad_glCreateProgram'
/usr/bin/ld: triangle.c:(.text+0x2de): undefined reference to `glad_glAttachShader'
/usr/bin/ld: triangle.c:(.text+0x2f7): undefined reference to `glad_glAttachShader'
/usr/bin/ld: triangle.c:(.text+0x310): undefined reference to `glad_glLinkProgram'
/usr/bin/ld: triangle.c:(.text+0x321): undefined reference to `glad_glGetProgramiv'
/usr/bin/ld: triangle.c:(.text+0x348): undefined reference to `glad_glGetProgramInfoLog'
/usr/bin/ld: triangle.c:(.text+0x3ab): undefined reference to `glad_glDeleteShader'
/usr/bin/ld: triangle.c:(.text+0x3bc): undefined reference to `glad_glDeleteShader'
/usr/bin/ld: triangle.c:(.text+0x4b9): undefined reference to `glad_glGenVertexArrays'
/usr/bin/ld: triangle.c:(.text+0x4d1): undefined reference to `glad_glGenBuffers'
/usr/bin/ld: triangle.c:(.text+0x4e9): undefined reference to `glad_glGenBuffers'
/usr/bin/ld: triangle.c:(.text+0x501): undefined reference to `glad_glBindVertexArray'
/usr/bin/ld: triangle.c:(.text+0x512): undefined reference to `glad_glBindBuffer'
/usr/bin/ld: triangle.c:(.text+0x528): undefined reference to `glad_glBufferData'
/usr/bin/ld: triangle.c:(.text+0x54b): undefined reference to `glad_glBindBuffer'
/usr/bin/ld: triangle.c:(.text+0x561): undefined reference to `glad_glBufferData'
/usr/bin/ld: triangle.c:(.text+0x584): undefined reference to `glad_glVertexAttribPointer'
/usr/bin/ld: triangle.c:(.text+0x5ad): undefined reference to `glad_glEnableVertexAttribArray'
/usr/bin/ld: triangle.c:(.text+0x5bb): undefined reference to `glad_glBindBuffer'
/usr/bin/ld: triangle.c:(.text+0x5ce): undefined reference to `glad_glBindVertexArray'
/usr/bin/ld: triangle.c:(.text+0x607): undefined reference to `glad_glClearColor'
/usr/bin/ld: triangle.c:(.text+0x630): undefined reference to `glad_glClear'
/usr/bin/ld: triangle.c:(.text+0x63e): undefined reference to `glad_glUseProgram'
/usr/bin/ld: triangle.c:(.text+0x64f): undefined reference to `glad_glBindVertexArray'
/usr/bin/ld: triangle.c:(.text+0x660): undefined reference to `glad_glDrawElements'
/usr/bin/ld: triangle.c:(.text+0x696): undefined reference to `glad_glDeleteVertexArrays'
/usr/bin/ld: triangle.c:(.text+0x6ae): undefined reference to `glad_glDeleteBuffers'
/usr/bin/ld: triangle.c:(.text+0x6c6): undefined reference to `glad_glDeleteBuffers'
/usr/bin/ld: triangle.c:(.text+0x6de): undefined reference to `glad_glDeleteProgram'
/usr/bin/ld: /tmp/ccDMX5H7.o: in function `framebuffer_size_callback(GLFWwindow*, int, int)':
triangle.c:(.text+0x764): undefined reference to `glad_glViewport'
collect2: error: ld returned 1 exit status
I compiled the program with this command:
g++ triangle.c -o triangle -Wall -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor
EDIT:
I used these two resources to perform the installation of the libraries:
https://www.youtube.com/watch?v=ZwaHQ6c-ma0
https://shnoh171.github.io/gpu%20and%20gpu%20programming/2019/08/26/installing-glfw-on-ubuntu.html
Glad consists of multiple files:
A glad.h header file, which you have to include into your files
A glad.c source file which you have to compile together with your own source files.
Judging from your error message, you forgot to also compile the glad.c source file. You can adapt your command line to something like:
g++ triangle.c <GLADPATH>/glad.c -o triangle -Wall -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor
Replace <GLADPATH> with the path to the glad directory
There are two essential components while using gpp compiler, that allow to run OpenGL programs:
LINKER FILE (for example glad.c)
THE FILE THAT HAS TO BE COMPILED APART FROM YOUR MAIN PROGRAM.
The easiest way to handle the linker file is to move it to the folder where your main.cpp is located. Then compile the program in a following way:
g++ main.cpp glad.c -lglfw -lGL -ldl
HEADER FILE (for example glad.h)
A file which is defined in the curly brackets <> of the #include directive. The path that is placed in curly brackets <> needs to be continuation of the directory that is placed in one of the following paths:
$ cpp -v
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/9/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
For example, when #include <glad/glad.h> is specified in your code, glad.h should be located in:
/usr/include/glad/glad.h
Apart from that, you don't need to use the libraries like :
-lGLU -lglut -lGLEW -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lXinerama -lXcursor. Use the minimum amount of "-l's" and once you compile linker file with main file, there's no need to define glad in the form of static library (-lglad).
Thus, the answer is:
g++ triangle.c glad.c -lglfw -lGL -ldl
and you should get orange square on the green background (if you don't forget about header and linker files located in the right folders)
Related
I was following an OpenGL tutorial when I ran into an issue where I wasn't getting the expected outcome. I was expecting to see a triangle in a window, but instead, I got an empty window. I have made many changes to the code to help it run on my machine, but none fixed the issue. Some of these changes include adding:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
to request the core profile context, adding a VAO with the VBO, I even downloaded the code from the tutorial, but to no avail. Does anyone know a way to fix my issue?
Code:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
static unsigned int CompileShader(unsigned int type, const std::string& source){
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE){
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const std::string& vertexshader, const std::string& fragmentshader){
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexshader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentshader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK){
std::cout << "REEEEEEEEEEE" << std::endl;
}
float vertices[6] = {
-0.5f, -0.5f,
0.0f, 0.5f,
-0.5f, -0.5f
};
unsigned int buf;
glGenBuffers(1, &buf);
glBindBuffer(GL_ARRAY_BUFFER, buf);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
std::string vertexshader =
"#version 330 core\n"
"\n"
"in vec4 position;\n"
"out vec4 color;\n"
"\n"
"void main(){\n"
" gl_Position = position;\n"
" color = position;\n"
"}\n";
std::string fragmentshader =
"#version 330 core\n"
"\n"
"in vec4 in_color;\n"
"out vec4 color;\n"
"\n"
"void main(){\n"
" color = in_color;\n"
"}\n";
unsigned int shader = CreateShader(vertexshader, fragmentshader);
glUseProgram(shader);
glClearColor(0.33f, 0.33f, 0.33f, 1.0f);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
CMakeLists:
cmake_minimum_required (VERSION 3.5)
if(POLICY CMP0072)
cmake_policy(SET CMP0072 NEW)
else(NOT POLICY CMP0072)
message(STATUS "Error")
endif(POLICY CMP0072)
project (opengl-learning)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR})
set (GCC_COVERAGE_LINK_FLAGS "-lglfw3 -pthread -ldl -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lGLEW -lGLU -lGL")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
file (GLOB source_files "${source_dir}/*.cpp")
add_executable (opengl-learning ${source_files})
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
target_link_libraries(opengl-learning ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
Versions (requesting core profile)
glGetString(GL_VERSION) >> 3.3 (Core Profile) Mesa 18.3.6
glGetString(GL_SHADING_LANGUAGE_VERSION) >> 3.30
OS: Linux Debian 10
If any extra information is required, feel free to ask.
When you use a core profile OpenGL Context you must create a Vertex Array Object:
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buf);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
The default Vertex Array Object (0) is only valid using a compatibility profile OpenGL context:
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
I recommend reading Vertex Specification.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 months ago.
I'm using codeblocks and MinGW and windows 10.
I put the directory for the include folder for glad and GLFW under compiler under search directories. I put the directory for the lib folder for GLFW under compiler under search directories. I also put libopengl32.a, libglfw3.a and libglfw3dll.a under Linker settings.
Errors:
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_CreateDCW'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_GetDeviceCaps'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_DeleteDC'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_GetDeviceCaps'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_GetDeviceCaps'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_GetDeviceCaps'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_CreateDCW'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_GetDeviceGammaRamp'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_DeleteDC'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_CreateDCW'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_SetDeviceGammaRamp'|
C:\GL\glfw\lib\libglfw3.a(win32_monitor.c.obj):win32_monitor.c|| undefined reference to `__imp_DeleteDC'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_CreateDIBSection'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_CreateBitmap'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_DeleteObject'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_DeleteObject'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_CreateRectRgn'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_DeleteObject'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_DeleteObject'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_CreateDIBSection'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_CreateBitmap'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_DeleteObject'|
C:\GL\glfw\lib\libglfw3.a(win32_window.c.obj):win32_window.c|| undefined reference to `__imp_DeleteObject'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_SwapBuffers'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_ChoosePixelFormat'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_SetPixelFormat'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_DescribePixelFormat'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_SetPixelFormat'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_DescribePixelFormat'|
C:\GL\glfw\lib\libglfw3.a(wgl_context.c.obj):wgl_context.c|| undefined reference to `__imp_DescribePixelFormat'|
||error: ld returned 1 exit status|
||=== Build failed: 31 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Code:
#include <glad/glad.h>
#include <glfw3.h>
#include <iostream>
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;
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 0 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
#ifdef __APPLE__
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow( SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL );
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 function pointers
// ---------------------------------------
if( !gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress ) )
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// ------------------------------------
// vertex shader
int vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &vertexShaderSource, NULL );
glCompileShader( vertexShader );
// check for shader compile errors
int success;
char infoLog[ 512 ];
glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &success );
if( !success )
{
glGetShaderInfoLog( vertexShader, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
int fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragmentShaderSource, NULL );
glCompileShader( fragmentShader );
// check for shader compile errors
glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &success );
if( !success )
{
glGetShaderInfoLog( fragmentShader, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
int shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, vertexShader );
glAttachShader( shaderProgram, fragmentShader );
glLinkProgram( shaderProgram );
// check for linking errors
glGetProgramiv( shaderProgram, GL_LINK_STATUS, &success );
if( !success )
{
glGetProgramInfoLog( shaderProgram, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader( vertexShader );
glDeleteShader( fragmentShader );
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
unsigned int VBO, VAO;
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 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof( float ), (void*)0 );
glEnableVertexAttribArray( 0 );
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer( GL_ARRAY_BUFFER, 0 );
// 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 );
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while( !glfwWindowShouldClose( window ) )
{
// input
// -----
processInput( window );
// render
// ------
glClearColor( 0.2f, 0.3f, 0.3f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
// draw our first triangle
glUseProgram( shaderProgram );
// seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glBindVertexArray( VAO );
glDrawArrays( GL_TRIANGLES, 0, 3 );
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers( window );
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays( 1, &VAO );
glDeleteBuffers( 1, &VBO );
glDeleteProgram( shaderProgram );
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput( 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 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 );
}
I fixed it by linking my project to libgdi32.a, which should be located in the lib folder in your MinGW install folder.
I'm not sure how it works on Code::Blocks, but on the command line with g++ you just add -lgdi32 to your command line options.
I already tried altering the c_cpp_properties file to include the path where glad.h is installed, like this:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${default}",
"/home/jose_miguel/Desktop/simulador/librerias"
],
"defines": [],
"compilerPath":
"/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
I have even copy-pasted the glad.h file to usr/bin, where most executables are (including g++) and vs code will still not detect the file. The error being "glad.h: no such file or directory". Even intellisense brings up the file when you're writing it. Could someone please point me in the right direction.
I will post my code for reference's sake, it's a simple triangle in opengl:
#include <glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
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;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
// glfw: initialize and configure
// ------------------------------
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_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
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 function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// ------------------------------------
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
unsigned int VBO, VAO;
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);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// 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);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(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 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);
}
Just got this to work for me, all answers I've found are way more complicated than they need to be.
tl;dr: add glad.c to your g++ compilation command in tasks.json (example below)
As per this documentation https://learnopengl.com/Getting-started/Creating-a-window
Go to the GLAD web service, make sure the language is set to C++, and
in the API section select an OpenGL version of at least 3.3 (which is
what we'll be using; higher versions are fine as well). Also make sure
the profile is set to Core and that the Generate a loader option is
ticked. Ignore the extensions (for now) and click Generate to produce
the resulting library files.
GLAD by now should have provided you a zip file containing two include
folders, and a single glad.c file. Copy both include folders (glad and
KHR) into your include(s) directoy (or add an extra item pointing to
these folders), and add the glad.c file to your project.
Once downloaded, add the include folders usr/local/include
cd glad/include
cp -r glad/ KHR/ /usr/include/
and the glad.c file to the root of the project in vscode.
Then in your tasks.json file, it should look something like this
{
"tasks": [
{
...
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"glad.c",
"-lglfw",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
...
}
],
"version": "2.0.0"
}
So you have to include the glad.c file in your compilation command.
And of course import the glad header before the glfw one.
#include <glad/glad.h>
#include <GLFW/glfw3.h>
I just download this demo, create a cocoa project, add frameworks OpenGL, GLKit, GLUT, now it can compile, but I got the linker error:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any thoughts??
Here is the cpp file I added to the project (and I didn't do anything to the files the project already had, like ViewController.* , AppDelegate.*) :
//
// geometryFeedback.cpp
//
//
#include "stdio.h"
#include <OpenGL/gl3.h>
#include <GLUT/glut.h>
// Shader macro
#define GLSL(src) "#version 400 core\n" #src
// Vertex shader
const GLchar* vertexShaderSrc = GLSL(
in float inValue;
out float geoValue;
void main() {
geoValue = sqrt(inValue);
}
);
// Geometry shader
const GLchar* geoShaderSrc = GLSL(
layout(points) in;
layout(triangle_strip, max_vertices = 3) out;
in float[] geoValue;
out float outValue;
void main() {
for (int i = 0; i < 3; i++) {
outValue = geoValue[0] + i;
EmitVertex();
}
EndPrimitive();
}
);
int main() {
// Window
// Compile shaders
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSrc, nullptr);
glCompileShader(vertexShader);
GLuint geoShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geoShader, 1, &geoShaderSrc, nullptr);
glCompileShader(geoShader);
// Create program and specify transform feedback variables
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, geoShader);
const GLchar* feedbackVaryings[] = { "outValue" };
glTransformFeedbackVaryings(program, 1, feedbackVaryings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glUseProgram(program);
// Create VAO
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create input VBO and vertex format
GLfloat data[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
GLint inputAttrib = glGetAttribLocation(program, "inValue");
glEnableVertexAttribArray(inputAttrib);
glVertexAttribPointer(inputAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0);
// Create transform feedback buffer
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data) * 3, nullptr, GL_STATIC_READ);
// Perform feedback transform
glEnable(GL_RASTERIZER_DISCARD);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo);
glBeginTransformFeedback(GL_TRIANGLES);
glDrawArrays(GL_POINTS, 0, 5);
glEndTransformFeedback();
glDisable(GL_RASTERIZER_DISCARD);
glFlush();
// Fetch and print results
GLfloat feedback[15];
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(feedback), feedback);
for (int i = 0; i < 15; i++) {
printf("%f\n", feedback[i]);
}
return 0;
}
The Linker Information:
Ld /Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Products/Debug/TransformFeedback.app/Contents/MacOS/TransformFeedback normal x86_64
cd /Users/WY/Documents/3DPrinting/code/TransformFeedback
export MACOSX_DEPLOYMENT_TARGET=10.11
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Products/Debug -F/Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Products/Debug -filelist /Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Intermediates/TransformFeedback.build/Debug/TransformFeedback.build/Objects-normal/x86_64/TransformFeedback.LinkFileList -Xlinker -rpath -Xlinker #executable_path/../Frameworks -mmacosx-version-min=10.11 -fobjc-arc -fobjc-link-runtime -stdlib=libc++ -framework GLKit -framework QTKit -framework OpenGL -framework GLUT -framework Cocoa -Xlinker -dependency_info -Xlinker
/Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Intermediates/TransformFeedback.build/Debug/TransformFeedback.build/Objects-normal/x86_64/TransformFeedback_dependency_info.dat -o /Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Products/Debug/TransformFeedback.app/Contents/MacOS/TransformFeedback
duplicate symbol _main in:
/Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Intermediates/TransformFeedback.build/Debug/TransformFeedback.build/Objects-normal/x86_64/geometryFeedback.o
/Users/WY/Library/Developer/Xcode/DerivedData/TransformFeedback-apcdjlohrayxsdeuvwzbzxatazbc/Build/Intermediates/TransformFeedback.build/Debug/TransformFeedback.build/Objects-normal/x86_64/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm trying to learn some OpenGL and immediately found out versions of OpenGL >3.2 were the more relevant ones to learn.
So I've set up my Mac OS X 10.10.3 with Xcode and command line tools to get some examples going.
But jebus this thing is being a pain in the butt.
The error I'm getting is.
Undefined symbols for architecture x86_64:
"LoadShaders(char const*, char const*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So I have the most recent version of GLFW GLEW cmake to get things going.
I add my header and library paths to the project.
Library Search Paths
/opt/X11/lib /usr/lib/ /Users/mac/Dev/workspace/C++/Test/glfw/build/src/Debug /Users/mac/Dev/workspace/C++/Test/glew/lib
I add the last two directories just to double up on the directory
Header Search Path look similar with include instead of lib
my linkers are vast (from trying random things)
-framework OpenGl -lGLUT -lglew -lglfw -lGL -lGLU -lXmu -lXi -lXext -lX11 -lXt
and I've linked the Binaries with the Libraries as well. Where am I going wrong??
my GPU is the Intel HD 4000 and appears to support up to OpenGL4.1.
Do I need to add compiler flags? Is this just not plausible?
Here's the tutorial code that I'm trying to run.
#include <stdio.h>
#include <stdlib.h>
//GLEW
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLUT/glut.h>
//GLFW
#include <GLFW/glfw3.h>
#include <AGL/glm.h>
GLFWwindow* window;
//no real explanation of what this is.....
#include <common/shader.hpp>
//MAIN FUNCTION
int main(int argc, char *argv[])
{
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
// specify GL information
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want 4.1>= OpenGL_version >=3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
window = glfwCreateWindow( 600, 375, "Tutorial 02", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
// Create Vertex Array Object.
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
do{
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
// Use our Shader
glUseProgram(programID);
// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// glVertexAttribPointer(
// Atrribute,
// size,
// type,
// normalized,
// stride,
// array buffer offset
// );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
// Cleanup VBO
glDeleteBuffers(1, &vertexbuffer);
glDeleteVertexArrays(1, &VertexArrayID);
glDeleteProgram(programID);
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
I mostly followed this guideline here
Oscar Chavez, Setup instructions
The function LoadShaders() is not part of OpenGL, and it is not part of any of the libraries you are using (I see GLEW and GLFW). In short, LoadShaders() is missing.
My guess is that LoadShaders() is a function that the author of the tutorial wrote, but the formatting for the tutorial is a bit frustrating (to say the least!) so I'm not sure.
//no real explanation of what this is.....
#include <common/shader.hpp>
Probably something the author of that tutorial wrote himself and simply dumped into the project sources, without telling anything more. The most annoying part about this lines is the use of wedge brackets (<…>) instead of quotes ("…") because this misleadingly suggests that the header is part of the system libraries and not something local to the project. Somwehere there likely is a common/shader.cpp and therein will probably be a function LoadShaders. It is this very function, that is something completely custom, not something part of OpenGL or any of the helper libraries, which your linker is telling you to be amiss.