Linker error in Dev C++: undefined reference to 'glfwInit' [duplicate] - c++

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/).

Related

Calling glewInit() causes main function to not be called

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.

GLEW and glfw compile error: undefined reference to symbol 'XConvertSelection'

I'm trying to compile this code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Playground", 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
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);
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 );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
It's a code from a tutorial I found about OpenGL, I program mostly in Java when it comes to OpenGL, but I wanted to try something new so I went to try in C++.
I'm using QtCreator for this project.
At first I included GLEW and glfw3 libraries:
And the same for the glfw library file.
And then, when I try compiling the program I get this error:
In text:
$ /home/sapir/Qt/5.4/gcc_64/bin/qmake -spec linux-g++ CONFIG+=debug -o Makefile ../Test/Test.pro
$ g++ -Wl,-rpath,/home/sapir/Qt/5.4/gcc_64 -o Test main.o -L/home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib/ -lglfw3 -lGLEW -lGLEWmx
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(glx_context.c.o): undefined reference to symbol 'glXQueryExtension'
//usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [Test] Error 1
23:12:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Test (kit: Desktop Qt 5.4.0 GCC 64bit)
When executing step "Make"
23:12:13: Elapsed time: 00:00.
I tired searching for an answer in forums and here, but I couldn't find anything that solved this problem.
Anybody got any Ideas?
After adding
-lXxf86vm -lXrandr -lGL -lGLU -lXi
to the gcc compiler, I get a different error, which contains:
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
This is my make file: http://pastebin.com/xL5Hpwsf
And this is my .pro file: http://pastebin.com/yhkV7nn7
Ok, after some research I found that the DSO error which I got means that the order of the includes I've implemented is incorrect and cause the compilation to fail.
So what I did is I used the command:
pkg-config --static --libs x11 xrandr xi xxf86vm glew glfw3
To get the packages I need for them to run and in the right order.
Then I compiled the project accordingly.
That's it :)
I got same error "undefined reference to symbol 'XConvertSelection'" while compiling example from Irrlicht 3D, solved by adding "-lX11".
Then I got error "undefined reference to symbol 'XF86VidModeGetGamma'", solved by adding "-lXxf86vm"

Invalid GL context with GLFW and Unofficial OpenGL SDK/GLEW

