I'm attempting to use GLFW and GLEW. Currently my code works, and I see a window with triangles:
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <time.h>
#include "constants.h"
int main(void) {
GLFWwindow* window;
if (!glfwInit()) {
glfwTerminate();
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Window", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// if (glewInit() != GLEW_OK) {
// fprintf(stderr, "failure");
// return -1;
// }
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
while (glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0, 0);
glVertex2f(0, 1);
glVertex2f(1, 0);
glEnd();
glfwSwapBuffers(window); /* Swap front and back buffers */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
The above code works. However, when I uncomment the lines:
if (glewInit() != GLEW_OK) {
fprintf(stderr, "failure");
return -1;
}
The program exits silently. I confirmed with a breakpoint that the main function is not even being called. If it helps, here is my compilation command:
g++ -g main.cpp -I\glfw-3.3.2\include -I\glew-2.1.0\include -L\glfw-3.3.2\lib-mingw-w64 -L\glew-2.1.0\lib\Release\x64 -lglfw3 -lgdi32 -lopengl32 -luser32 -lshell32 -lglew32 -o main.exe
I can't figure this out for the life of me. I'm calling glewInit after making the OpenGL window, I'm importing glew before glfw. What am I doing wrong?
Crash before main is either because of static initialization or dynamic libraries.
It seems you are on windows, in that case glew release files contain two libraries glew32.lib and glew32s.lib.
You are linking against the non-static one, try -lglew32s. The first one is just a stub that will search for .dll, usually located in glew/bin folder, you would have to copy it to the working directory with main.exe. Remove GLEW_STATIC if you choose to go this way.
To prevent future errors, add glewExperimental=GL_TRUE; somewhere before glewInit. It enables OpenGL 3.3+ functions on some drivers.
As for why is it working now, the whole reason for existence of extension loading libraries in the first place is that the default drivers include only OpenGL 1.x functionality. All newer functions must be loaded directly at run-time from .dll drivers supplied by your GPU using dllopen() and GetProcAddress() which is exactly what these libraries do for you. That is why most newer functions are actually implemented as macros (wrapping the function pointer). That said, you are not using any new functions (you really should), so everything is working fine.
Related
everytime i try to compile it to test it doesnt recongnise files
#include <GL\glew.h>
#include <GLFW/glfw3.h>
#ifdef _WIN32
#pragma comment(lib, "winmm.lib")
#endif // _WIN32
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);
if(!glewInit())
return -1;
/* 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;
}
When you are using a compiler you need to tell it the directory to find files that you include.
In the line
#include <GL\glew.h>
#include <GLFW/glfw3.h>
This is telling the compiler to include the files glew.h and glfw3.h.
The problem is how is the compiler supposed to know where those files are (it doesn't search your entire Drive for them. c++ compilers have a way for you to tell it where these files are relative to.
In gcc/g++ you use the -I flag and specify where this directory is.
specify the folders where you stored gl\glew.h and glfw\glfw3.hto the gcc using that -I option. If your files are store in C:\Users\John\Projects\dependencies\GLFW\include\glew\glew.h and C:\Users\John\Projects\dependencies\Glew\include\glfw\glfw3.h
then use the the flags like so
gcc -IC:\Users\John\Projects\dependencies\GLFW\include
-IC:\Users\John\Projects\dependencies\Glew\include main.c -o out_file
I'm starting to learn the OpenGL stuff, but unfortunately, I can't make the initialization right.
I've added the glfw and glew libraries and these functions give me a strange error,
how can I make it work?
The code:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
std::cout << "GLFW initialization failed.\n";
return -1;
}
if (glewInit()!=GLEW_OK)
{
std::cout << "GLEW initialization failed.\n";
return -1;
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "Wiondow failed.\n";
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;
}
The errors:
To link the GLEW library correctly, proper preprocessor definitions have to be set. See GLEW - Installation:
[...] On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll [...]
I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a
I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.
The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.
Please see these screenshots: http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD
C++.cpp
#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"
int main(int argv, char** args){
display x(800,600,"something");
while(!x.isClosed()){
glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
x.Update();
}
return 0;
}
display.cpp
#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit(); //NO ERRORS OCCUR
if(status != GLEW_OK){
std::cerr << "glew failed to initialize" << std::endl;
}
m_isClosed = false;
}
display::~display(){
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool display::isClosed(){
return m_isClosed;
}
void display::Update(){
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
m_isClosed = true;
}
}
}
display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/
class display{
public:
display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~display();
private:
display(const display& other){}
display& operator=(const display& other){}
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
#endif /* DISPLAY_H_ */
To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).
Create OpenGL context and window
A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).
Initilize SDL
If you are using SDL you have to create the window and you have to create the OpenGL context.
SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
SDL_GLContext glContext = SDL_GL_CreateContext( window );
Note, you should check for errors with SDL_GetError.
The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.
SDL_GL_MakeCurrent( window, glContext );
Initilize GLUT
To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.
glutInit(&argc, argv);
glutCreateWindow("OGL window");
Initilize GLFW
Note, glfwInit returns GLFW_TRUE if succeded:
if ( glfwInit() != GLFW_TRUE )
return;
GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
return;
}
glfwMakeContextCurrent( wnd );
After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.
Set up GLEW
Note that glewInit returns GLEW_OK if succeded:
if ( glewInit() != GLEW_OK )
return;
To link the GLEW library correctly you have to set up proper preprocessor definitions:
On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll
See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
and finnally:
gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)
Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test
Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.
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/).
The program following, is one that creates a window which does nothing except close when you press esc. When I compile it with cygwin, there are no errors. The GLEW I use is from Cygwin Ports, and the SDL2 is version 2.0.3, from their website's SDL2-devel-2.0.3-mingw.tar.gz download. I have SDL2.dll in the directory of the compiled executable.
Links with: -lSDL2 -lSDL2main -lGLEW -lGLU -lGL -lSDL2 -lSDL2main -lGLEW -lGLU -lGL, twice to ensure everything is linked.
Also compiled with: -std=c++11
On my computer, the following program prints out:
OpenGL Vendor: (null)
OpenGL Renderer: (null)
OpenGL Shading Language Version: (null)
OpenGL Extensions: (null)
Error initializing GLEW! Missing GL version
The program appears to work otherwise. The main problem is that if I try to call, for example glGenVertexArrays, the program will crash with STATUS_ACCESS_VIOLATION. (See the crashing code here. I think this has something to do with GLEW's error Missing GL version.
#include <cstdio>
#include <chrono>
#include <thread>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <SDL2/SDL_opengl.h>
#include <GL/glu.h>
const int width = 1000;
const int height = 500;
bool Running = true;
#undef main
int main (int argc, char *argv[]) {
FILE* cdebug = fopen("cdebug.txt", "w");
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(cdebug, "SDL could not initialize! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
#define setAttr(attr, value) \
if (SDL_GL_SetAttribute(attr, value) < 0) { \
fprintf(cdebug, "SDL failed to set %s to %s, SDL Error: %s\n", #attr, #value, SDL_GetError()); fflush(cdebug);\
}
setAttr(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
setAttr(SDL_GL_CONTEXT_MINOR_VERSION, 3);
setAttr(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
setAttr(SDL_GL_RED_SIZE, 8);
setAttr(SDL_GL_GREEN_SIZE, 8);
setAttr(SDL_GL_BLUE_SIZE, 8);
setAttr(SDL_GL_DEPTH_SIZE, 24);
setAttr(SDL_GL_DOUBLEBUFFER, 1);
#undef setAttr
/*
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
*/
SDL_Window *window = SDL_CreateWindow(
"test",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
if (window == NULL) {
fprintf(cdebug, "Window could not be created! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
SDL_GLContext GLContext = SDL_GL_CreateContext(window);
if (GLContext == NULL) {
fprintf(cdebug, "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
if (SDL_GL_MakeCurrent(window, GLContext) < 0) {
fprintf(cdebug, "OpenGL context could not be made current! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
}
fprintf(cdebug, "OpenGL Vendor: %s\n", glGetString(GL_VENDOR));
fprintf(cdebug, "OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
fprintf(cdebug, "OpenGL Shading Language Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
fprintf(cdebug, "OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
fflush(cdebug);
glewExperimental = GL_TRUE;
{
GLenum glewError = glewInit();
if (glewError != GLEW_OK) {
fprintf(cdebug, "Error initializing GLEW! %s\n", glewGetErrorString(glewError)); fflush(cdebug);
}
}
SDL_Event event;
while (Running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP: {
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
Running = false;
break;
}
break;
}
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
SDL_GL_DeleteContext(GLContext);
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
return 0;
}
You are mixing cygwin and mingw in ways in which you shouldn't.
If you use cygwin's toolchain and -lGL and so on, you link against cygwin's OpenGL - which is not the native OpenGL lib on windows, but the one provided by cygwin's X server, implementing the GLX protocol.
The mingw version of SDL will use the native GL lib (opengl32.dll) on windows, using the wgl API. So SDL might even create a context for you, but the GL functions your programm are calling belong to a completely different GL implementation - for which your program never created a GL context.
The solution is to stick with one or the other: Completely use cygwin, and a cygwin version of SDL, and a cygwin X server. However, that is not the path I would recommend. I don't know if that would even get you some HW acceleration at all.
The more useful solution would be to not use cygwin, but mingw, for the whole project, with a mingw version of GLEW. That will result in a completely native windows binrary which will use the native OpenGL library with all features provided by the driver and not require cygwin's dlls and especially not cygwin's X server.
I managed to get things working in a weird way.
I am using a self compiled version of SDL2 but the SDL2-devel-2.0.3-mingw.tar.gz provided by the SDL website seems to work as well and using a combination of them (such as mingw version's libs and self-compiled .dll) seem to work as well.
For GLEW, I am using my own compiled version. To compile this, I used their website's source glew-1.11.0.zip and extracted this. Then I edited glew-1.11.0/Makefile and edited line 24 to SYSTEM = cygming. Then in glew-1.11.0/config/Makefile.cygming on line's 7 and 8, I removed the -mno-cygwin flag (so the line's are CC := gcc and LD := gcc) and added -D_WIN32 to line 10 (so the line becomes CFLAGS.SO = -DGLEW_BUILD -D_WIN32). Then in glew-1.11.0, I ran make all and let it compile. After that, I copied glew-1.11.0/include/GL to my includes directory. Next, I copied glew-1.11.0/lib/libglew32.dll.a to my libs folder. I also copied glew-1.11.0/lib/glew32.dll to my .exe's folder. Then to get it to not produce a linker error, I had to place a #define _WIN32 before my #include <GL/glew.h>.
To link everything, I managed to compile it with a minimum of -lSDL2 -lSDL2main -lglew32.dll -lopengl32.