SDL2 不是基于 VS 代码构建的
SDL2 not being built on VS code
我想在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 + 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"
}
]
}
这必须解决问题。
如果你在窗口显示方面遇到问题,我会告诉你对我有帮助的东西。
引用:
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
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- Mingw-64 在构建和安装后不会编译 openCV 代码
- Clion显示错误,但可以使用Cmake成功构建代码
- 使用 g++7 构建的代码在访问未对齐的内存时崩溃
- 无法在 geany 上运行代码。无法执行构建命令
- 在Qt/C++中从一个代码构建两个略有不同的项目
- SDL2 不是基于 VS 代码构建的
- 使用 C 代码构建 Tensorflow lite micro
- setuptools:从C++代码构建共享库,然后构建链接到共享库的 Cython 包装器
- 我无法使用可视化工作室代码构建C++
- R dyn.load "Symbol not found"错误,即使C++代码构建良好
- 将c++代码构建为共享库
- 使用现有的cpp核心代码构建Web应用程序
- C++为使用armadillo和hdf5库的简单代码构建错误
- 使用站点的编译代码构建库时,我无法解决C++中的错误 (LNK2001)
- Eclipse中的非源代码构建
- 将测试代码构建为库或可执行文件
- iOS:从c++代码构建静态库失败
- xcode 6.X arm64代码构建失败,没有任何编译或链接错误
- 如何为交叉编译的代码构建单元测试