I have started work on a new project with OpenGL 3.3. I was using GLFW and GLEW for window setup and loading of GL functions, but switched to the Unofficial OpenGL SDK instead of GLEW. The problem remained, though:
I was getting a segmentation fault when calling glCreateShader(G_VERTEX_SHADER), and it turned out, that the function pointer was NULL. I later found out that it was caused by an invalid GL Context.
This is the setup code:
#include <glload/gl_3_3.h>
#include <glload/gll.h>
#include <GL/glfw.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include "Cube.h"
template <class T>
int arraySize(T *a) {
return (sizeof(a) / sizeof(*a));
}
int main() {
if(!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
// Open a window and create its OpenGL context
if(!glfwOpenWindow(1024, 768, 0,0,0,0, 0,0, GLFW_WINDOW)) {
fprintf(stderr, "Failed to open GLFW window\n");
glfwTerminate();
return -1;
}
if(LoadFunctions() == LS_LOAD_FAILED) {
fprintf(stderr, "Failed to load GL functions.\n");
return -1;
}
I have searched for answers on Google, and on here, but haven't been able to find anything. I also asked in the OpenGL IRC channel on Freenode, and they told me to try the Unofficial SDK instead of GLEW, because GLEW with the core profile is bad. This didn't work, though.
The most weird thing is, that it worked previously, with the exact same setup as now.
By the way, I am using Windows 7 x64 with the newest available drivers.
SOLUTION:
I was being dumb, and calling glCreateShader() before glewInit(). Sorry for being dumb :(

glfw openGL c++ window background and title

This is my source code from a series of tutorials I'm taking a look regarding opengl 3+.
//#include <stdio.h>
//#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
using namespace glm;
#include <iostream>
using namespace std;
int main()
{
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window\n" );
glfwTerminate();
return -1;
}
else
{
glfwSetWindowTitle( "Tutorial 01" );
}
// Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwEnable( GLFW_STICKY_KEYS );
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
return 0;
}
Everything works great except that when the window initializes it has a background color of white and the title 'GLFW WINDOW' and but after 1-2 secs the title changes to Tutorial 01 as it should have been in the first place and the background becomes black, as it should have been also.
In previous studies of opengl (2.x) with glut that I did a couple of years back I didnt have issues like that could someone explain what is wrong here?
I get the same behaviour (even running a release exe outside of the IDE) on an ATI FirePro V5700. If you're really bothered by it, download the GLFW source and change line 764 of carbon_window.c, line 1210 of win32_window.c, and line 962 of x11_window.c.
.\lib\carbon\carbon_window.c
(void)SetWindowTitleWithCFString( _glfwWin.window, CFSTR( "GLFW Window" ) );
.\lib\win32\win32_window.c
_glfwWin.window = CreateWindowEx( _glfwWin.dwExStyle, // Extended style
_GLFW_WNDCLASSNAME, // Class name
"GLFW Window", // Window title
_glfwWin.dwStyle, // Defined window style
wa.left, wa.top, // Window position
fullWidth, // Decorated window width
fullHeight, // Decorated window height
NULL, // No parent window
NULL, // No menu
_glfwLibrary.instance, // Instance
NULL ); // Nothing to WM_CREATE
.\lib\x11\x11_window.c
_glfwPlatformSetWindowTitle( "GLFW Window" );
I'm not experiencing any problems that you describe. I copied and pasted, compiled, then ran your code as you posted it (commenting out references to GLM as I don't have that library installed). The title changes instantaneously for me -- I never even see the window having the title "GLFW WINDOW", and the color of the graphics area is immediately black. Could it be that your computer simply isn't very fast?
What happens if you do the following?
do{
// Draw nothing, see you in tutorial 2 !
glClear(GL_COLOR_BUFFER_BIT);
// Swap buffers
glfwSwapBuffers();
glfwSleep(0.016);
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ) );
Edit: My GPU is an Nvidia GTX 580 (capable of at least OpenGL 4.3).
The background color of the window is configured by the call to the openGL function glClearColor() and refreshed with the glClear() function.
The delay in the changing of the title of the window may perhaps have something to do with the fact that to create a openGL 3.x it is required to create a standard OpenGL context (version 1.x or 2.x) and then get your application to opt-in to using an OpenGL 3.x context. A 1-2 sec delay though seems a lot.
Well as it seems it has to do with the IDE, if you run the actual .exe it works as intended and with no delay at all.

Access violation. when using GLEW and GLFW

I am sure that everything is linked correcly. I initially was using glload and glfw from the Unofficial GLSDK but then I decided to do away with glload which meant that I had to use glew in order to get at the modern headers.
#include <GL/glew.h>
#include <GL/glfw.h>
I have included glew before glfw as per the instructions.
During run time the OpenGL window opens
//(relevant code)
if(!glewInit()) {return -1; }
if(!glfwInit()) {return -1; }
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// also tried glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
if(!glfwOpenWindow(1024, 768, 8, 8, 8, 8, 24, 8, GLFW_WINDOW)){
glfwTerminate();
return -1;
}
glfwSetWindowTitle("OpenGL 3.2");
//init method
glGenVertexArrays(1, &vao); //<< Access violation here.
Any ideas what my problem is here?
I have looked at. "Access violation using VBO with glew" But it was no help.
glewInit is to be called after a OpenGL context has been created and bound to the current thread, i.e. after glfwOpenWindow in your case.
A little late, but figured I'd pipe in anyways.
As mentioned by datenwolf, in your relevant code posted the glewInit() should return an error due to it's positioning.
The other potential issue you could be encountering is described on http://www.opengl.org/wiki/OpenGL_Loading_Library under the GLEW section.
copy-paste from above:
GLEW has a problem with core contexts.
It calls glGetString(GL_EXTENSIONS)​,
which causes GL_INVALID_ENUM​ on GL 3.2+ core context as soon as glewInit()​ is called.
Solution for GLEW (also provided by above link) is to enable 'EXPERIMENTAL' support. Ex:
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if( err != GLEW_OK )
{
printf("GlewInit error");
exit(1);
}
Also late but for anyone still looking
glewExperimental = GL_TRUE;
Before initializing the context was got rid of the access violation error, but instead made the program exit with a GL_INVALID_ENUM error. With GLFW, I had to additionally comment out the window hints:
//glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
//glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
//glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
The programs then started compiling!