提问人:SI TUKANG GAMBAR 提问时间:11/4/2023 最后编辑:SI TUKANG GAMBAR 更新时间:11/4/2023 访问量:103
我无法通过 gcc 使用 SFML 库编译 c++
I Can't compile c++ with SFML library by gcc
问:
所以首先有一个名为 main.cpp 的文件,我想使用它来编译它:g++ -o sfml_example main.cpp -lsfml-graphics -lsfml-window -lsfml-system
顺便说一句,这是代码:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Window");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}
当我尝试编译它时,它只是这样说:/usr/bin/ld: cannot find -lsfml-graphics: No such file or directory /usr/bin/ld: cannot find -lsfml-window: No such file or directory /usr/bin/ld: cannot find -lsfml-system: No such file or directory collect2: error: ld returned 1 exit status
我也尝试这个输入:g++ -v -o sfml_example main.cpp -lsfml-graphics -lsfml-window -lsfml-system
它打印出以下输出:
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/13/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-13.2.1-20231011/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.1 20231011 (Red Hat 13.2.1-4) (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'sfml_example' '-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'sfml_example-'
/usr/libexec/gcc/x86_64-redhat-linux/13/cc1plus -quiet -v -D_GNU_SOURCE main.cpp -quiet -dumpdir sfml_example- -dumpbase main.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -version -o /tmp/ccDQjcr6.s
GNU C++17 (GCC) version 13.2.1 20231011 (Red Hat 13.2.1-4) (x86_64-redhat-linux)
compiled by GNU C version 13.2.1 20231011 (Red Hat 13.2.1-4), GMP version 6.2.1, MPFR version 4.2.0-p12, MPC version 1.3.1, isl version isl-0.24-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/13/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/13/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13
/usr/lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/backward
/usr/lib/gcc/x86_64-redhat-linux/13/include
/usr/local/include
/usr/include
End of search list.
Compiler executable checksum: eec6ea8c5ebefd8c87c81ea2002f55e3
COLLECT_GCC_OPTIONS='-v' '-o' 'sfml_example' '-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'sfml_example-'
as -v --64 -o /tmp/ccsGUG0P.o /tmp/ccDQjcr6.s
GNU assembler version 2.40 (x86_64-redhat-linux) using BFD version version 2.40-13.fc39
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/13/:/usr/libexec/gcc/x86_64-redhat-linux/13/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/13/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/13/:/usr/lib/gcc/x86_64-redhat-linux/13/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/13/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'sfml_example' '-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'sfml_example.'
/usr/libexec/gcc/x86_64-redhat-linux/13/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/13/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/13/lto-wrapper -plugin-opt=-fresolution=/tmp/ccw87IFm.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o sfml_example /usr/lib/gcc/x86_64-redhat-linux/13/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/13/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/13/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/13 -L/usr/lib/gcc/x86_64-redhat-linux/13/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/13/../../.. /tmp/ccsGUG0P.o -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/13/crtend.o /usr/lib/gcc/x86_64-redhat-linux/13/../../../../lib64/crtn.o
/usr/bin/ld: cannot find -lsfml-graphics: No such file or directory
/usr/bin/ld: cannot find -lsfml-window: No such file or directory
/usr/bin/ld: cannot find -lsfml-system: No such file or directory
collect2: error: ld returned 1 exit status
我试图找到lsfml文件的位置,并在/lib64中找到了它 注意:我使用的是 Fedora Linux Workspace 版本 39
因此,请任何能找到解决方案的人如何成功编译代码,然后发送解决方案。 谢谢!
答:
我从未使用过但运行 Docker 映像,我只在裸露的新 Fedora 安装上执行了以下步骤,它编译和链接良好。fedora,
fedora:latest
启动 Fedora 的 Docker 镜像:
docker run -it -v "$(pwd)":/work fedora:latest
Fedora 容器内部:
sudo dnf install g++ pkg-config SFML-devel
cd /work
g++ main.cpp $(pkg-config --libs --cflags sfml-all) -o program
如果您的系统没有按照这些步骤进行编译,我会删除并重新安装这三个软件包。
评论
我只是自己试穿,重新安装软件包,只安装非 C 软件包,然后尝试运行此命令:它有效!g++ sfml.cpp -o prog -lsfml-graphics -lsfml-window -lsfml-system
我只是按照这家伙的指示:https://youtu.be/5aWMIpRiuww?si=EHxkKw0nxYdc18D1
评论
-L /lib64
gcc -L/lib64 -lsfml-graphics -lsfml-window -lsfml-system main7.cpp -o main7
仍然具有与以前相同的输出g++ main7.cpp ...
-v