用visualstudio代码编译SDL2 我一直试图为VisualStudio代码设置一个C++环境,直到我尝试使用SDL2。我使用的编译器是mingw64中包含的“g++”。我为mingw下载了SDL2.09开发库。我将所有头文件复制并粘贴到include文件夹中(这是正确的,因为我没有发现无法找到头文件的错误),并将libs粘贴到libs文件夹中(编译器也能够找到它们)。我还将dll复制并粘贴到编译代码的目录中。然后,我建立了程序,不断地出错。根据库的顺序,有时可能会出现“main”或winMain的多个定义。有时它说一些SDL的东西是未定义的。然而,每次我运行它时,一致的是,当我使用SDL_init或任何SDL函数时,它会说引用未定义

用visualstudio代码编译SDL2 我一直试图为VisualStudio代码设置一个C++环境,直到我尝试使用SDL2。我使用的编译器是mingw64中包含的“g++”。我为mingw下载了SDL2.09开发库。我将所有头文件复制并粘贴到include文件夹中(这是正确的,因为我没有发现无法找到头文件的错误),并将libs粘贴到libs文件夹中(编译器也能够找到它们)。我还将dll复制并粘贴到编译代码的目录中。然后,我建立了程序,不断地出错。根据库的顺序,有时可能会出现“main”或winMain的多个定义。有时它说一些SDL的东西是未定义的。然而,每次我运行它时,一致的是,当我使用SDL_init或任何SDL函数时,它会说引用未定义,c++,visual-studio-code,g++,sdl-2,mingw-w64,C++,Visual Studio Code,G++,Sdl 2,Mingw W64,我已经包括了添加mingw32库以及其他库。我还尝试重新排列库的顺序,这会得到不同错误消息的不同结果,但没有成功。我尝试过在EclipseIDE中对相同的错误消息进行相同的设置 "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++_sdl2_build", "command": "D:\\Coding\\mingw64\\bin\\g++.exe", "arg

我已经包括了添加mingw32库以及其他库。我还尝试重新排列库的顺序,这会得到不同错误消息的不同结果,但没有成功。我尝试过在EclipseIDE中对相同的错误消息进行相同的设置

  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++_sdl2_build",
      "command": "D:\\Coding\\mingw64\\bin\\g++.exe",
      "args": [
        "-lmingw32",
        "-lSDL2main",
        "-lSDL2",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-w"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "D:\\Coding\\mingw64\\bin\\g++.exe",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "D:\\Coding\\mingw64\\bin"
      }
    }
  ]
}
^^^是生成文件所在的my tasks.json文件

#include <sdl.h>
#include <iostream>

using namespace std;

int main(int argc, char *args[])
{
      if (SDL_Init(SDL_INIT_VIDEO) < 0)
      {
            cout << "SDL init failed" << endl;
            return 1;
      }

      cout << "SDL init successful" << endl;

      SDL_Quit();
      return 0;
}
切换SDL2和SDL2主库顺序后的其他错误:

D:/Coding/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main]+0x0): multiple definition of `main'
D:/Coding/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0): first defined here
D:/Coding/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[OutOfMemory]+0x1a): undefined reference to `SDL_ShowSimpleMessageBox'
D:/Coding/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main_getcmdline]+0xe4): undefined reference to `SDL_SetMainReady'
D:/Coding/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main_getcmdline]+0x131): undefined reference to `SDL_ShowSimpleMessageBox'
C:\Users\YARHAR~1\AppData\Local\Temp\ccOsF7gq.o: In function `SDL_main':
d:/Coding/Github/cpp_coding/SDL_testing/sdl_test.cpp:8: undefined reference to `SDL_Init'
d:/Coding/Github/cpp_coding/SDL_testing/sdl_test.cpp:16: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
最好的错误情况是,在构建文件中,我没有包含mingw32库,并且库的顺序是SDL2main和SDL2。由此产生的错误有:

C:\Users\YARHAR~1\AppData\Local\Temp\ccjAeK0z.o: In function `SDL_main':
d:/Coding/Github/cpp_coding/SDL_testing/sdl_test.cpp:8: undefined reference to `SDL_Init'
d:/Coding/Github/cpp_coding/SDL_testing/sdl_test.cpp:16: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

我希望能够成功地编译程序,让SDL2真正工作。

库必须在使用它们的文件之后指定,因此您的
“${file}”
必须在任何库之前。另外,
-w
是有史以来最糟糕的想法-你想要更多的警告(例如,
-Wall-Wextra
),而不是一个警告都没有。库必须在使用它们的文件之后指定,因此你的
“${file}”
必须在任何库之前。而且
-w
是有史以来最糟糕的主意-你想要更多的警告(例如
-Wall-Wextra
),而不是一个警告都没有。
C:\Users\YARHAR~1\AppData\Local\Temp\ccjAeK0z.o: In function `SDL_main':
d:/Coding/Github/cpp_coding/SDL_testing/sdl_test.cpp:8: undefined reference to `SDL_Init'
d:/Coding/Github/cpp_coding/SDL_testing/sdl_test.cpp:16: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1