环境

===============================

cmake版本: 3.16

编译器: vs 2019

glut: 3.7

===============================

假设GLUT的安装目录为G:\dev\glutdlls37beta.

1. 编写解决方案的CMakeLists.txt.

cmake_minimum_required(VERSION 3.10)

project(GLUT_DEMO)

set(CMAKE_PREFIX_PATH "G:/dev/glutdlls37beta")
set(GLUT_INCLUDE_DIR "G:/dev/glutdlls37beta")

find_package(GLUT REQUIRED)

# 如果未设置,报错, error LNK2026: 模块对于 SAFESEH 映像是不安全的。
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")

if (${GLUT_FOUND})
    MESSAGE(STATUS "Find GLUT")
    MESSAGE(STATUS "GLUT libraries: ${GLUT_LIBRARIES}")
    MESSAGE(STATUS "GLUT include : ${GLUT_INCLUDE_DIR}")
    MESSAGE(STATUS "GLUT glut_library : ${GLUT_glut_LIBRARY}")
endif(${GLUT_FOUND})

add_subdirectory(src)

2. 在src目录下建立项目文件夹, 编写项目的CMakeLists.txt

set(DEMO_SRC
    main.cpp)

include_directories(${GLUT_INCLUDE_DIR})
link_directories(${GLUT_INCLUDE_DIR})

# 将exe输出到单独的bin目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

link_libraries("glut32")

add_executable(hello ${DEMO_SRC})

3. 编写范例代码, main.cpp

#include <iostream>

#include <glut.h>

void draw(void)
{
    // TODO
}
int main(int argc, char* argv[])
{
    // initialize
    glutInit(&argc, argv);

    glutInitWindowSize(640, 480);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

    glutInitWindowPosition(100, 100);

    // create window
    glutCreateWindow("TestBench");

    

    glutDisplayFunc(draw);
    
    // enter main loop = for event.
    glutMainLoop();
    return 0;
}

4. 执行CMake程序,

GLUT-HelloWorld范例_HelloWorld

 

 5. 使用vs编写程序, 得到可执行文件, 运行效果如图.

GLUT-HelloWorld范例_HelloWorld_02

 

毕!

 

 

                            ------------ 勿在浮沙筑高台