首页 > 解决方案 > 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)

标签: c++macosvisual-studio-codelinker-errorssdl-2

解决方案


用于在 Visual Code 上安装 SDL2 或其他库。请按照以下步骤操作。

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

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

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

如果您使用 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


推荐阅读