问题描述
我想在 macOS 上使用 SDL2 库制作游戏.我已按照安装说明进行操作,并将 SDL2.framework 目录添加到/Library/Frameworks.但是,我无法使用 VS 代码构建我的代码,代码如下:
I wanted to make a game using the SDL2 library on macOS. I have followed the installation instructions and have added the SDL2.framework directory to /Library/Frameworks. But, I have not been able to build my code using VS code and here is the code:
#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;
}
这是我在构建它时遇到的错误:
and this is the error that I am getting while building it:
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
这是我的tasks.json:
Here is my tasks.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"
},
]
}
我已经回答了类似问题的多个答案,但无法解决这个问题.
I have gone through multiple answers to similar questions but am not able to get around this problem.
这个问题的奇怪之处在于代码在终端上运行良好.这是命令: 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)
The strange thing about this problem is that the code is running fine on terminal.Here is the command : 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
(the file's name is test.cpp)
推荐答案
用于在 Visual Code 上安装 SDL2 或其他库.按照以下步骤操作.
For installing SDL2 or other libraries on Visual Code. Follow the next steps.
首先,确保您的工作区具有这样的结构.
First, be sure your workspace has a structure like this.
├── bin
├── include
├── lib
├── Makefile
└── src
确保将各自的文件粘贴到 include 和 lib 上.
Be sure you paste the respective files on include and lib.
如果你用brew安装SDL2,你一定已经复制了以下内容:
If you install SDL2 with brew, you must have copied the content of:
/usr/local/Cellar/sdl2/2.0.5/include/
on include
和
/usr/local/Cellar/sdl2/2.0.5/lib/
on lib
接下来,使用以下代码创建 Makefile 并添加相应的库.
Next, create a Makefile with the code below and add the respective libraries.
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 + b 快捷键:
Actually, you can just run make
and it will be done, but for simplicity, you can make a task.json
file like this and run it with the cmd + shift + b
shorcut:
{
"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"
}
]
}
那一定能解决问题.
如果您在显示窗口时遇到问题,我会让您帮助了我.
If you're having a problem with the window showing, I let you what helped me.
参考文献:
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
这篇关于SDL2 不是基于 VS 代码构建的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!