与 glfw3 链接时出错

Error occurs when linking with glfw3

本文关键字:出错 链接 glfw3      更新时间:2023-10-16

我一直在尝试编译C++代码,最近不依赖IDE,我决定使用编辑器和命令行来编写和编译代码。

问题是我想制作一个 glfw 应用程序,但在与 glfw3.lib 和 opengl32.lib 链接时出现错误:

A.exe:致命错误 LNK1120:92 个未解析的外部

操作系统: 视窗 7
编译器:叮当

src/main.cpp:

#include <stdlib.h>
#include <stdio.h>
#include "window.h"
#define WIDTH 800
#define HEIGHT 600
int main(int argc, char* argv[]){
    if (!Window_init(WIDTH, HEIGHT, "HELLOWORLD")){
        printf("Problem loading window!n");
        return 1;
    }
    return 0;    
}

src/window.h

#ifndef WINDOW_H
#define WINDOW_H
#include "GLFW/glfw3.h"
#define GLFW_INCLUDE_NONE
static GLFWwindow* window;
int Window_init(int width, int height, char* title);
void Window_update();
int Window_init(int width, int height, char* title){
    if (!glfwInit())return 0;
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    window = glfwCreateWindow(width, height, title,NULL, NULL);
    if (!window){
        return 0;
    }
    glfwMakeContextCurrent(window);
    return 1;
}
void Window_update(){
}
#endif // !WINDOW_H

编译命令:

clang++ ..src*.cpp ..src*.h -L..lib -lglfw3 -lopengl32 -I..include -std=c++17 

错误:

LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
glfw3.lib(init.c.obj) : warning LNK4217: locally defined symbol ___stdio_common_vsprintf imported in function __glfwInputError
glfw3.lib(wgl_context.c.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(egl_context.c.obj) : warning LNK4217: locally defined symbol _free imported in function __glfwInitEGL
glfw3.lib(monitor.c.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(vulkan.c.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(win32_window.c.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(win32_joystick.c.obj) : warning LNK4217: locally defined symbol _free imported in function _pollJoystickState
glfw3.lib(window.c.obj) : warning LNK4217: locally defined symbol _free imported in function _glfwDestroyWindow
glfw3.lib(input.c.obj) : warning LNK4217: locally defined symbol _free imported in function _glfwSetInputMode
glfw3.lib(win32_init.c.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(win32_monitor.c.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(monitor.c.obj) : warning LNK4217: locally defined symbol _qsort imported in function _refreshVideoModes
glfw3.lib(win32_joystick.c.obj) : warning LNK4049: locally defined symbol _qsort imported
glfw3.lib(wgl_context.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(egl_context.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(monitor.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(vulkan.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(win32_window.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(win32_joystick.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(window.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(input.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(win32_init.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(win32_monitor.c.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(context.c.obj) : error LNK2019: unresolved external symbol __imp__strstr referenced in function __glfwStringInExtensionString
glfw3.lib(win32_joystick.c.obj) : error LNK2001: unresolved external symbol __imp__strstr
glfw3.lib(context.c.obj) : error LNK2019: unresolved external symbol __imp__strncmp referenced in function __glfwRefreshContextAttribs
glfw3.lib(egl_context.c.obj) : error LNK2001: unresolved external symbol __imp__strncmp
glfw3.lib(context.c.obj) : error LNK2019: unresolved external symbol __imp____stdio_common_vsscanf referenced in function __vsscanf_l
glfw3.lib(win32_init.c.obj) : error LNK2019: unresolved external symbol __imp__RegisterDeviceNotificationW@12 referenced in function _createHelperWindow
glfw3.lib(win32_init.c.obj) : error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function _createHelperWindow
glfw3.lib(win32_window.c.obj) : error LNK2001: unresolved external symbol __imp__CreateWindowExW@48
glfw3.lib(win32_init.c.obj) : error LNK2019: unresolved external symbol __imp__DestroyWindow@4 referenced in function __glfwPlatformTerminate
glfw3.lib(win32_window.c.obj) : error LNK2001: unresolved external symbol __imp__DestroyWindow@4
glfw3.lib(win32_init.c.obj) : error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function _createHelperWindow
glfw3.lib(win32_window.c.obj) : error LNK2001: unresolved external symbol __imp__ShowWindow@8
glfw3.lib(win32_init.c.obj) : error LNK2019: unresolved external symbol __imp__SystemParametersInfoW@16 referenced in function __glfwPlatformInit
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__CreateDCW@16 referenced in function __glfwPlatformGetGammaRamp
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__DeleteDC@4 referenced in function __glfwPlatformGetGammaRamp
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__GetDeviceCaps@8 referenced in function _createMonitor
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__GetDeviceGammaRamp@8 referenced in function __glfwPlatformGetGammaRamp
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__SetDeviceGammaRamp@8 referenced in function __glfwPlatformSetGammaRamp
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__ChangeDisplaySettingsExW@20 referenced in function __glfwPlatformGetVideoModes
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__EnumDisplaySettingsW@12 referenced in function __glfwPlatformGetVideoMode
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__EnumDisplaySettingsExW@16 referenced in function __glfwPlatformGetMonitorPos
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__EnumDisplayDevicesW@16 referenced in function __glfwPlatformGetMonitors
glfw3.lib(win32_monitor.c.obj) : error LNK2019: unresolved external symbol __imp__realloc referenced in function __glfwPlatformGetMonitors
glfw3.lib(monitor.c.obj) : error LNK2019: unresolved external symbol __imp___strdup referenced in function __glfwAllocMonitor
glfw3.lib(win32_window.c.obj) : error LNK2001: unresolved external symbol __imp___strdup
glfw3.lib(win32_joystick.c.obj) : error LNK2001: unresolved external symbol __imp___strdup
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__CreateBitmap@20 referenced in function _createIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DeleteObject@4 referenced in function _createIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__CreateDIBSection@24 referenced in function _createIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__TrackMouseEvent@4 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DispatchMessageW@4 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__PeekMessageW@20 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetMessageTime@0 referenced in function _translateKey
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SendMessageW@16 referenced in function __glfwPlatformSetWindowIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__PostMessageW@16 referenced in function __glfwPlatformPostEmptyEvent
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__WaitMessage@0 referenced in function __glfwPlatformWaitEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DefWindowProcW@16 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__UnregisterClassW@8 referenced in function __glfwUnregisterWindowClassWin32
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__RegisterClassExW@4 referenced in function __glfwRegisterWindowClassWin32
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__MoveWindow@24 referenced in function __glfwPlatformSetWindowAspectRatio
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetWindowPos@28 referenced in function __glfwPlatformSetWindowMonitor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__IsWindowVisible@4 referenced in function __glfwPlatformWindowVisible
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__IsIconic@4 referenced in function __glfwPlatformWindowIconified
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__BringWindowToTop@4 referenced in function __glfwPlatformCreateWindow
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__IsZoomed@4 referenced in function __glfwPlatformWindowMaximized
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__OpenClipboard@4 referenced in function __glfwPlatformGetClipboardString
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__CloseClipboard@0 referenced in function __glfwPlatformGetClipboardString
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetClipboardData@8 referenced in function __glfwPlatformSetClipboardString
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetClipboardData@4 referenced in function __glfwPlatformGetClipboardString
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__EmptyClipboard@0 referenced in function __glfwPlatformSetClipboardString
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetFocus@4 referenced in function __glfwPlatformCreateWindow
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetActiveWindow@0 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetKeyState@4 referenced in function _getKeyMods
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetAsyncKeyState@4 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetKeyNameTextW@12 referenced in function __glfwPlatformGetKeyName
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetCapture@4 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__ReleaseCapture@0 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__MsgWaitForMultipleObjects@20 referenced in function __glfwPlatformWaitEventsTimeout
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetSystemMetrics@4 referenced in function __glfwPlatformSetWindowIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetForegroundWindow@4 referenced in function __glfwPlatformCreateWindow
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetDC@4 referenced in function _createIcon
glfw3.lib(wgl_context.c.obj) : error LNK2001: unresolved external symbol __imp__GetDC@4
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__ReleaseDC@8 referenced in function _createIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetPropW@12 referenced in function _createNativeWindow
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetPropW@8 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__RemovePropW@8 referenced in function __glfwPlatformDestroyWindow
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetWindowTextW@8 referenced in function __glfwPlatformSetWindowTitle
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetClientRect@8 referenced in function __glfwPlatformGetFramebufferSize
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetWindowRect@8 referenced in function __glfwPlatformSetWindowAspectRatio
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__AdjustWindowRectEx@16 referenced in function __glfwPlatformGetWindowFrameSize
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetCursorPos@8 referenced in function __glfwPlatformPollEvents
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetCursor@4 referenced in function __glfwPlatformSetCursor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetCursorPos@4 referenced in function __glfwPlatformGetCursorPos
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__ClipCursor@4 referenced in function __glfwPlatformSetCursorMode
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__ClientToScreen@8 referenced in function __glfwPlatformGetWindowPos
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__ScreenToClient@8 referenced in function __glfwPlatformGetCursorPos
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__WindowFromPoint@8 referenced in function _cursorInClientArea
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetRect@20 referenced in function __glfwPlatformGetWindowFrameSize
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__PtInRect@12 referenced in function _cursorInClientArea
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetWindowLongW@8 referenced in function __glfwPlatformSetWindowMonitor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__SetWindowLongW@12 referenced in function __glfwPlatformSetWindowMonitor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__GetClassLongW@8 referenced in function __glfwPlatformSetWindowIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__LoadCursorW@8 referenced in function __glfwPlatformCreateStandardCursor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DestroyIcon@4 referenced in function __glfwPlatformDestroyCursor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__LoadImageW@24 referenced in function __glfwRegisterWindowClassWin32
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__CreateIconIndirect@4 referenced in function _createIcon
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__CopyIcon@4 referenced in function __glfwPlatformCreateStandardCursor
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DragQueryFileW@16 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DragQueryPoint@8 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DragFinish@4 referenced in function _windowProc@16
glfw3.lib(win32_window.c.obj) : error LNK2019: unresolved external symbol __imp__DragAcceptFiles@8 referenced in function _createNativeWindow
glfw3.lib(win32_joystick.c.obj) : error LNK2019: unresolved external symbol __imp__GetRawInputDeviceInfoA@16 referenced in function _supportsXInput
glfw3.lib(win32_joystick.c.obj) : error LNK2019: unresolved external symbol __imp__GetRawInputDeviceList@12 referenced in function _supportsXInput
glfw3.lib(wgl_context.c.obj) : error LNK2019: unresolved external symbol __imp__ChoosePixelFormat@8 referenced in function _loadWGLExtensions
glfw3.lib(wgl_context.c.obj) : error LNK2019: unresolved external symbol __imp__DescribePixelFormat@16 referenced in function __glfwCreateContextWGL
glfw3.lib(wgl_context.c.obj) : error LNK2019: unresolved external symbol __imp__SetPixelFormat@12 referenced in function __glfwCreateContextWGL
glfw3.lib(wgl_context.c.obj) : error LNK2019: unresolved external symbol __imp__SwapBuffers@4 referenced in function _swapBuffersWGL
MSVCRT.lib(_chandler4gs_.obj) : error LNK2019: unresolved external symbol __except_handler4_common referenced in function __except_handler4
a.exe : fatal error LNK1120: 92 unresolved externals
clang++.exe: error: linker command failed with exit code 1120 (use -v to see invocation)

让我们演示一下算法。

  1. 让我们看到未解析符号之间的__imp__GetRawInputDeviceInfoA
  2. 谷歌GetRawInputDeviceInfoA .
  3. 结果的第一页是 https://msdn.microsoft.com/en-us/library/windows/desktop/ms645597(v=vs.85(.aspx
  4. 打开页面并将其滚动到底部。我们看到那里| Library | User32.lib |.
  5. 将库添加到命令行:clang++ ..src*.cpp -L..lib -lglfw3 -lopengl32 -I..include -std=c++17 User32.lib
  6. 尝试构建。
  7. 从 1 到 6 重复,直到修复所有未解析的符号。

从命令行中删除..src*.h