Question

I'm following an OpenGL tutorial and I'm at the last step, using GLEW to setup the FPS counter. I had a problem where I had to use #pragma comment(lib, "glew32sd.lib") to successfully link my library. Whereas with freeglut, I didn't have to explicitly reference it.

My structure is simple,

 library\
    dll\
      freeglut.dll
      glew32d.dll
    include\
      GL\
        freeglut.h
        freeglut_ext.h
        freeglut_std.h
        glew.h
        glut.h
        glxew.h
        wglew.h
    lib\
      freeglut.lib
      glew32d.lib
      glew32sd.lib

I have set Visual Studio to include the include\ dir and to look for additional libraries in lib. The dll folder is set to copy to the output via a post-build job. I'm pretty sure it works because up to this point everything was fine with freeglut. Only glew isn't working.

How is Visual Studio not able to link glew but it is working with freeglut ? They are in the same directory. I see that freeglut is being linked because I see it in the linker's output 1> Processed /DEFAULTLIB:freeglut.lib. Both libraries are compiled from my computer.

I tried to compile GLEW as a static library so I'm assuming the compiler does not load the .dll so I might as well remove it. But in both cases it wouldn't work. I tried using #define GLEW_STATIC and #define GLEW_BUILD, would not link either.

Then I stumbled on an answer on SO and found that if I did #pragma comment(lib, "glew32sd.lib") it would correctly load my library. Now I'm assuming that by explicitly telling the linker to use this .lib it makes it work. But why then does freeglut gets loaded automatically ? I'm not referencing directly it anywhere.

Right now my code is working but I'm just curious. I'm a new C++ developer and the concept of static libraries and dynamic libraries is still too fresh in my mind.

Was it helpful?

Solution

You need to tell the linker which libraries you use either via the pragma or in the Project->Linker->Input->Additional Dependencies area of the project properties.

In the case of freeglut the pragmas are supplied for you in freeglut_std.h unless you explicitly define FREEGLUT_LIB_PRAGMAS to 0. If you did that you would need to add the dependency manually either via your own pragmas or in the Additional Dependencies area.

In general it is up to you to add the dependencies you need. In my experience it is rare for a library to attempt to do it automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top