【问题标题】:Can't compile GLUT helloworld with MinGW无法使用 MinGW 编译 GLUT helloworld
【发布时间】:2015-04-20 13:45:30
【问题描述】:

我想设置 OpenGL 库并运行一个简单的 GL 程序:

#include <windows.h>
#include <Gl/glut.h>

int main()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f (0.25, 0.25, 0.0);
    glVertex3f (0.75, 0.25, 0.0);
    glVertex3f (0.75, 0.75, 0.0);
    glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush();
    return 0;
}

我正在使用 CLion 构建项目,因此这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)
project(MuspellsheimR)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(MuspellsheimR ${SOURCE_FILES})

我已经下载了 GLUT here 并将 glut.dll 放入 C:\Windows\SysWOW64 (以及 C:\Windows\System32 以防万一),将 glut.h 放入 C:\MinGW\include\GL 和glut32.lib 到 C:\MinGW\lib。 这是我得到的输出消息:

CMakeFiles\ProjectName.dir/objects.a(main.cpp.obj): In function `glutInit_ATEXIT_HACK':
c:/mingw/include/gl/glut.h:486: undefined reference to `__glutInitWithExit@12'
CMakeFiles\ProjectName.dir/objects.a(main.cpp.obj): In function `glutCreateWindow_ATEXIT_HACK':
c:/mingw/include/gl/glut.h:503: undefined reference to `__glutCreateWindowWithExit@8'
CMakeFiles\ProjectName.dir/objects.a(main.cpp.obj): In function `glutCreateMenu_ATEXIT_HACK':
c:/mingw/include/gl/glut.h:549: undefined reference to `__glutCreateMenuWithExit@8'
CMakeFiles\ProjectName.dir/objects.a(main.cpp.obj): In function `main':
PATH/main.cpp:7: undefined reference to `glClearColor@16'
PATH/main.cpp:8: undefined reference to `glClear@4'
PATH/main.cpp:10: undefined reference to `glOrtho@48'
PATH/main.cpp:11: undefined reference to `glBegin@4'
PATH/main.cpp:12: undefined reference to `glVertex3f@12'
PATH/main.cpp:13: undefined reference to `glVertex3f@12'
PATH/main.cpp:14: undefined reference to `glVertex3f@12'
PATH/main.cpp:15: undefined reference to `glVertex3f@12'
PATH/main.cpp:16: undefined reference to `glEnd@0'
PATH/main.cpp:17: undefined reference to `glFlush@0'

【问题讨论】:

  • 您收到了哪些错误消息?
  • @m.s.啊,是的,当然。添加到帖子中。
  • 永远不要将glut.dll 之类的东西放在C:\Windows 中。曾经。只需将其放在可执行文件旁边即可。
  • @rubenvb,也不起作用。仍然得到相同的错误输出
  • 这不是为了解决您的问题,@m.s 的回答。解决了您的问题(未链接到 glut 库)。

标签: windows opengl cmake mingw glut


【解决方案1】:

通常,您让 CMake 使用 find_package 处理依赖项。 您肯定错过了链接步骤:

project(MuspellsheimR)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
set(SOURCE_FILES main.cpp)
include_directories( ${OPENGL_INCLUDE_DIRS}  ${GLUT_INCLUDE_DIRS} )
add_executable(MuspellsheimR ${SOURCE_FILES})
target_link_libraries(MuspellsheimR ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )

【讨论】:

  • 好的,现在我收到了No rule to make target 'C:/Windows/System32/glut32.dll', needed by 'executable.exe'. Stop.
  • @arcadia 你有那个文件吗?无论如何,在您的find_package 之后,添加message (STATUS "GLUT_LIBRARY = ${GLUT_LIBRARY}") 并发布您的cmake 输出
  • @Antonio,我得到了GLUT_LIBRARY = C:/Windows/System32/glut32.dll,然后是同样的mingw32-make.exe[3]: *** No rule to make target 'C:/Windows/System32/glut32.dll', needed by 'MuspellsheimR.exe'. Stop. mingw32-make.exe[2]: *** [CMakeFiles/MuspellsheimR.dir/all] Error 2 mingw32-make.exe[1]: *** [CMakeFiles/MuspellsheimR.dir/rule] Error 2
  • @arcadia 你在做类似add_library(GLUT_LIBRARY ...) 的奇怪事情吗?你 CMakeLists.txt 应该本质上是 m.s.已发布,仅此而已
  • @arcadia 尝试以下所有方法:1) 运行 cmake 并从干净的目录构建 2) 删除您刚才提到的设置 GLUT_INCLUDE_DIR,因为它已经包含在 gs 的代码中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 2021-10-26
  • 2013-04-25
  • 2017-01-18
  • 1970-01-01
相关资源
最近更新 更多