所以, 我尝试在 C++ 项目中使用 SDL2 库(在 Visual Studio Code 上)。我使用Mingw来编译,但我一直有错误:
D:\Path/View/View.cpp:20: undefined reference to `SDL_Init'
D:\Path/View/View.cpp:21: undefined reference to `SDL_CreateWindow'
D:\Path/View/View.cpp:22: undefined reference to `SDL_CreateRenderer'
D:\Path/View/View.cpp:25: undefined reference to `SDL_GetError'
l:/common/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
我有以下任务来构建我的项目(SDL库包含在mingw文件夹中):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-Wall",
"-g","main.cpp",
"-o","main.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
我也尝试以下方法:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-Wall",
"-I","D:/Path/SDL2-2.0.8/i686-w64-mingw32/include",
"-L","D:/Path/SDL2-2.0.8/i686-w64-mingw32/lib",
"-lSDL2main",
"-lSDL2",
"-g","main.cpp",
"-o","main.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
Intellisense 可以与 include 路径一起正常工作:“D:/Path/SDL2-2.0.8/i686-w64-mingw32/include”。 我不知道我在哪里错过了什么:I
mingw 标志的顺序很重要(不知道为什么这么想):
"args": [
"-Wall",
"-g","main.cpp",
"-o","main.exe",
"-lmingw32",
"-lSDL2main",
"-lSDL2"
],
或者使用路径(-I 表示包含,-L 表示库)