0

How do I link the library in a way that visual studio 2015 can actually see it?

Here's a few screenshot of my folders: glfw3, glfw3/include/glfw/, glfw3/lib

I've attempted to link these folders into visual studio, under "vc++ directories => include/library directories as such: include, library

also linking the same to my project directly, under linker => input => additional dependencies

to me, that seems like everything is correct (this is how it was in the tutorial i am following, learnopengl.com)

note: I am also doing the same with the GLAD library, which that has a .c file that I put directly into my sources, along with the header files linked the same way as with GLFW.

but despite trying to run this code:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

int main() {
    glfwInit();
    return 0;
}

it will always return this error, or similar ones:

1>------ Build started: Project: opengl_test, Configuration: Debug Win32 ------
1>  main.cpp
1>main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
1>C:\Users\Honza\Desktop\C++ programs\lib\glfw3\lib\glfw3.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86'
1>c:\users\honza\documents\visual studio 2015\Projects\opengl_test\Debug\opengl_test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I'm guessing there is something wrong with how I linked the libraries. I am willing to literally re-install visual studio if I have to, just please give me some possible solutions. I want to start learning opengl but all this jazz with linking libraries is leaving me frustrated and confused.

EDIT: The problem here was that I was using 64-bit GLFW binaries instead of the 32-bit ones, and compiling in 32-bit. I've fixed that, and now I get even more errors:

1>------ Build started: Project: opengl_test, Configuration: Debug Win32 ------
1>libglfw3.a(init.c.obj) : error LNK2019: unresolved external symbol ___chkstk_ms referenced in function __glfwInputError
1>libglfw3.a(init.c.obj) : error LNK2019: unresolved external symbol _vsnprintf referenced in function __glfwInputError
1>MSVCRTD.lib(vsnprintf.obj) : error LNK2001: unresolved external symbol _vsnprintf
1>libglfw3.a(context.c.obj) : error LNK2019: unresolved external symbol _sscanf referenced in function __glfwRefreshContextAttribs
1>MSVCRTD.lib(vsnprintf.obj) : error LNK2001: unresolved external symbol __vsnprintf
1>C:\Users\Honza\Documents\Visual Studio 2015\Projects\opengl_test\Debug\opengl_test.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 3
    `library machine type 'x64' conflicts with target machine type 'X86'` You sure your target is x64? – nm_tp Dec 10 '18 at 16:31
  • i don't know what that means. – Jan Procházka Dec 10 '18 at 16:37
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Matthieu Brucher Dec 10 '18 at 16:40
  • 2
    You are compiling in 32bits when the glfw library is 64bits. Doesn't work. – Matthieu Brucher Dec 10 '18 at 16:40
  • even though I was under the assumption i downloaded the 32-bit glfw binaries, i downloaded them again and this time there's even more errors... https://pastebin.com/4r3qPeCJ – Jan Procházka Dec 10 '18 at 16:54
  • I think the binaries are still wrong for your compiler. – drescherjm Dec 10 '18 at 17:37
  • I think this is related to the new error but not a duplicate: https://stackoverflow.com/questions/33185622/gobject-unresolved-externals-symbol-chkstk-ms – drescherjm Dec 10 '18 at 17:47
  • @JanProcházka This min you are trying to build your program for the x64 (64-bit) architecture but a part of it (in this case, the library) is x86 (32-bit). This is, most of the time, not going to work. If you're unfamiliar with the difference between x64 and x86, I suggest you read up. – nm_tp Dec 11 '18 at 09:22

2 Answers2

0

You write that you tried to link "these folders", while in fact, you need 2 separate things in order to use a function from a static library.

  1. Use the proper header file so compilation succeeds. From the error you quoted, compilation went well. The program knows there is an external function named _glfwInit.
  2. Link to the specific library (.lib file). Looks like you placed a path of a folder and not the full path of the .lib file.

Under Linker => input => additional dependencies, place the full path of the gfw3.lib file.

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
0

I faced the same problem while following learnopengl.com, The issue is that you need to rebuild glfw library with cmake again,
but this time when you choose visual studio 2015 or whatever for your generator for this project, you must also choose: OPTIONAL PLATFORM FOR GENERATOR -> x64
you can make sure that every thing is OK when you build glfw inside Visual Studio and the platform that appears is x64

Anas Shaikhany
  • 65
  • 1
  • 1
  • 5