【发布时间】:2020-05-30 13:43:41
【问题描述】:
我想在 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)
【问题讨论】:
-
我认为您的问题在这里:
"-F/Library/Frameworks -framework SDL2",我认为您需要将其分成 2 个参数而不是 1 个。"-F/Library/Frameworks", "-framework SDL2", -
@drescherjm 我试过了,但后来我明白了,错误:未知参数:'-framework SDL2'
-
@drescherjm 可能
-framework SDL2也应该分开。 -
@HolyBlackCat 非常感谢!它现在正在工作。我一直在这几个小时,终于解决了。
-
IDE 似乎引用了单个参数,因此
"-F/Library/Frameworks -framework SDL2",表示使用名为/Library/Frameworks -framework SDL2的文件夹作为 -F 的一部分,而不是按空格将其拆分为 3 个单独的参数
标签: c++ macos visual-studio-code linker-errors sdl-2