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

Author Topic: Static Linking  (Read 1758 times)

0 Members and 1 Guest are viewing this topic.

Mbutu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Static Linking
« on: July 11, 2023, 03:12:32 pm »
I made a git clone of SFML yesterday and managed to compile dynamic dlls. I even got the win32 example up and running.

After that I wanted to link the libraries statically and got some problems. I used VS Code and cmake on Windows 11 x64 to compile the libraries and I have been pretty careful to test different variants of x86 and x64 code to "match" the library and the example "win32" I'm trying to link.  Right now the lib files should be x86, and I'm trying to use cl like this:

Quote
PS C:\Users\Mbutu\Documents\projects\sfml\sfml-src\SFML\examples\win32> cl .\Win32.cpp /EHsc /std:c++17  -I ..\..\include\ ..\..\..\build\lib\release\sfml-graphics-s.lib ..\..\..\build\lib\Release\sfml-window-s.lib ..\..\..\build\lib\Release\sfml-system-s.lib advapi32.lib user32.lib gdi32.lib

I get 19 unresolved "__declspec(dllimport)" errors which I supposed is due to how I try to link the libs. If I do the same with dynamic libraries it all works fine.

This is my output.
Quote
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32535 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Win32.cpp
Microsoft (R) Incremental Linker Version 14.36.32535.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Win32.exe
Win32.obj
..\..\..\build\lib\release\sfml-graphics-s.lib
..\..\..\build\lib\Release\sfml-window-s.lib
..\..\..\build\lib\Release\sfml-system-s.lib
advapi32.lib
user32.lib
gdi32.lib
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Transformable::setPosition(class sf::Vector2<float> const &)" (__imp_?setPosition@Transformable@sf@@QAEXABV?$Vector2@M@2@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Transformable::setRotation(class sf::Angle)" (__imp_?setRotation@Transformable@sf@@QAEXVAngle@2@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Transformable::setOrigin(class sf::Vector2<float> const &)" (__imp_?setOrigin@Transformable@sf@@QAEXABV?$Vector2@M@2@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class sf::Vector2<float> const & __thiscall sf::Transformable::getOrigin(void)const " (__imp_?getOrigin@Transformable@sf@@QBEABV?$Vector2@M@2@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Texture::Texture(void)" (__imp_??0Texture@sf@@QAE@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Texture::~Texture(void)" (__imp_??1Texture@sf@@QAE@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall sf::Texture::loadFromFile(class std::filesystem::path const &,class sf::Rect<int> const &)" (__imp_?loadFromFile@Texture@sf@@QAE_NABVpath@filesystem@std@@ABV?$Rect@H@2@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class sf::Vector2<unsigned int> __thiscall sf::Texture::getSize(void)const " (__imp_?getSize@Texture@sf@@QBE?AV?$Vector2@I@2@XZ) referenced in function _main   
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::RenderTarget::clear(class sf::Color const &)" (__imp_?clear@RenderTarget@sf@@QAEXABVColor@2@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::RenderTarget::draw(class sf::Drawable const &,class sf::RenderStates const &)" (__imp_?draw@RenderTarget@sf@@QAEXABVDrawable@2@ABVRenderStates@2@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class sf::Time __thiscall sf::Clock::getElapsedTime(void)const " (__imp_?getElapsedTime@Clock@sf@@QBE?AVTime@2@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Clock::Clock(void)" (__imp_??0Clock@sf@@QAE@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual void __thiscall sf::Window::close(void)" (__imp_?close@Window@sf@@UAEXXZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Window::display(void)" (__imp_?display@Window@sf@@QAEXXZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::RenderWindow::RenderWindow(struct HWND__ *,struct sf::ContextSettings const &)" (__imp_??0RenderWindow@sf@@QAE@PAUHWND__@@ABUContextSettings@1@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall sf::RenderWindow::~RenderWindow(void)" (__imp_??1RenderWindow@sf@@UAE@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Sprite::Sprite(class sf::Texture const &)" (__imp_??0Sprite@sf@@QAE@ABVTexture@1@@Z) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall sf::Sprite::~Sprite(void)" (__imp_??1Sprite@sf@@UAE@XZ) referenced in function _main
Win32.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class sf::RenderStates const sf::RenderStates::Default" (__imp_?Default@RenderStates@sf@@2V12@B) referenced in function _main
Win32.exe : fatal error LNK1120: 19 unresolved externals

Help anyone?  :-[
« Last Edit: July 11, 2023, 03:19:18 pm by Mbutu »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Static Linking
« Reply #1 on: July 11, 2023, 04:17:22 pm »
I'm not an expert with CMake but I will just say that you haven't mentioned which compiler you are using to compile SFML. If someone is to help you, they'd likely need to know the compiler.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Static Linking
« Reply #2 on: July 11, 2023, 04:26:46 pm »
Have a look here for info on how to static link SFML: https://www.sfml-dev.org/faq.php#build-link-static
In particular, you need to define SFML_STATIC to change how all library functions are imported and you need to manually link with a bunch of libraries (that are automatic for dynamic linking) like opengl32 and freetype.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Static Linking
« Reply #3 on: July 11, 2023, 04:37:43 pm »
Right now the lib files should be x86, and I'm trying to use cl like this:
You really shouldn't be manually calling cl :D

I highly recommend using CMake, e.g. with the SFML CMake Template, or use Visual Studio directly.
Writing manual compiler or linker commands simply doesn't scale.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mbutu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Static Linking
« Reply #4 on: July 11, 2023, 05:25:11 pm »
Thanks for the help everyone.

I got frustrated and deleted the directory/workspace, made a new copy and did it all over again. This time I never changed the environment (stayed with x64, MSBuild Tools and the same version of the compiler the whole time). For some reason it's working now.

I guess I messed up some directive creating the lib files, maybe using an old outdated SDK. Its also possible my VS Code extension did not update all the binaries during rebuild ( I switched from Release (x86) to Release (x64) several times). The c++ and cmake extensions for VS Code is not always on point it seems.  And to be fair, me manually editing the makefiles was probably not helping. But its all good now  :)

You really shouldn't be manually calling cl :D

Hey now, its the other way around. Using too much automated VS-Code stuff makes me confused as to which .json /.yaml file is causing the Code-extension for CMake to malfunction and what is actually being passed to the the compiler.  ::)
« Last Edit: July 11, 2023, 05:39:31 pm by Mbutu »

 

anything