C++/OpenGL应用程序编译问题

源码:计算机图形学编程 使用OpenGL和C++

// 计算机图形学编程 使用OpenGL和C++
// 2.1.1 C++/OpenGL应用程序
// https://www.epubit.com/onlineEbookReader?id=6b230683-3021-4c0f-bc36-88259c8f4526&pid=4be35db1-fb7f-4a90-91cc-bf6144969a97&isFalls=true&src=normal

#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>

using namespace std;

void init(GLFWwindow* window) { }

void display(GLFWwindow* window, double currentTime) { 
   glClearColor(1.0, 0.0, 0.0, 1.0); 
   glClear(GL_COLOR_BUFFER_BIT);
}

int main(void) { 
    if (!glfwInit()) { exit(EXIT_FAILURE); } 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 
...
}


报错信息:libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x172): undefined reference to `__imp_CreateDCW’

当设置了如下编译选项时,
-static-libgcc -lopengl32 -lglfw3 -lglew32
报错:

Compiling single file...
--------
- Filename: D:\xxx\c\hello_GLFW.cpp
- Compiler Name: TDM-GCC 9.2.0 64-bit Debug

Processing C++ source file...
--------
- C++ Compiler: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\bin\g++.exe
- Command: g++.exe "D:\xxx\c\hello_GLFW.cpp" -o "D:\xxx\c\hello_GLFW.exe" -g3 -I"D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\include" -I"D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\include" -I"D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\9.2.0\include" -I"D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\lib\gcc\x86_64-w64-mingw32\9.2.0\include\c++" -L"D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\lib" -L"D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib" -static-libgcc -lopengl32 -lglfw3 -lglew32 -g3
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x172): undefined reference to `__imp_CreateDCW'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1aa): undefined reference to `__imp_GetDeviceCaps'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1c8): undefined reference to `__imp_GetDeviceCaps'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x220): undefined reference to `__imp_GetDeviceCaps'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x285): undefined reference to `__imp_GetDeviceCaps'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x2ad): undefined reference to `__imp_DeleteDC'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xc4d): undefined reference to `__imp_GetDeviceCaps'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xc65): undefined reference to `__imp_GetDeviceCaps'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1287): undefined reference to `__imp_CreateDCW'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x12a5): undefined reference to `__imp_GetDeviceGammaRamp'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x12b8): undefined reference to `__imp_DeleteDC'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1413): undefined reference to `__imp_CreateDCW'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1431): undefined reference to `__imp_SetDeviceGammaRamp'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1444): undefined reference to `__imp_DeleteDC'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x237): undefined reference to `__imp_CreateDIBSection'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x2a5): undefined reference to `__imp_CreateBitmap'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x2d1): undefined reference to `__imp_DeleteObject'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x3d9): undefined reference to `__imp_DeleteObject'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x3e9): undefined reference to `__imp_DeleteObject'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xc47): undefined reference to `__imp_CreateRectRgn'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xcac): undefined reference to `__imp_DeleteObject'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x85f): undefined reference to `__imp_DescribePixelFormat'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xd85): undefined reference to `__imp_DescribePixelFormat'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1180): undefined reference to `__imp_SwapBuffers'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x15b5): undefined reference to `__imp_ChoosePixelFormat'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x15ce): undefined reference to `__imp_SetPixelFormat'
D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1a2b): undefined reference to `__imp_DescribePixelFormat'

D:/tool/Dev-Cpp_6.3_TDM-GCC_9.2_Portable/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1a6e): undefined reference to `__imp_SetPixelFormat'
collect2.exe: error: ld returned 1 exit status

Compilation results...
--------
- Errors: 1
- Warnings: 0
- Compilation Time: 0.98s

折腾半天,快放弃了,耐心阅读官网资料:
With MinGW or Visual C++ on Windows

The static version of the GLFW library is named glfw3. When using this version, it is also necessary to link with some libraries that GLFW uses.

When using MinGW to link an application with the static version of GLFW, you must also explicitly link with gdi32. Other toolchains including MinGW-w64 include it in the set of default libraries along with other dependencies like user32 and kernel32.

解决方法:

在编译选项里面添加 -lgdi32解决。
环境(
TDM-GCC 9.2.0 64-bit Debug ,
-static-libgcc -lopengl32 -lglfw3 -lglew32 -lgdi32
使用的自己源码编译的glfw-3.3.8文件)
备用文件glfw-3.3.8.bin.WIN64.zip
点击 64-bit Windows binaries
The current version is 3.3.8, which was released on July 22, 2022 . See the release notes for details.

对于32位编译(TDM-GCC 9.2.0 32-bit Debug )

glfw-3.3.8.bin.WIN32.zip点击32-bit Windows binaries 拷贝
\glfw-3.3.8.bin.WIN32\lib-mingw-w64目录下的文件到D:\tool\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\TDM-GCC-64\x86_64-w64-mingw32\lib32
问题解决。

链接库文件还有顺序的

贴一个VSCode的配置

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\tool\\Dev-Cpp_6.3_TDM-GCC_9.2_Portable\\TDM-GCC-64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                //"-static-libgcc",
                "-lopengl32",
                "-lglfw3",
                "-lglew32",
                "-lgdi32"
               /* "-lgraphics64",
                "-lgdiplus",
                "-luuid",
                "-lmsimg32",
                "-limm32",
                "-lole32",
                "-loleaut32",
                "-lwinmm",
                */
                //"-lfreeglut"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值