如何解决对“__imp_glClear”的未定义引用
- G++ 版本:(Rev2,由 MSYS2 项目构建)10.3.0
- CMake 版本: 3.20.0
- 忍者版: 1.10.2
- GLFW 版本: 3.3.4
- 操作系统:Windows 10
- IDE:VSCode
最近我决定从教程中学习 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,--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
)
Main.cpp
#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)
Adder.cpp
#include "adder.h"
int add(int x,int y)
{
return x + y;
}
加法器.h
int add(int x,int y);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。