Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Cross compiling SFML project in ubuntu  (Read 2857 times)

0 Members and 1 Guest are viewing this topic.

metal_in_my_veins

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Cross compiling SFML project in ubuntu
« on: December 20, 2021, 10:32:12 am »
Hello I am trying to cross compile a simple SFML hello world program in ubuntu using mingw compiler.

What I did:
1. Created a test directory (mkdir sfmltest)
2. Moved the test program main.cxx in sfmltest (mv ~/main.cxx sfmltest)
3. Unzipped the windows build of SFML (GCC 7.3.0 MinGW (SEH) - 64-bit)
4. Copied the dlls of bin/ folder in sfmltest
5. Copied the include/ directory in /usr/local/include
6. Copied the lib/ directory in /usr/local/lib

7. Then I did this:
    x86_64-w64-mingw32-g++ main.cxx -o binary.exe -I/usr/local/include -L/usr/local/lib -lsfml-audio-s -lsfml-graphics-s -lsfml-network-s -lsfml-system-s -lsfml-window-s

8. And this is the output:
    /usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x34): undefined reference to `__imp__ZN2sf9VideoModeC1Ejjj'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x67): undefined reference to `__imp__ZN2sf6StringC1EPKcRKSt6locale'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0xe8): undefined reference to `__imp__ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x116): undefined reference to `__imp__ZNK2sf6Window6isOpenEv'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x132): undefined reference to `__imp__ZN2sf6Window9pollEventERNS_5EventE'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x14d): undefined reference to `__imp__ZN2sf6Window5closeEv'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x17b): undefined reference to `__imp__ZN2sf5ColorC1Ehhhh'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x196): undefined reference to `__imp__ZN2sf12RenderTarget5clearERKNS_5ColorE'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x1a6): undefined reference to `__imp__ZN2sf6Window7displayEv'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x1bb): undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccBIUR3s.o:main.cxx:(.text+0x206): undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
collect2: error: ld returned 1 exit status

So what's wrong here? My end target is to create a statically linked standalone binary which will run in windows.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10226
    • View Profile
    • development blog
    • Email
Re: Cross compiling SFML project in ubuntu
« Reply #1 on: December 20, 2021, 10:50:54 am »
Guess you successfully ignored the red box on the download page

Quote
The compiler versions have to match 100%!

Whatever cross-compiler you use certainly won't match the compiler used to build that SFML package. ;)

However, the given errors are not yet related to that, instead you just didn't declare SFML_STATIC so it's still looking for import (i.e. "__imp__" / shared library) symbols.
If you manage to get the cross-compilation to work, then congrats! From my personal experience however, it's much simpler to just boot up a Windows VM or utilize some of the free CI builds (GitHub Action / Appveyor), than having to deal with cross-compilation.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

metal_in_my_veins

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Cross compiling SFML project in ubuntu
« Reply #2 on: December 20, 2021, 11:02:59 am »
I just added '#define SFML_STATIC' on the top of main.cxx. Now the errors are even more bigger (undefined reference to glClearColor, glMatrixMode etc etc). Are these errors caused by unmatched compiler version?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10226
    • View Profile
    • development blog
    • Email
Re: Cross compiling SFML project in ubuntu
« Reply #3 on: December 20, 2021, 11:18:46 am »
No, when static linking, you also need to link all of SFML's dependencies, like OpenGL etc.
See the Getting Started tutorial, e.g. for Code::Blocks for a full list.

It's recommended to put the SFML_STATIC on the compilation level instead of in the source file.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

metal_in_my_veins

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Cross compiling SFML project in ubuntu
« Reply #4 on: December 20, 2021, 04:10:32 pm »
Look I need help :'( This is my new approach:

1. i686-w64-mingw32-g++ -DSFML_STATIC -c -I/usr/local/include main.cxx
2. i686-w64-mingw32-g++ main.o -L/usr/local/lib -lFLAC -lfreetype -logg -lopenal32 -lsfml-audio-s -lsfml-graphics-s -lsfml-system -lsfml-network -lsfml-window -lvorbis -lvorbisenc -lvorbisfile

output:
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x7b): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0xaa): undefined reference to `sf::String::String(char const*, std::locale const&)'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x132): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x160): undefined reference to `sf::Window::isOpen() const'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x188): undefined reference to `sf::Window::pollEvent(sf::Event&)'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x1a6): undefined reference to `sf::Window::close()'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x1db): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x1f4): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x204): undefined reference to `sf::Window::display()'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x216): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/i686-w64-mingw32-ld: main.o:main.cxx:(.text+0x2a5): undefined reference to `sf::RenderWindow::~RenderWindow()'
collect2: error: ld returned 1 exit status

NOTE: I also tried this way:
i686-w64-mingw32-g++ main.o /usr/local/lib/libFLAC.a /usr/local/lib/libfreetype.a /usr/local/lib/libogg.a .........

Still same result.

Why can't the linker detect the archive files? Those files (libogg.a, libfreetype.a libsfml-system-s.a etc etc) are in /usr/local/lib. I am not finding any clue about what should I do now.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10226
    • View Profile
    • development blog
    • Email
Re: Cross compiling SFML project in ubuntu
« Reply #5 on: December 20, 2021, 05:46:14 pm »
The order of the libraries is wrong, but is I guess only partially responsible for the errors.

My Rule of Thumb for linking libraries is: If X depends on Y, X has to come before Y.

As all SFML modules depend on sfml-system, sfml-system has to be listed last.
Also sfml-graphics depends on freetype and sfml-audio depends on flac, ogg and openal, so those should be listed last.

Then you're missing system libs, which I don't even know how to link cross-platform, you'd need winmm, gdi32 and if use networking, also ws2_32.

And don't forget what I wrote at the beginning, you'll have to build SFML yourself, to be compatible with your given cross-compiler.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/