使用 SFML (macOS) 手动链接和编译 C++ 程序 [英] Manually link and compile C++ program with SFML (macOS)
问题描述
我正在尝试使用 SFML 创建小型 C++ 程序,但我不想使用 Xcode 模板.我正在使用 VSCode.
I am trying to create small C++ program with SFML and I don't want to use the Xcode template. I am using VSCode.
我下载了 Mac 版 SFML,然后将头文件和动态库复制到我的项目目录中,如下所示:
I downloaded SFML for Mac, and copied the headers and dylibs to my project directory like so:
include/<- 从包含目录(SFML 目录和所有标题)复制内容
include/ <- copied contents from the include directory (SFML dir with all headers)
lib/<- 从 lib 文件夹中复制的内容(所有 dylib 文件)
lib/ <- copied contents from the lib folder (all the dylib files)
main.cpp 看起来像这样:
main.cpp looks like this:
#include "include/SFML/Graphics.hpp"
int main()
{
return EXIT_SUCCESS;
}
我用来编译的命令(成功):g++ main.cpp -o main -I include -L lib -l sfml-graphics -l sfml-window -l sfml-system
The command I use to compile (succesfully):
g++ main.cpp -o main -I include -L lib -l sfml-graphics -l sfml-window -l sfml-system
运行应用程序时出现以下错误:
And when running the app I get following error:
dyld: Library not loaded: @rpath/libsfml-graphics.2.5.dylib
Referenced from: /Users/mikal/code/cpp/sfml-box2d/./main
Reason: image not found
zsh: abort ./main
推荐答案
我需要通过添加这些链接器选项来告诉链接器在运行时在哪里查找动态库:
I needed to tell the linker where to look for the dynamic libraries at runtime by adding these linker options:
-Wl,-rpath ./lib
更多信息在这里:https://gcc.gnu.org/legacy-ml/gcc-help/2005-12/msg00017.html
所以编译 hello SFML 代码的完整命令是:g++ main.cpp -o main -I include -L lib -l sfml-system -l sfml-window -l sfml-graphics -l sfml-audio -l sfml-network -Wl,-rpath ./lib代码>
So the full command to compile for example the hello SFML code is:
g++ main.cpp -o main -I include -L lib -l sfml-system -l sfml-window -l sfml-graphics -l sfml-audio -l sfml-network -Wl,-rpath ./lib
这篇关于使用 SFML (macOS) 手动链接和编译 C++ 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!