我想用 C++ 制作图形用户界面应用程序,发现 SFML 是一个不错的选择。幸运的是我在 linux 上,所以我的系统上已经安装了 SFML(2.4)。 所以我开始搜索一些教程并找到了一个制作简单窗口的教程。但是当我运行代码时,我收到一条错误消息,指出对 sf::(我正在使用的函数) 的 undefined reference 。这是代码
#include <SFML/Graphics.hpp>
int main(void)
{
sf::RenderWindow window(sf::VideoMode(640,480),"SFML working");
return 0;
}
这是错误日志。
cd '/home/jasper/NetBeansProjects/SFML'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/jasper/NetBeansProjects/SFML'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/sfml
make[2]: Entering directory '/home/jasper/NetBeansProjects/SFML'
mkdir -p dist/Debug/GNU-Linux
g++ -o dist/Debug/GNU-Linux/sfml build/Debug/GNU-Linux/main.o
build/Debug/GNU-Linux/main.o: In function `main':
/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::String::String(char const*, std::locale const&)'
/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::RenderWindow::~RenderWindow()'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU-Linux/sfml' failed
make[2]: *** [dist/Debug/GNU-Linux/sfml] Error 1
make[2]: Leaving directory '/home/jasper/NetBeansProjects/SFML'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/jasper/NetBeansProjects/SFML'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 172ms)
我尝试在谷歌上搜索解决方案,但找不到任何有效的解决方案。所以我认为一些专家意见会很好。请帮忙。谢谢
最佳答案
您忘记链接 SFML 库。这些至少是:
- sfml 图形
- sfml 窗口
- sfml 系统
作为documentation解释说,在 Linux 上可以这样做:
g++ myapp.o -o myapp -lsfml-graphics -lsfml-window -lsfml-system
或者,如果使用 IDE,请在链接器设置中将它们指定为附加依赖项。
关于c++ - 对 sf::的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500451/