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

Author Topic: Can´t static link when compiling.. (dynamic linking works)  (Read 1407 times)

0 Members and 1 Guest are viewing this topic.

digimikeh

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Can´t static link when compiling.. (dynamic linking works)
« on: August 17, 2023, 09:13:01 pm »
Hello!

i could not build with static linking... but built correctly with dynamic linking libraries.

the only thing i know is that static linking packages libraries into the executable file, so it is bigger than the size of dynamicaly packaged file..  i don´t know if there is another difference in runtime maybe..

Dynamic linking compiles fine, since docs talks about static linking only i do this question, can i go for dynamic linking fo my final shipping project?

Thanks.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Can´t static link when compiling.. (dynamic linking works)
« Reply #1 on: August 18, 2023, 01:13:03 am »
You haven't said what went wrong or which compiler you are using, so its hard to say how to fix it.
First thing to check: you need to define SFML_STATIC in your project (or at least make sure its defined before including any sfml headers).

Next, at least on Visual Studio, you need to link with every dependency. For example sfml-graphics-s.lib requires you to link with opengl32.lib and freetype.lib too. Dynamic linking doesn't need that, the dlls include their dependencies already, but static linking works differently and needs then explicitly.
You can find a list of which libraries you need here: https://www.sfml-dev.org/tutorials/2.6/start-vc.php
I don't know about other platforms, I don't use SFML on them.

The way static and dynamic linking works on Windows (.lib and .dll files):
Static linking - a .lib file (static library) contains precompiled but unlinked code, basically an archive of .obj files. When you link with the .lib, the linker will add any required parts to the final .exe file. It doesn't add parts that aren't used by the .exe. You can now distribute the .exe on its own.

Dynamic linking - a .dll contains fully compiled and linked code. There is also a tiny .lib (import library) that contains just a list of what's exported by the dll. When you link with the .lib, code is added to the .exe to find and load the .dll at runtime and call the code in it. You distribute the .exe and the .dlls, not the .lib

A static link will actually give you a smaller distributed size, because only the parts of the libraries that are used will be added to the final .exe. A dynamic link will give a smaller .exe size, but the .dlls need to be there too and they contain everything, even if its never needed, so the total size is bigger.
Although if the .dlls are already on the user's system in a suitable location (there's certain locations that are checked), you don't need to distribute them. But then if the user doesn't have them, the program will crash.

(There's other stuff, this is just the basics)

ethicszolpidem

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Can´t static link when compiling.. (dynamic linking works)
« Reply #2 on: September 15, 2023, 09:53:54 am »
The size of files can be checked. When libraries are linked in dynamically rather than statically, executables can be made smaller. This has the potential to improve distribution and cut down on storage needs

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Can´t static link when compiling.. (dynamic linking works)
« Reply #3 on: September 15, 2023, 01:45:10 pm »
Some actual numbers.
I built latest SFML 3 as both static and dynamic. Then I made a simple sfml test app that linked with graphics, system and window.

The exe is 12,288 bytes using dynamic sfml and 239,616 bytes using static sfml.

HOWEVER, the actual size for storage and distribution is 1,149,952 bytes for dynamic and 239,616 bytes for static. This is because dynamic sfml requires you to include 1,137,664 bytes of dlls in addition to the 12,288 byte exe. The static build doesn't need those extra files.

 

anything