Can't link GLFW C++; Added the .dll, .h, .lib, but still getting LNK2019

Danny Simons picture Danny Simons · Mar 18, 2013 · Viewed 7.1k times · Source

I've added the .dll to Windows/System32, the .lib to VS/lib, and the .h to the VS/include.

All that would be left is linking it through the project. This is what I have:


#include <glfw.h>

int main(void)
{
    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    if (!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW))
        return -1;

    /* Loop until the user closes the window */
    while (glfwGetWindowParam(GLFW_OPENED))
    {
        /* Render here */

        /* Swap front and back buffers and process events */
        glfwSwapBuffers();
    }

    return 0;
}

1>Main.obj : error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwGetWindowParam referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwOpenWindow referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
1>C:\Users\Danny\documents\visual studio 2010\Projects\OGL2\Debug\OGL2.exe : fatal error LNK1120: 4 unresolved externals

I've went to Project > Properties > Configuration Properties > Linker > Added the following to Additional Dependencies:


opengl32.lib;GLFW.lib;glu32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)

And for good measure, I also set Ignore All Default Libraries to "No".

I know I should have linked: opengl32.lib, GLFW.lib, glu32.lib, kernel32.lib and user32.lib

Unfortunately, the LNK 2019 problems persist. Really am unsure why. All help is greatly appreciated.

Answer

Michael Burr picture Michael Burr · Mar 18, 2013

The external names indicate that your project is building for a 64-bit target (32-bit DLL exports generally have the @nn suffix used by the stdcall calling convention). I'll bet your libraries are build for a 32-bit target.

Change the project settings to Win32 instead of x64.

I guessed incorrectly - the unresolved external symbols such as _glfwSwapBuffers indicate that you're building for a 32-bit target. When building for a 64-bit target, the external names do not get decorated with an underscore.

But the basic solution is the same - make sure that the target that is being built is the same as the target that the libraries you have are. So if you've downloaded the 64-bit GLFW libraries, make sure your VS project is building for a Win64 target.

I was able to compile and link your test program after extracting the 64-bit GLFW binary distribution into c:\temp:

C:\temp>cl /I c:\temp\glfw-2.7.7.bin.WIN64\include\GL test.cpp c:\temp\glfw-2.7.7.bin.WIN64\lib-msvc110\GLFWDLL.lib
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj
c:\temp\glfw-2.7.7.bin.WIN64\lib-msvc110\GLFWDLL.lib

C:\temp>