c++ - SDL2 不是基于 VS 代码构建的
问题描述
我想在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
这是我的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"
},
]
}
我已经对类似问题进行了多个回答,但无法解决这个问题。
这个问题的奇怪之处在于代码在终端上运行良好。这是命令:(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)
解决方案
用于在 Visual Code 上安装 SDL2 或其他库。请按照以下步骤操作。
首先,确保您的工作区具有这样的结构。
├── bin
├── include
├── lib
├── Makefile
└── src
确保将相应的文件粘贴到include和lib上。
如果您使用 brew 安装 SDL2,您必须复制以下内容:
/usr/local/Cellar/sdl2/2.0.5/include/
上include
和
/usr/local/Cellar/sdl2/2.0.5/lib/
上lib
接下来,使用下面的代码创建一个 Makefile 并添加相应的库。
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
:
{
"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
推荐阅读
- delphi - Delphi 10.4.2 - 如何修复“元素中缺少必需的属性“包含”
“ 错误? - sql - 在 SQL 中连接日期和时间以获取 datetime: 1900-01-01 而不是实际日期错误
- python - 具有奇怪行为的 Keras 模型预测:“training=True”时高acc,否则不好
- amazon-web-services - aws quicksight - 未能满足约束:在此区域中无法访问指定的资源('us-east-1')
- html - What's the use of Content-Type header when the response is an attachment?
- django - 在我的仪表板的views.py 中写什么?
- linux - 在 Linux 中查找并复制包含模式的多个文件
- linux - 如何在 bash 脚本中获取分区标签
- html - 我的 CSS 代码在 Chrome 中不起作用,内联 CSS、内部 CSS 或外部 CSS 都不起作用
- r - 在 tidyverse 中动态添加多列