Build a streamlined version of the FreeGLUT library (use MinGW + VSCode + CMake gui to build the FreeGLUT library)

  • Build the FreeGLUT library using MinGW + VSCode + CMake gui
    • Prerequisites
      • Download MinGW
      • Download VSCode
      • Download CMake gui
      • DownloadFreeGLUT
    • Configuration process
    • test

Use MinGW + VSCode + CMake gui to build the FreeGLUT library

Prerequisite

Download MinGW

Download address: MinGW
Detailed download tutorials can be found online

Download VSCode

Download address: VSCode

After the installation of vscode is completed, you need to download these plug-ins.

Download CMake gui

Download address: Cmake
Be sure to download the gui version, not the source code

Download and run (After installation, the previous version of cmake on your computer may be uninstalled)
When running the installer, there will be an initial wait before clicking next.

HereThis must be emphasized, otherwise you will configure the environment variables yourself

If there is a version problem, just change to another version. According to my many experiments, version 3.27.4 will report an error. I don’t know about other versions.

CMake Deprecation Warning at CMakeLists.txt:1 (CMAKE_MINIMUM_REQUIRED):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

Download FreeGLUT

Download address: FreeGLUT

The downloaded source file compressed package is .tar.gz. It is troublesome to decompress. You need to go to the file path. Hold down Shift + right mouse button in the blank space and choose to open the Powershell window here.

Use this command to decompress
tar -xzvf archive.tar.gz -C C:\destination folder
archive.tar.gz is the name of the compressed package, and C:\target folder is the folder to which it is extracted.
It is recommended to download the .zip compressed package from Github, it is not troublesome.

Configuration process

  1. Find the installation path of C’ma’ke, mine is I:\CMake\bin

Open the cmake gui.exe program

  1. Select the source files and folder to build the project
    “Where is the source code” selects the path to your source file.
    “Where to build the binaries” selects the folder to use for the build (an empty folder is preferred).
  2. Click “Configure” and select the compiler


  1. Configuration options: CMake will try to automatically detect MinGW’s paths and configuration options. Check and adjust the following options (as needed):
 - CMAKE_INSTALL_PREFIX: Set the installation path of FreeGLUT.
      (Default is C:/Program Files (x86)/freeglut)
    - BUILD_SHARED_LIBS: Select whether to build shared libraries.
    - BUILD_STATIC_LIBS: Select whether to build static libraries.
  1. Click “Configure”: Click the “Configure” button and CMake will start configuration.

  2. Generate project files: Once the configuration is complete, click the “Generate” button and CMake will generate the MinGW Makefiles project file.

  3. Find your project path, which is the “Where to build the binaries” folder just now, right-click and select open with vscode

    If there is no VScode opening option, just go to vscode to open this folder

  4. Open terminal

  5. Enter the command mingw32-make and wait for the compilation to be completed. After the compilation is completed, a lib folder will be generated under the project folder, which contains the compiled dynamic library and static library!
    Successful effects:

  6. Install the library: If CMAKE_INSTALL_PREFIX was set in step 4, run the mingw32-make install command to copy the library and header files to the specified installation path.
    Successful effects:


Here I set the path of CMAKE_INSTALL_PREFIX in the project folder.

Test

  1. The following is a simple C++ example program using FreeGLUT. This example program creates a window and displays a simple colored triangle.
#include <GL/freeglut.h>

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    
    glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0); // red
    glVertex2f(0.0, 1.0);
    
    glColor3f(0.0, 1.0, 0.0); // green
    glVertex2f(-1.0, -1.0);
    
    glColor3f(0.0, 0.0, 1.0); // blue
    glVertex2f(1.0, -1.0);
    glEnd();
    
    glFlush();
}

int main(int argc, char** argv) {
    glutInit( & argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutCreateWindow("FreeGLUT Example");
    
    glClearColor(0.0, 0.0, 0.0, 1.0); //Set the background color to black
    
    glutDisplayFunc(display);
    glutMainLoop();
    
    return 0;
}

Create a new cpp file, copy the above code into it, save it, then compile and run.
2. Commands run on the command line

g + + main.cpp -o freeGLUTTest -lfreeglut -lopengl32 //Compile file
./freeGLUTTest //Run file

This compiles the source code file named main.cpp into an executable file named freeGLUTTest. The -lfreeglut and -lopengl32 options are used to link with the FreeGLUT and OpenGL libraries.

E.G.

  1. operation result

  1. You can also use makefiles to compile.
CC = g++
CFLAGS=-Wall-Wextra
LDFLAGS=-lfreeglut-lopengl32

myOpenGLApp: main.cpp
    $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

clean:
    rm -f myOpenGLApp
# myOpenGLApp Compiled file name, main.cpp source file required for compilation

Open the makefile and add the above code

and then use
mingw32-makeCompile
main.cpp
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

clean:
rm -f myOpenGLApp

MyOpenGLApp compiled file name, main.cpp source file required for compilation

Open the makefile and add the above code

and then use
Compile with `mingw32-make`
`./myOpenGLApp` runs