0

I'm trying to run a program with GLFW in C++. I'm using visual studios 2019 on a windows 10 computer. I put the .h's and the .lib's in the proper folders and linked to them in the project settings. I'm running a minimal GLFW program I got off the internet. When I run it I get the following error:

1>------ Build started: Project: Riemannian, Configuration: Debug Win32 ------
1>Main.cpp
1>Main.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwTerminate referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwCreateWindow referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwWindowShouldClose referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwPollEvents referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwMakeContextCurrent referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in function _main
1>C:\Users\Aaron\source\repos\Riemannian\Debug\Riemannian.exe : fatal error LNK1120: 8 unresolved externals
1>Done building project "Riemannian.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I tried adding glfw3.dll to system32, but that didn't change anything. (side note, GLFW came with a file glfw3dll.lib. Is that just a .lib file that serves the same purpose as glfw3.dll?) Next I tried linking to opengl32.lib in my project settings. This got rid of the first error, the one with the __imp__ prefix. After that there were only 7 unresolved external symbols. My project type is "Application (.exe)" if that's relevant. I'm out of ideas and tired of googling, does anyone know what to do?

On a broader note, I have wasted so many hours trying to get libraries to work in Python, Java, and C++. I am incredibly frustrated and I wish someone could just explain to me how libraries work once and for all so that next time I have a problem I won't be completely lost.

Botje
  • 26,269
  • 3
  • 31
  • 41
A. Kriegman
  • 510
  • 1
  • 4
  • 18
  • You need to make sure you are linking in the correct mode (debug/release) and architecture (win32/x64) of glfw3. Can you be more precise about *which* edition of glfw3 you downloaded and **how** you are trying to link to it? Putting glfw3.dll in your system directory only (potentially) helps when *running* the program, not linking it. – Botje Aug 01 '19 at 08:00
  • So either you link *statically* (no dll required at runtime) and link with `glfw3.lib`, or you link *dynamically* and then you need to link with `glfw3dll.lib` and [set a preprocessor variable](https://www.glfw.org/docs/latest/build_guide.html#build_link_win32) – Botje Aug 01 '19 at 08:08
  • I have glfw-3.3.bin.WIN64. I moved the folder GLFW with the headers in it to ...\VC\Tools\MSVC\14.22.27905\include and the .lib's into ...\14.22.27905\lib\x86. I then went into my project settings and went to C/C++ > General > Additional include directories, and I added the include path. Then in VC++ directories > Library directories I added the path for the .lib's. – A. Kriegman Aug 01 '19 at 08:11
  • Since you downloaded the 64-bit edition, you also need to compile in 64bit mode (x64) as opposed to Win32. You then need to add the correct `glfw3.lib` to Linker -> Input -> Additional Dependencies as well so that Visual Studio actually *does* something with the file. The procedure in general is [like this](https://stackoverflow.com/a/4446085/1548468). – Botje Aug 01 '19 at 08:14
  • Also note that putting random external files in system paths never ends well. You should keep the include files and library files with your project directory, and configure your project to look there. – Botje Aug 01 '19 at 08:15
  • I added `#define GLFW_DLL` and now all the names in the error messages have the `__imp__` prefix. – A. Kriegman Aug 01 '19 at 08:15
  • Please stop flailing about and trying random things. That define is only needed if you link dynamically (via glfw3dll.lib). I was trying to help you set up static linking (glfw3.lib) so you do not need an extra dll at runtime! – Botje Aug 01 '19 at 08:18
  • 1
    @A.Kriegman So you've added the paths. Have you added **the library itself** (to Linker/Input/Additional Dependencies)? And really stop moving things around, you're only setting up future problems for yourself. It's perfectly possible to build programs using header files and library files placed **where they belong**, instead of moving them to miscellaneous locations,. – john Aug 01 '19 at 08:20
  • Finally got it working! Thanks for the help, I think I understand a bit better now. – A. Kriegman Aug 01 '19 at 08:31
  • Interestingly, when I installed GLEW I didn't need to add anything to the additional dependencies list. – A. Kriegman Aug 01 '19 at 08:36
  • Because glew is a [header-only library](https://en.wikipedia.org/wiki/Header-only): it is self-sufficient. The [stb](https://github.com/nothings/stb/) libraries are other examples you might run into. – Botje Aug 01 '19 at 08:42

1 Answers1

0

If all this setup is too complicated, you can also just take the Glitter project as a base. It takes care of all the boilerplate and just lets you get on with writing OpenGL. As a bonus, it is based on the cross-platform CMake build tool so your project should be easy to port to other operating systems.

Botje
  • 26,269
  • 3
  • 31
  • 41