首页 > 解决方案 > 我未能在 Visual Studio Code 上完成 C++ 代码

问题描述

我正在使用“glut”库并尝试通过 gcc 编译器编译代码,但结果不理想。当我尝试在 Visual Studio Code 上构建命令(Ctrl+Shift+B)时,终端返回以下错误消息。

> 执行任务:gcc -IC:\MinGW\include -IC:\Users\Ayata\Documents\Work\freeglut-3.0.0\include -oc:\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): 未定义引用_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 终端进程以退出代码终止:1

终端将被任务重用,按任意键关闭它。

我猜不正确地链接到库目录会导致此错误。但我不知道如何解决它。你能告诉我这个错误的解决方案吗?我将在下面显示我的代码。

~ps~ 我已经成功编译了简单的“Hello World”程序!效果很好!

↓我的代码“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();
}

我的代码“task.json”。我设置了“-I”选项在指定路径中搜索头文件。(在这种情况下,它将在“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,
            }
        },
    ]
}

~其他信息~

操作系统:Windows 10(64 位)

gcc 版本:gcc (MinGW.org GCC-8.2.0-3) 8.2.0

头文件“glut.h”位于“C:\Users\Ayata\Documents\Work\freeglut-3.0.0\GL”。

cpp 文件“main.cpp”位于“C:\Users\Ayata\Documents\WorkReport\MakeGameByUsingCpp\main.cpp”。

json 文件“task.json”位于“C:\Users\Ayata\Documents\WorkReport\MakeGameByUsingCpp.vscode\tasks.json”。

↓我的环境图片

https://imgur.com/a/Z4w0Wt4

标签: c++gccvisual-studio-codeglut

解决方案


推荐阅读