I am reading a book called learn OpenGL and I am struggling with the set up. I am using code blocks. What I have done so far is:
I downloaded the 32 bit binaries for GLFW from this website: http://www.glfw.org/download.html
I also downloaded GLAD from this website:
http://glad.dav1d.de/
I then added all the files from these downloads to an include folder. After this I added glad.c and glad.h to my project. Then in codeblocks I clicked the project tab then went to build options - search directories - compiler and added the 2 include folders that I got from the downloads and the src folder. Then in search directories I went to linker and added lib-mingw.
The book I am reading told me to add this code:
#include <GLFW/glfw3.h>
#include <iostream>
int main()
{
//setting the version of openGL
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//creating a window
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window = NULL)
{
std::cout <<"Failed to create GLFW window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
return 0;
}
My problem is I keep getting the error: 'GLADloadproc' was not declared in this scope and 'GLADLoadGLLoader' was not declared in this scope. Also I have tried putting the glad folder into my project file directory so that the #include <glad/glad.h> can be found. What could be the problem?
Related
I tried to create a window with the simplest code possible:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h> // Always include it before glfw.h
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
int main(void) {
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // Mac
GLFWwindow* window = glfwCreateWindow(720, 480, "OpenGL", NULL, NULL); // Create a window
if (window == NULL) {
fprintf(stderr, "Failed to create window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
return 0;
}
This is my Makefile:
game:
g++ src/main.cpp -std=c++17 -o play -I include -L lib -l glfw.3 -l GLEW.2.2
When I compile my code there is no error, but when I try to play my code I have this error:
Failed to create window
Invalid window hint 0x00000003
Invalid window hint 0x00000003
Context profiles are only defined for OpenGL version 3.2 and above
Can someone help me? I don't know why my window doesn't want to be created...
GLFW_VERSION_MAJOR & GLFW_VERSION_MINOR are not valid arguments to glfwWindowHint:
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
^^^^^^^^^^^^^^^^^^ nope
glfwWindowHint(GLFW_VERSION_MINOR, 3);
^^^^^^^^^^^^^^^^^^ also nope
Use GLFW_CONTEXT_VERSION_MAJOR & GLFW_CONTEXT_VERSION_MINOR instead.
As per the docs, emphasis mine:
GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR specify the client API version that the created context must be compatible with. The exact behavior of these hints depend on the requested client API.
Note: Do not confuse these hints with GLFW_VERSION_MAJOR and GLFW_VERSION_MINOR, which provide the API version of the GLFW header.
I'm getting the error LNK2005 for seemingly no reason, or at least none that can I can identify. Essentially, using the following code I can compile without issue.
test.h
#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
namespace test
{
//Objects and variables
GLFWwindow* window;
//Function prototypes
bool Initialize();
}
test.cpp
#include "test.h"
#include <iostream>
bool test::Initialize()
{
std::cout << "Initializing GLFW: OpenGL version 3.3 \n";
//Initialize GLFW
glfwInit();
//Set window properties (version 3.3, core profile, not resizeable)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//Create Window
window = glfwCreateWindow(800, 800, "Learn OpenGL", nullptr, nullptr);
if (window = nullptr)
{
std::cout << "Failed to create GLFW window \n";
glfwTerminate();
return false;
}
return true;
}
int main()
{
test::Initialize();
return 0;
}
However, when compiling nearly the exact same thing (http://pastebin.com/VpPep9pM), along with some other code, it gives the errors:
Error LNK2005 "struct GLFWwindow * window" (?window##3PAUGLFWwindow##A) already defined in Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj
Error LNK2005 "struct GLFWwindow * System::window" (?window#System##3PAUGLFWwindow##A) already defined in Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj
Error LNK1169 one or more multiply defined symbols found OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\Debug\OpenGL.exe
So, I would like to know what causes the errors, I'm assuming it has something to do with the "context".
In your header file, you are defining you variable, when it needs to be declared in the header and defined in one source file.
In test.h
namespace test {
extern GLFWwindow* window;
}
and in main.cpp
namespace test {
GLFWwindow* window;
}
Without the extern in the header, you create one instance of test::window in every source file that includes it, which is a problem if there are two or more definitions.
I'm trying to compile a short openGl and GLFW code but I'm failing in..
I've included all the .h like this :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
I'm on linux so I've made a :
sudo zypper install libglfw2
When I'm compiling like this :
g++ main.cpp -lGLU
I have :
main.cpp:14:22: fatal error: GL/glfw2.h:Any file or directory
#include <GL/glfw2.h>
^
compilation terminated.
I've also tried to include #include <GL/glfw3.h> but without success.
This is my complet code :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
int main()
{
if (glfwInit() == false)
{
fprintf(stderr, "GLFW failed to initialise.\n");
return (-1);
}
glfwWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(640, 480, "OPENGL", NULL, NULL);
if (!window)
{
fprintf(stderr, "Window failed to create\n");
glfwTerminate();
return (-1);
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return (-1);
}
return (0);
}
Could someone know what I have to do to compile my code ?
Thanks
There is no GL/glfw2.h and never has been, and the code above uses the GLFW 3 API. See Renamed library and header file for more information about header paths.
There is no reason to use GLFW 2 for new projects, so I would avoid the libglfw2 package. Instead see if there is a GLFW 3 package.
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 years ago.
I'm trying to follow this tutorial for OpenGL. I originally copied the code by hand, but that wasn't working, so I've copy-pasted the code straight from the website. I keep getting this error:
[Linker error] undefined reference to 'glfwInit'
from this code (which feels longer than necessary):
//C++ standard headers
#include <stdio.h>
#include <stdlib.h>
//GLEW header
#include <GL/glew.h>
//GLFW header
#include <GL/glfw3.h>
int main()
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
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
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", 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);
do{
// Draw nothing, see you in tutorial 2 !
// 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 );
}
I've got no idea why it's not compiling. Anyone know what's going on?
EDIT: I'm using Dev-C++, as stated in the title.
undefined reference to 'glfwInit'
means that the linker did not find the library where glfwInit() is defined. You have to add glfw3.a to your linker input. Indeed, Dev-C++ use MinGW so unlike Visual Studio, the libraries can not be .lib.
To do that with Dev-C++, go to your 'project options', 'parameters', and 'add a library'. Then browse the explorer to find the glfw3.a I mentioned (usually in GLFW-<version>/lib/).
i'm sorry if the question layout is weird or something this is my first time asking a question. I have started learning c++ a week ago and now that i know how to create a window with GLFW I would like to clean up my code and make it nicer. I tried to put the "create window" stuff inside a .cpp file named "window.cpp" and then import it to the main file. but when i take out the GLFWCreateWindow function it doesn't recognize the window name "w_gill" in the swapbuffer, windowshouldclose and destroywindow functions. Can anyone please help me?
Here is the Main.cpp file:
#include <iostream>
#include <Windows.h>
#include "window.cpp"
int main(){
do{
createWindow();
glfwSwapBuffers(w_gill);
glfwPollEvents();
} while (!glfwWindowShouldClose(w_gill));
glfwDestroyWindow(w_gill);
glfwTerminate();
return 0;
}
And the Window.cpp file:
#include <GL\glew.h>
#include <GLFW\glfw3.h>
int windowWidth = 1920 / 2;
int windowHeight = 1080 / 2;
int createWindow(){
if (!glfwInit())
{
glfwTerminate();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow* w_gill;
w_gill = glfwCreateWindow(windowWidth, windowHeight, "Gillow", NULL, NULL);
if (!w_gill)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(w_gill);
glfwSetInputMode(w_gill, GLFW_STICKY_KEYS, GL_TRUE);
}
Nevermind I got it, I had to put
GLFWwindow* w_gill;
int createWindow();
inside of the main.cpp file so they are linked. Thanks for responding anyway.
Your problem has to do with the "scope" or lifetime that a variable has in C. The scope of w_gill is local to its containing function, and as such, there is no visibility to that variable outside of the function. In addition to moving the createWindow() function to its own .CPP file, you must have made some other changes, as even if it was still in main.cpp you would still have the same issue.