Search code examples
c++static-linking

How to configure my libraries and dependencies to compile my App as static


I am Programming an App with visual studio 2022, in c++, x64 with libraries installed with help of vcpkg. They are dynamic link libraries.

I want to use my App as static, to run it in another PC without need to install frameworks, or make an installer, or other dependencies, just one .exe and that´s it. Maybe is called "Portable"?

My AI asistent says that I should first change to Release, and change Code generation option to "/MT"

I do it and I started to experiment Linking problems, of course because my libraries are configured as dynamic. But...

Is that exactly the problem that I have in this message?, or do I have more? I can really not understand the Message.

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: class std::locale __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::getloc(void)const " (__imp_?getloc@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEBA?AVlocale@2@XZ)    WxWidgets_Structured    C:\Users\Juan\source\repos\WxWidgets_Structured\ConfigManager.obj   1   
Error   LNK1120 1 unresolved externals  WxWidgets_Structured    C:\Users\Juan\source\repos\WxWidgets_Structured\x64\Release\WxWidgets_Structured.exe    1   

Solution

  • As soon as you want to use something that you get through a DLL you cannot build a statically built program. Full stop.

    Possible ways:

    • most libraries come in either dynamic or static libraries: verify (twice) whether the packages that you use are provided also as static libraries

    • there are a number of one file packaging program tools. For example py2exe or pyinstaller take a Python interpretor, one or more scripts and the required libraries, pack everything into a kind of zip archive along with a bootstrap that unpacks everything into a temporary folder and then launch the Python interpretor with its main script. You could probably adapt a self-extracting zip file to immediately launch your program after extracting the archive containing the program and the required DLL.

      But that actually boils down to first building an installer and then have the installer immediately run the code and clean the installation after use. You could search whether InnoSetup of NSIS can be tweaked to obtain that...