我设置了简单的 OpenGl 项目(Clion IDE,msys 的 g++ 编译器) 我下载了glfw和glew源文件,用mingw32-make手动编译它们。 由于链接问题仍然无法构建项目(我猜)。同时,我以前用过这种方式 glfw 没有任何问题,只有添加 glew 库才开始出现问题。 我是 c++ 新手,所以这个错误让我很困惑。我已经没有办法修复它了。
cmake:
cmake_minimum_required(VERSION 3.14)
project(udemy1)
set(CMAKE_CXX_STANDARD 17)
add_executable(udemy1
src/main.cpp
)
add_library(glew STATIC IMPORTED)
add_library(glfw STATIC IMPORTED)
set_target_properties(glew PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/GLEW/libglew32.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
set_target_properties(glfw PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/GLFW/libglfw3.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(udemy1 glew)
target_link_libraries(udemy1 glfw)
源
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH = 800, HEIGHT = 600;//window dimensions
int main()
{
if (!glfwInit())
{
printf("GLFW init failed!");
glfwTerminate();
return 1;
}
//setup GLFW window props
//OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
//CORE FILE -> no backwards compatibility
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//allow forward compat
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow *mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "test window", NULL, NULL);
if(!mainWindow)
{
printf("GLFW window creation failed");
glfwTerminate();
return 1;
}
//get buffer size information
int bufferWidth, bufferHeight;
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
//set context for glew to use
glfwMakeContextCurrent(mainWindow);
//allow modern extentions
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK)
{
printf("glew init failed!");
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
//set up viewport size
glViewport(0, 0, bufferWidth, bufferHeight);
//loop untill window closed
while(!glfwWindowShouldClose(mainWindow))
{
//get and handle user input events
glfwPollEvents();//check for events
//clear window
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(mainWindow);//two scenes
}
return 0;
}
project structure:
udemy1
-build
-cmake-build-debug
-Dependencies
-GLEW
-glew32.dll
-glewinfo.exe
-libglew32.a
-libglew32.dll.a
-visualinfo.exe
-GLFW
-glfw3.dll
-libglfw3.a
-libglfw3dll.a
-include
-GL
-glew.h
-glxew.h
-wglew.h
-GLFW
-glfw3.h
-glfw3native.h
-src
-main.cpp
CmakeList.txt
编辑,附加错误。我的错
====================[ Build | all | Debug ]=====================================
"D:\DEV\cpp\cl\CLion 2019.2\bin\cmake\win\bin\cmake.exe" --build D:\DEV\cpp\projects\udemy1\build --target all -- -j 8
mingw32-make[1]: Entering directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[2]: Entering directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[2]: Leaving directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[2]: Entering directory 'D:/DEV/cpp/projects/udemy1/build'
[ 50%] Linking CXX executable udemy1.exe
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\udemy1.dir/objects.a(main.cpp.obj): in function `main':
D:/DEV/cpp/projects/udemy1/src/main.cpp:42:(.text+0xe9): undefined reference to `__imp_glewExperimental'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:44:(.text+0xf3): undefined reference to `__imp_glewInit'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:53:(.text+0x140): undefined reference to `glViewport'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:61:(.text+0x166): undefined reference to `glClearColor'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:62:(.text+0x170): undefined reference to `glClear'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\udemy1.dir\build.make:88: udemy1.exe] Error 1
mingw32-make[2]: Leaving directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[1]: *** [CMakeFiles\Makefile2:72: CMakeFiles/udemy1.dir/all] Error 2
mingw32-make[1]: Leaving directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make: *** [Makefile:83: all] Error 2
我确实尝试编译其他版本的glew。-同样的错误。 我使用 Visual Studio IDE 和预编译库复制了项目(并且它有效)。
静态链接到glew时,您需要定义
GLEW_STATIC
预处理器符号:
target_compile_definitions(glew PUBLIC GLEW_STATIC)
如果您不定义此符号,glew 将其所有函数定义为
dllimport
,这就是为什么您会看到 __imp_glewExperimental
缺少符号。