手动 link 并使用 SFML (macOS) 编译 C++ 程序

Manually link and compile C++ program with SFML (macOS)

我正在尝试使用 SFML 创建小型 C++ 程序,但我不想使用 Xcode 模板。 我正在使用 VSCode.

我为 Mac 下载了 SFML,并将 headers 和动态库复制到我的项目目录中,如下所示:

main.cpp 看起来像这样:

#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

当 运行 应用程序出现以下错误时:

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

我需要通过添加这些链接器选项来告诉链接器在运行时在哪里寻找动态库:

-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