无法使用MinGW编译GLUT helloworld

时间:2015-04-20 13:45:30

标签: windows opengl cmake mingw glut

我想设置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 \将\ 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'

1 个答案:

答案 0 :(得分:2)

通常,您让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} )