Visual Studio Static Linking for Standalone Exe

笑着哭i 提交于 2020-02-23 03:39:33

问题


I've read multiple posts with regards to this topic, but none of them have enabled me to build a statically linked exe.

In my release configuration (x64) I have the following:

Configuration Properties -> General : Use of MFC - Use MFC in a Static Library

Configuration Properties -> C/C++ -> Code Generation : Runtime Library - Multi-threaded (/MT)

Configuration Properties -> Linker -> Command Line : Additional Options - I have all the required Windows libs "kernel32.lib", etc. (as use of MFC removed them from the "All Options" window above)

Configuration Properties -> Manifest Tool -> Input and Output : Embed Manifest - No

Note that in Configuration Properties -> Linker -> Input there are 5 lib files that I'm using in my project, eg glfw3.lib and I'm using Full optimisation (/Ox).

After building the project and running the exe myself I receive errors "The code execution cannot proceed because glfw3.dll was not found..." etc.

Using dependencywalker I can see that it needs the dlls associated with the libs, which it of course cannot find.

Am I misunderstanding how to do this or is there something else that might be wrong?

(I'm using Visual Studio 2017)


回答1:


Yes, it appears you have a slight misunderstanding.

If something is offered as a DLL, then it is meant to be used as a DLL. There may be some way to incorporate a DLL into an executable, but it would be a hack. That's not how things are supposed to work.

The lib file that you are linking against exists simply in order to provide you with functions that you can link against, and these do nothing but delegate to the corresponding functions in the dynamically loaded DLL. Without it you would have to locate each entrypoint of the DLL yourself, which would be perfectly doable, but a bit cumbersome.

So: you must either find a version of glfw3 which is packaged as a statically linkable library (I have no idea whether one exists) or live with the fact that your .exe will need to be shipped together with glfw3.dll.



来源:https://stackoverflow.com/questions/45426047/visual-studio-static-linking-for-standalone-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!