'I failed to compile C++ code on Visual Studio Code

I'm using "glut" library and trying to compile code by gcc compiler, but it gives undesireble result. When I try to build command (Ctrl+Shift+B) on Visual Studio Code, The Terminal returns below error messages.

Executing task: gcc -I C:\MinGW\include -I C:\Users\Ayata\Documents\Work\freeglut-3.0.0\include -o c:\Users\Ayata\Documents\WorkReport\MakeGameByUsingCpp\main c:\Users\Ayata\Documents\WorkReport\MakeGameByUsingCpp\main.cpp
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Ayata\AppData\Local\Temp\cc0Hoyy5.o:main.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Ayata\AppData\Local\Temp\cc0Hoyy5.o:main.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit@8'`
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Ayata\AppData\Local\Temp\cc0Hoyy5.o:main.cpp:(.text+0x162): undefined reference to `glClear@4'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Ayata\AppData\Local\Temp\cc0Hoyy5.o:main.cpp:(.text+0x16a): undefined reference to `_imp__glutSwapBuffers@0'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.

I guess incorrect linking to library directory causes this error. But I don't know how to fix it. Would you tell me solution for this error? I'll show my codes below.

P.S. I've succeeded compiling simple "Hello World" program! It worked well!

main.cpp

#include <GL/glut.h> //importing glut library
int WindowPositionX = 100;
int WindowPositionY = 100;
int WindowWidth = 512;
int WindowHeight = 512;
char WindowTitle[] = "世界の始まり";

void Initialize(void);
void Display(void);

int main(int argc, char *argv[]){
  glutInit(&argc, argv);
  glutInitWindowPosition(WindowPositionX, WindowPositionY);
  glutInitWindowSize(WindowWidth, WindowHeight);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  glutCreateWindow(WindowTitle);
  glutDisplayFunc(Display);
  Initialize();
  glutMainLoop();
  return 0;
}
void Initialize(void){
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glEnable(GL_DEPTH_TEST);
}
void Display(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glutSwapBuffers();
}

My code task.json. I set -I option to search header file in appointed path. (In this case, it will search in C:\\Users\\Ayata\\Documents\\Work\\freeglut-3.0.0\\include)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build main",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-I",
                "C:\\MinGW\\include",
                "-I",
                "C:\\Users\\Ayata\\Documents\\Work\\freeglut-3.0.0\\include",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}",
                "${file}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true,
            }
        },
    ]
}

Other information:

OS: Windows 10 (64-bit)

gcc version: gcc (MinGW.org GCC-8.2.0-3) 8.2.0

header file glut.h is located at C:\Users\Ayata\Documents\Work\freeglut-3.0.0\GL.

Cpp file main.cpp is located at C:\Users\Ayata\Documents\WorkReport\MakeGameByUsingCpp\main.cpp.

Json file task.json is located at C:\Users\Ayata\Documents\WorkReport\MakeGameByUsingCpp\.vscode\tasks.json.

Image of my environment:



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source