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

Author Topic: How to link everything statically  (Read 1975 times)

0 Members and 1 Guest are viewing this topic.

pexdark

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to link everything statically
« on: May 29, 2023, 03:57:05 pm »
Hi! I wanted to create an app that would use as few files as possible. I know i should use static linking, but i'm not sure how to do so. For the sake of simplicity, i'm using as little of additional software as i can - that means i'm launching the app directly from console.

So far i was using a command like this:
g++ Shader.cpp -Iinclude -Llib -lsfml-graphics -lsfml-window -lsfml-system -lwinmm -lopengl32 -lgdi32 -o Shader.exe
where "include" and "lib" are the folder names, and i use the "Shader.cpp" file from the examples.

However when i try the same setup with a command like this:
g++ Shader.cpp -Iinclude -Llib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32 -o Shader.exe
it throws a bunch of errors like these:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<username>\AppData\Local\Temp\ccjZLZ16.o:Shader.cpp:(.text+0x8c): undefined reference to `__imp__ZN2sf6StringC1EPKcRKSt6locale&#39;

I should also mention i'm using MinGW, and my only expirience with cpp is making cmd applications.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to link everything statically
« Reply #1 on: May 29, 2023, 04:05:16 pm »
Have you defined SFML_STATIC?

You also need to link all of the dependencies for each module you use manually when linking statically.

There is a list here (in the table; scroll down):
https://www.sfml-dev.org/tutorials/2.5/start-cb.php#creating-and-configuring-a-sfml-project

Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

pexdark

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to link everything statically
« Reply #2 on: May 29, 2023, 04:19:47 pm »
I assume to define SFML_STATIC you just need to write #define SFML_STATIC at the beginning of the file.
Also i checked the entire list and sorted the libraries, so now the command looks like this:
g++ Shader.cpp -Iinclude -Llib -lwinmm -lsfml-system-s -lopengl32 -lgdi32 -lsfml-window-s -lfreetype -lsfml-graphics-s -o Shader.exe

When i try launching the command i get errors like these:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: lib/libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x461): undefined reference to `sf::err()&#39;
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: lib/libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x631): undefined reference to `sf::err()&#39;
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: lib/libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x761): undefined reference to `sf::err()&#39;

i guess that's some progress?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to link everything statically
« Reply #3 on: May 29, 2023, 04:51:14 pm »
In which file are defining SFML_STATIC?

I haven't used MinGW; I was just following what it said in the tutorial there but it's clearly for a more visual approach using Code::Blocks.

With that said, it does state that you "need to define the SFML_STATIC macro in the preprocessor options of your project" so how you do that using command line, I presume you'll know better than I do.


Another thing to pay attention to is that SFML needs to built with exactly the same compiler as your project so if you aren't using an exact match, I presume you're building SFML yourself (mainly because most of the "undefined reference" errors usually stem from mismatching versions)?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to link everything statically
« Reply #4 on: May 29, 2023, 05:19:44 pm »
Wrong linking order isn't it? It's supposed to be dependencies last (dependencies are listed in the link provided by hapax)

pexdark

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to link everything statically
« Reply #5 on: May 29, 2023, 05:26:01 pm »
Wrong linking order isn't it? It's supposed to be dependencies last (dependencies are listed in the link provided by hapax)
Oh, it worked!
You're right, all i had to do is assemble the dependencies in the right order:
g++ Shader.cpp -Iinclude -Llib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lwinmm -lopengl32 -lgdi32 -lfreetype -o Shader.exe
And silly me was just about to compile all of sfml on my own to check if there's some kind of issue with compatibility...
Thank you both so much for the help! Aside from resolving my issue it also pushed me to do some research and learn how exactly the compiler works.