我一直在尝试为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实际运行。
我遇到了同样的问题,但是改变顺序对我来说不起作用。这是task.json文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-lSDL2",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
这是c时的终端输出
ld:找不到-lSDL2的库clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)终端进程以退出代码终止:1