SDL2 不是基于 VS 代码构建的

SDL2 not being built on VS code

本文关键字:代码 构建 VS SDL2      更新时间:2023-10-16

我想在macOS上使用SDL2库制作一个游戏。我按照安装说明进行操作,并将 SDL2.framework 目录添加到/Library/Frameworks。但是,我无法使用VS代码构建我的代码,这是代码:

#include <SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
cout << "not working" << endl;
else
cout << "working" << endl;
return 0;
}

这是我在构建它时遇到的错误:

ld: warning: directory not found for option '-F /Library/Frameworks -framework SDL2'
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-f9f99c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1

这是我的任务.json:

{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "shell: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-F/Library/Frameworks -framework SDL2",
"-I/Library/Frameworks/SDL2.framework/Versions/A/Headers",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
]
}

我已经对类似的问题进行了多次回答,但无法解决这个问题。

这个问题的奇怪之处在于代码在终端上运行良好。 这是命令:g++ test.cpp -I/Library/Frameworks/SDL2.framework/Versions/A/Headers -framework SDL2 -F/Library/Frameworks -o test && "/Users/shrishshankar/Desktop/Projects/Space_Invaders/"test(文件名为test.cpp(

用于在可视代码上安装 SDL2 或其他库。按照后续步骤操作。

首先,确保您的工作区具有这样的结构。

├── bin
├── include
├── lib
├── Makefile
└── src

确保将相应的文件粘贴到包含上。

如果使用 brew 安装 SDL2,则必须复制以下内容:

/usr/local/Cellar/sdl2/2.0.5/include/include

/usr/local/Cellar/sdl2/2.0.5/lib/lib

接下来,使用以下代码创建一个生成文件并添加相应的库。

CXX       := g++
CXX_FLAGS := -std=c++2a -ggdb
BIN     := bin
SRC     := src
INCLUDE_PATHS := include
LIBRARY_PATHS = lib
LIBRARIES   := -lSDL2 -lSDL2_image  #Don't forget that -l is the option
EXECUTABLE  := main
.PHONY: clean all
all: $(BIN)/$(EXECUTABLE)
run: clean all

./$(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
$(CXX) $(CXX_FLAGS) $^ -o $@ -I$(INCLUDE_PATHS) -L$(LIBRARY_PATHS) $(LIBRARIES)
#   Basically this is what we are doing.
#   g++ src/main.cpp -o main -Iinclude -Llib -lSDL2-2.0.0
clean:
-rm $(BIN)/*

实际上,你可以运行make它就会完成,但为了简单起见,你可以像这样制作一个task.json文件,并使用cmd + shift + bshorcut运行它:

{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make run",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}

对于调试,这里是launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}

这必须解决问题。

如果你在窗口显示方面遇到问题,我会告诉你对我有帮助的东西。

引用:

https://medium.com/@edkins.sarah/set-up-sdl2-on-your-mac-without-xcode-6b0c33b723f7

https://dev.to/talhabalaj/setup-visual-studio-code-for-multi-file-c-projects-1jpi