无法编译 GLFW 和 GLAD

Unable to compile GLFW and GLAD

我正在尝试使用 GLFW 和 GLAD 设置一个项目。 文件结构如下:

opengl
│   build.bat
│   main.c
│
├───bin
│
├───deps
│   └───lib-vc2022
│           glfw3.lib
│
└───include
    ├───glad
    │       glad.c
    │       glad.h
    │
    ├───GLFW
    │       glfw3.h
    │       glfw3native.h
    │
    └───KHR
            khrplatform.h

我有 运行 vcvarsall.bat 并将编译器设置为 x64,因此我可以访问 msvc 编译器。 要编译我 运行 build.bat 的代码,其中包含:(我从 glfw 网站获得了 64 位 Prebuilt Windows 二进制文件)

@echo off

if not exist bin mkdir bin

pushd bin

cl  /Zi /Od /I..\include  C:\Dev\opengl\include\glad\glad.c ..\main.c  /link /NODEFAULTLIB /LIBPATH:..\deps\lib-vc2022 glfw3.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 opengl32.lib
popd 

main.c 包含在 glfw 中打开 window 所需的代码。 我包含在 main.c 中的文件如下:

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

然后我 运行 build.bat 得到以下错误:

glad.obj : error LNK2019: unresolved external symbol __stdio_common_vssca
nf referenced in function _vsscanf_s_l
glad.obj : error LNK2019: unresolved external symbol free referenced in f
unction free_exts
glad.obj : error LNK2019: unresolved external symbol malloc referenced in
 function get_exts
glad.obj : error LNK2019: unresolved external symbol memcpy referenced in
 function get_exts
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol memcpy
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
memcpy
glad.obj : error LNK2019: unresolved external symbol strlen referenced in
 function get_exts
glad.obj : error LNK2019: unresolved external symbol strncmp referenced i
n function find_coreGL
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 __GSHandlerCheck
glfw3.lib(wgl_context.obj) : error LNK2001: unresolved external symbol __
GSHandlerCheck
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __
GSHandlerCheck
glfw3.lib(osmesa_context.obj) : error LNK2001: unresolved external symbol
 __GSHandlerCheck
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __GSHand
lerCheck
glfw3.lib(win32_init.obj) : error LNK2001: unresolved external symbol __G
SHandlerCheck
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
__GSHandlerCheck
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol _
_GSHandlerCheck
glad.obj : error LNK2001: unresolved external symbol __GSHandlerCheck
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol __GSHandl
erCheck
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol __GSHan
dlerCheck
glfw3.lib(context.obj) : error LNK2001: unresolved external symbol __GSHa
ndlerCheck
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 __security_check_cookie
glfw3.lib(wgl_context.obj) : error LNK2001: unresolved external symbol __
security_check_cookie
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __
security_check_cookie
glfw3.lib(osmesa_context.obj) : error LNK2001: unresolved external symbol
 __security_check_cookie
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __securi
ty_check_cookie
glfw3.lib(win32_init.obj) : error LNK2001: unresolved external symbol __s
ecurity_check_cookie
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
__security_check_cookie
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol _
_security_check_cookie
glad.obj : error LNK2001: unresolved external symbol __security_check_coo
kie
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol __securit
y_check_cookie
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol __secur
ity_check_cookie
glfw3.lib(context.obj) : error LNK2001: unresolved external symbol __secu
rity_check_cookie
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 __security_cookie
glfw3.lib(wgl_context.obj) : error LNK2001: unresolved external symbol __
security_cookie
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __
security_cookie
glfw3.lib(osmesa_context.obj) : error LNK2001: unresolved external symbol
 __security_cookie
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __securi
ty_cookie
glfw3.lib(win32_init.obj) : error LNK2001: unresolved external symbol __s
ecurity_cookie
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
__security_cookie
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol _
_security_cookie
glad.obj : error LNK2001: unresolved external symbol __security_cookie
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol __securit
y_cookie
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol __secur
ity_cookie
glfw3.lib(context.obj) : error LNK2001: unresolved external symbol __secu
rity_cookie
LINK : error LNK2001: unresolved external symbol mainCRTStartup
glfw3.lib(win32_joystick.obj) : error LNK2019: unresolved external symbol
 __imp_calloc referenced in function _glfwInitJoysticksWin32
