Home > Enterprise >  I Can't compile c with SFML library by gcc
I Can't compile c with SFML library by gcc

Time:11-05

so first there is a file named main.cpp and I want to compile it using this: g -o sfml_example main.cpp -lsfml-graphics -lsfml-window -lsfml-system

btw this is the code:

#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;
}

and when I tried to compile it, it just said this: /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

I also try this input: g -v -o sfml_example main.cpp -lsfml-graphics -lsfml-window -lsfml-system

and it prints out this output:

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

I have tried to located where is the lsfml files, and I found it at /lib64 Note: I'm using Fedora Linux Workspace Version 39

So, please any people that can find the solution how to succeded to compile the code would send the solutions. Thanks!

CodePudding user response:

I just try on myself, reinstall the package, just install the non-C packages and then tried to run this command: g sfml.cpp -o prog -lsfml-graphics -lsfml-window -lsfml-system and it's work!

I Just followed this guy instructions: https://youtu.be/5aWMIpRiuww?si=EHxkKw0nxYdc18D1

CodePudding user response:

I have never used fedora, but running a Docker image of fedora:latest, I did just the following steps on a bare, new Fedora installation and it compiles and links fine.

Start Docker image of fedora:

docker run -it -v "$(pwd)":/work fedora:latest

Inside fedora container:

sudo dnf install g   pkg-config SFML-devel
cd /work
g   main.cpp $(pkg-config --libs --cflags sfml-all) -o program 

If your system doesn't compile with those steps, I would remove and reinstall those three packages.

  • Related