首页 > 解决方案 > 对 `__imp_glClear' 的未定义引用

问题描述

最近我决定从教程中学习 CMake。
*这个系列确切地说:https://www.youtube.com/watch?v=ED-WUk440qc&t=2s*\

我在添加 GLFW 时遇到了困难,因为此错误消息不断出现: 编辑:如果我不使用 GLFW 库中的任何内容但仍然包含它,它可以正常工作

错误信息

    C:\dev\CMGL>compile

C:\dev\CMGL>cmake -DGLFW_BUILD_DOCS=OFF -S . -B bin -G Ninja
-- The C compiler identification is GNU 10.3.0
-- The CXX compiler identification is GNU 10.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Using Win32 for window creation
-- Looking for dinput.h
-- Looking for dinput.h - found
-- Looking for xinput.h
-- Looking for xinput.h - found
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/CMGL/bin

C:\dev\CMGL>cd bin

C:\dev\CMGL\bin>Ninja
[20/20] Linking CXX executable CMGL.exe
FAILED: CMGL.exe
cmd.exe /C "cd . && C:\mingw64\bin\c++.exe   CMakeFiles/CMGL.dir/Main.cpp.obj -o CMGL.exe -Wl,--out-implib,libCMGL.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/dev/CMGL/src   -LC:/dev/CMGL/vendor/GLFW/src src/libheader.a  vendor/GLFW/src/libglfw3.a  -lkernel32 -luser32 -lgdi32 
-lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/CMGL.dir/Main.cpp.obj:Main.cpp:(.text+0xc9): undefined reference to `__imp_glClear'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

我试图复制该系列中的每一位代码以及使用预编译的二进制文件,但同样的错误不断出现


- 主目录

编译.bat

cmake -DGLFW_BUILD_DOCS=OFF -S . -B bin -G Ninja
cd bin
Ninja
pause
CMGL
pause
cd ../

CMakeLists.txt

cmake_minimum_required(VERSION 3.19.0)
project(CMGL)
add_executable(${PROJECT_NAME} Main.cpp)

add_subdirectory(src)
add_subdirectory(vendor/GLFW)

target_include_directories(${PROJECT_NAME} 
    PUBLIC vendor/GLFW/include
)

target_link_directories(${PROJECT_NAME} 
    PRIVATE src
    PRIVATE vendor/GLFW/src
)

target_link_libraries(${PROJECT_NAME} 
    header
    glfw
)

主文件

#include <iostream>
#include "src/adder.h"

#include <GLFW/glfw3.h>

int main()
{
   std::cout << "test" << std::endl;
   std::cout << add(3,4); 

   GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
    
}

.gitmodules

任何较低的目录都未被篡改

[submodule "vendor/GLFW"]
    path = vendor/GLFW
    url = https://github.com/glfw/glfw.git

- /src

CMakeLists.txt

add_library(header adder.cpp)

加法器.cpp

#include "adder.h"

int add(int x, int y)
{
    return x + y;
}

加法器.h

int add(int x, int y);

标签: c++cmakeglfwninja

解决方案


推荐阅读