glfw3.lib(wgl_context.obj) : error LNK2001: unresolved external symbol __
imp_calloc
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __
imp_calloc
glfw3.lib(osmesa_context.obj) : error LNK2001: unresolved external symbol
 __imp_calloc
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
__imp_calloc
glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_
calloc
glfw3.lib(vulkan.obj) : error LNK2001: unresolved external symbol __imp_c
alloc
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol _
_imp_calloc
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol __imp_cal
loc
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol __imp_c
alloc
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __imp_ca
lloc
glfw3.lib(win32_init.obj) : error LNK2001: unresolved external symbol __i
mp_calloc
glfw3.lib(win32_joystick.obj) : error LNK2019: unresolved external symbol
 __imp_free referenced in function deviceCallback
glfw3.lib(wgl_context.obj) : error LNK2001: unresolved external symbol __
imp_free
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __
imp_free
glfw3.lib(osmesa_context.obj) : error LNK2001: unresolved external symbol
 __imp_free
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
__imp_free
glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_
free
glfw3.lib(vulkan.obj) : error LNK2001: unresolved external symbol __imp_f
ree
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol _
_imp_free
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol __imp_fre
e
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol __imp_f
ree
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __imp_fr
ee
glfw3.lib(win32_init.obj) : error LNK2001: unresolved external symbol __i
mp_free
glfw3.lib(init.obj) : error LNK2019: unresolved external symbol __imp___s
tdio_common_vsprintf referenced in function _glfwInputError
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 __imp___stdio_common_vsprintf
glfw3.lib(win32_monitor.obj) : error LNK2019: unresolved external symbol
memset referenced in function _glfwPlatformGetVideoMode
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 memset
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol memset
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol memset
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol memset
glfw3.lib(win32_init.obj) : error LNK2001: unresolved external symbol mem
set
glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol _fltus
ed
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol _
fltused
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 _fltused
glfw3.lib(init.obj) : error LNK2001: unresolved external symbol _fltused
glfw3.lib(window.obj) : error LNK2001: unresolved external symbol _fltuse
d
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol _fltused
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
_fltused
glfw3.lib(window.obj) : error LNK2019: unresolved external symbol __imp_s
trncpy referenced in function glfwWindowHintString
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __imp_st
rncpy
glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_
strncpy
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 __imp_strncpy
glfw3.lib(context.obj) : error LNK2019: unresolved external symbol strstr
 referenced in function _glfwStringInExtensionString
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 strstr
glfw3.lib(context.obj) : error LNK2019: unresolved external symbol __imp_
strncmp referenced in function _glfwRefreshContextAttribs
glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __imp_st
rncmp
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __
imp_strncmp
glfw3.lib(context.obj) : error LNK2019: unresolved external symbol __imp_
__stdio_common_vsscanf referenced in function sscanf
glfw3.lib(input.obj) : error LNK2019: unresolved external symbol __imp_st
rcspn referenced in function glfwUpdateGamepadMappings
glfw3.lib(input.obj) : error LNK2019: unresolved external symbol __imp_st
rspn referenced in function glfwUpdateGamepadMappings
glfw3.lib(input.obj) : error LNK2019: unresolved external symbol __imp_re
alloc referenced in function glfwUpdateGamepadMappings
glfw3.lib(win32_monitor.obj) : error LNK2001: unresolved external symbol
__imp_realloc
glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_
realloc
glfw3.lib(input.obj) : error LNK2019: unresolved external symbol __imp_st
rtoul referenced in function parseMapping
glfw3.lib(monitor.obj) : error LNK2019: unresolved external symbol __imp_
qsort referenced in function refreshVideoModes
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 __imp_qsort
glfw3.lib(monitor.obj) : error LNK2019: unresolved external symbol memmov
e referenced in function _glfwInputMonitor
glfw3.lib(monitor.obj) : error LNK2019: unresolved external symbol powf r
eferenced in function glfwSetGamma
glfw3.lib(vulkan.obj) : error LNK2019: unresolved external symbol strcmp
referenced in function _glfwInitVulkan
glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol
 strcmp
glad.exe : fatal error LNK1120: 27 unresolved externals


我设法解决了这个问题,方法是将其添加到我的批处理文件中:

@echo off

if not exist bin mkdir bin

pushd bin

set LIBRARIES=glfw3.lib user32.lib gdi32.lib shell32.lib
cl /MDd /FC /Zi /O2 /I..\include ..\main.c C:\Dev\opengl\include\glad\glad.c /link /NODEFAULTLIB:msvcrt.lib /LIBPATH:..\deps\lib-vc2022\ %LIBRARIES%

popd