使用第三方库时,Netbeans 无法加载共享库:SFML

Netbeans fails to load shared libraries when using third party library: SFML

我知道存在类似的问题,我已经看到了它们,但在我决定必须问之前的故障排除时间里没有找到它们。

我是 C++ 的新手,通过 class 学习它。我的作业要求我使用第三方库:SFML。我当前的编码设置是使用带有 C++ 插件的 Netbeans Windows 10。之前我使用的是 Cygwin 编译器。 SFML 声称它要求它与编译它的编译器之一的确切版本一起使用,所以我选择了 MinGW 7.3.0,我已经安装并继续尝试。

在"Project" -> Properties -> Build -> C++ Compiler中,我添加了SFML的includes目录:

"C:/Users/Drayux/Documents/Coding/NetBeans/Third Party Libraries/SFML-2.5.1/include"

在这里,在 -> 预处理器定义中我还添加了:

SFML_STATIC

按照教程的建议 here

在“项目”-> 属性-> 构建-> 链接器中,我添加了 SFML 的 lib 和 bin 目录:

"C:/Users/Drayux/Documents/Coding/NetBeans/Third Party Libraries/SFML-2.5.1/bin"

"C:/Users/Drayux/Documents/Coding/NetBeans/Third Party Libraries/SFML-2.5.1/lib"

最后,我确保在“链接器”部分的“库”部分下分别手动添加每个库。也按照上述教程的建议。

完成所有这些后,我可以编写一个编译和 运行s 的标准程序。然而,一旦我开始包含 SFML 库的 headers,有时代码会生成,但不会 运行,有时它根本不会一起生成。

拿这个例子代码:

#include <iostream>
using namespace std;

#include <SFML/Graphics.hpp>
using namespace sf;

int main() {
    cout << "Test output line" << endl;
    
    RenderWindow window(VideoMode(200, 200), "Hello there!");
    //CircleShape shape(100.f);
    
    return 0;
}

当我尝试在上述配置下构建它时,构建成功,但 运行 却没有。这是两个控制台。

建造:

cd 'C:\Users\Drayux\Documents\Coding\NetBeans\Lab7C'
C:\Program Files\MinGW\MSYS\bin\make.exe -f Makefile CONF=Debug
"/C/Program Files/MinGW/MSYS/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/Drayux/Documents/Coding/NetBeans/Lab7C'
"/C/Program Files/MinGW/MSYS/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/lab7c.exe
make.exe[2]: Entering directory `/c/Users/Drayux/Documents/Coding/NetBeans/Lab7C'
make.exe[2]: `dist/Debug/MinGW-Windows/lab7c.exe' is up to date.
make.exe[2]: Leaving directory `/c/Users/Drayux/Documents/Coding/NetBeans/Lab7C'
make.exe[1]: Leaving directory `/c/Users/Drayux/Documents/Coding/NetBeans/Lab7C'

BUILD SUCCESSFUL (total time: 3s)

运行:

C:/Users/Drayux/Documents/Coding/NetBeans/Lab7C/dist/Debug/MinGW-Windows/lab7c.exe: error while loading shared libraries: sfml_window-d-2.dll: cannot open shared object file: No such file or directory

RUN FAILED (exit value 127, total time: 74ms)

我也尝试过将 LD_LIBRARY_PATH 添加到项目属性中 运行 下的环境部分的解决方案,但没有成功。

考虑到我的情况,没有其他解决方案,我感到被困住了。非常感谢任何帮助。

谢谢, 利亚姆

您可能应该将 -static 传递给链接器参数,以防止链接此库的动态版本,或者当 "I made sure to manually add each of the libraries individually under the Libraries portion of the Linker section" 您不小心添加了动态版本库以及而不是只添加静态版本。

经过几个小时甚至更多的脏话,我能够成功地 运行 一个基本的 SFML 程序并呈现一个基本的测试 window。

我知道我还有很多东西要学,但我的解决方案是共享库(.dll 文件)的位置。

我原以为在链接器配置中引用它们就可以完成这项工作,但似乎 .a 库正在编译程序的本地目录中寻找它们。截至目前我仍然不知道如何更改它。

因此,解决方案是手动将文件系统中的 .dll 文件移动到已编译程序的目录中,在我的例子中:

C:\Users\Drayux\Documents\Coding\NetBeans\SFML Test\dist\Debug\MinGW-Windows

希望这对以后遇到我的问题的人有所帮助!