我在使用Cmake链接OpenGL项目时遇到问题。
为了使用OpenGL扩展,我下载了预构建的GLEW库二进制文件。但是在构建了我的项目之后,我得到了一堆警告:
skipping incompatible C:/Users/.../lib when searching for -lglew32"
字符串
对于所有4个二进制文件,我都尝试了链接(glew32.a,glew32.dll.a,glew32mx.dll.a,glew32mx.a)。结果,没有找到所需的库,链接失败。
我的CmakeList:
cmake_minimum_required(VERSION 3.17)
project(Graphics)
set(CMAKE_CXX_STANDARD 14)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenGL/include/GLFW)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenGL/lib/GLFW)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenGL/include/GLEW)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenGL/lib/GLEW)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenGL/include/GL)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenGL/lib/Freeglut)
include_directories(include)
add_executable(Graphics main.cpp)
set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_FLAGS -m32)
target_link_libraries(Graphics glu32 OpenGL32 libglew32mx.dll.a freeglut libglfw3.a)
add_custom_target(freeglutdll
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/OpenGL/bin/freeglut.dll ${CMAKE_BINARY_DIR}
)
add_custom_target(glew32mxdll
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/OpenGL/bin/glew32mx.dll ${CMAKE_BINARY_DIR}
)
add_custom_target(glew32dll
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/OpenGL/bin/glew32.dll ${CMAKE_BINARY_DIR}
)
add_custom_target(glfw3dll
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/OpenGL/bin/glfw3.dll ${CMAKE_BINARY_DIR}
)
add_dependencies(Graphics glew32mxdll)
add_dependencies(Graphics glew32dll)
add_dependencies(Graphics freeglutdll)
add_dependencies(Graphics glfw3dll)
型
我尝试编译的代码非常简单:
#include "OpenGL/include/GL/freeglut.h"
#include "OpenGL/include/GLEW/glew.h"
#include "OpenGL/include/GLFW/glfw3.h"
#include <iostream>
#include <cstdio>
int main(int argc, const char **argv)
{
if (!glfwInit())
{
std::cerr << "Error while initializing glfw" << std::endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow *window = glfwCreateWindow(1280, 720, "Voxel", nullptr, nullptr);
if (window == nullptr)
{
std::cerr << "Error creating window" << std::endl;
return 1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cerr << "Error while initializing glew" << std::endl;
return -1;
}
return 0;
}
型
完整的编译日志如下:
====================[ Build | Graphics | Debug ]================================
"C:\Program Files\JetBrains\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" --build "C:\Users\Anton\C++ project\Graphics\cmake-build-debug" --target Graphics -- -j 6
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Anton/C++ project/Graphics/cmake-build-debug
[ 0%] Built target freeglutdll
[ 0%] Built target glfw3dll
[ 50%] Linking CXX executable Graphics.exe
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW/libglew32mx.dll.a when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW/libglew32mx.a when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW/glew32mx.dll when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW\libglew32mx.a when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW/libglew32mx.dll.a when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW/libglew32mx.a when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Users/Anton/C__PRO~1/Graphics/OpenGL/lib/GLEW/glew32mx.dll when searching for -lglew32mx
C:/PROGRA~2/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lglew32mx
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Graphics.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Graphics.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Graphics.dir/rule] Error 2
mingw32-make.exe: *** [Graphics] Error 2
CMakeFiles\Graphics.dir\build.make:105: recipe for target 'Graphics.exe' failed
CMakeFiles\Makefile2:97: recipe for target 'CMakeFiles/Graphics.dir/all' failed
CMakeFiles\Makefile2:104: recipe for target 'CMakeFiles/Graphics.dir/rule' failed
Makefile:137: recipe for target 'Graphics' failed
型
我可能做错了什么,我该如何纠正?
1条答案
按热度按时间ki0zmccv1#
我很简单地解决了这个问题--只需要使用prebuild.lib文件并在开始时定义GLEW_STATIC