3
votes

I can't link static libraries in my visual studio 2013 c++ project.

  1. I downloaded latest glew-1.11.0-win32 and glfw-3.1.1.bin.WIN32
  2. Set path to include directory (project Properties > C/C++ "Additional Include Directories")

    C:\Users\User\Documents\Visual Studio 2013\Projects\OpenGL\OpenGL\Common
    
  3. Add path to lib files (project Properties > Linker > General "Additional Library Directories")

    C:\Users\User\Documents\Visual Studio 2013\Projects\OpenGL\OpenGL\Common\Libs
    
  4. Add library names (project Properties > Linker > Input "Additional Dependencies")

    glew32.lib
    glfw3.lib
    
  5. Define preprocesor GLEW_STATIC (it's visible when I remove this)

    (project Properties > C/C++ > Preprocessor "Preprocessor Definitions")
    

When I build my project following error occur:

Error   1   error LNK2001: unresolved external symbol _glewExperimental C:\Users\User\documents\visual studio 2013\Projects\OpenGL\OpenGL\Source.obj    OpenGL
Error   2   error LNK1120: 1 unresolved externals   C:\Users\User\documents\visual studio 2013\Projects\OpenGL\Debug\OpenGL.exe OpenGL

When I want to comple this

#include<Windows.h>


#include<GL\glew.h>
#include<GLFW\glfw3.h>

#include<iostream>

GLFWwindow * window;

using namespace std; 

int main()
{
    glewExperimental = TRUE;
    GLenum error = glewInit();
    if (error != GLEW_OK)
    {
        cout << "Error!" << endl;
    }

    system("pause");

    return 0;
}

Windows 8.1 on virutal machine(VirtualBox) and Mirosoft Visual Studio 2013

What I am doing wrong? In other topics on this forum this solution works properly.

When I remove this two libs from directories I got same error, so probably Visual don't see these two libs. But path is set properly. Include path works, all header files are visible by intelsense.

1
Looking at the docs here, it seems to me that glew32.lib is not the actual static lib, but rather just an import lib for glew32.dll. The document seems to imply that you need to actually build the lib alongside your sources in order to link statically to it.bogdan
Do You know how to do that? Every tutorial is about linking own dll library. I found this one link but it did not work. Any idea?Szczepan
I downloaded the binary package from the GLEW site and found that, in the lib\Release\Win32 directory, alongside glew32.lib, there's also glew32s.lib. I have a sneaking suspicion that the one with the s is the actual static lib you're looking for. So, make only this change in Additional Dependencies and try again (everything else seems fine in your setup, and the document I referenced in the previous comment seems to be out of date - this other page says that a pre-built static lib should be included in the binary Windows package).bogdan

1 Answers

2
votes

@bogdan is correct: the static library version of GLEW is glew32s.lib.

BTW, the GLEW installation page on sourceforge.net says you have to "include include glew.h and glew.c into your project if you want static linking." While that sounds easy enough, it seemed like unnecessary roughness to do it for every openGL project. Instead, do the following:

  • Add the GLEW_STATIC Macro under Properties - Common Properties - C/C++ - Preprocessor - Preprocessor Definitions
  • Add glew32s.lib under Properties - Common Properties - Linker - Input - Additional Library Dependencies
  • If you didn't copy glew32s.lib to one of Visual Studio's default library paths, add the path to it under under Properties - Common Properties - Linker - General - Additional Library Directories

Worked for me.

In case you aren't aware of this, you can Add New Property Sheet, e.g. openglPropertySheet32Static, in the Property Manager (View menu - Other Windows - Property Manager). Add your openGL specific properties to it. Don't forget to explicitly save the sheet--I'm not sure it is saved automatically with the project. Then, all you have to do for future openGL projects is Add Existing Property Sheet, openglPropertySheet32Static, in the Property manager (you remember where you saved it, right?).