我一直在尝试为Visual Studio Code建立一个C ++环境,直到我尝试将其与SDL2一起使用为止。我使用的编译器是mingw64中包含的“ g ++”。我为mingw下载了SDL2.09开发库。我将所有头文件复制并粘贴到include文件夹中(这是正确的,因为我没有关于找不到头文件的错误),然后将libs粘贴到libs文件夹中(编译器也能够找到它们) 。我还复制了dll并将其粘贴到编译代码的目录中。然后,我构建了程序并不断出错。根据库的顺序,有时可能有多个“ main”或关于winMain的定义。有时它说一堆SDL东西是不确定的。但是,每次运行它时,一致的是,当我使用SDL_init或任何SDL函数时,都会说该引用未定义。
我已经包括添加mingw32库以及其他库。我还尝试过重新排列库的顺序,这样会得到不同错误消息的不同结果,但是没有成功。我曾尝试在eclipse ide中对相同的错误消息进行相同的设置。
"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"
}
}
]
}
^^^是构建文件所在的我的task.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;
}
^^是我尝试编译的cpp文件
尝试构建后出现一个错误:
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
C:\Users\YARHAR~1\AppData\Local\Temp\ccBO9BFb.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和SDL2main库的顺序后的其他错误:
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真正起作用。