Statically linked EXE in Win 10 not working


#1

I am trying to produce a statically linked executable in Win 10 with Visual Studio (latest available versions/updates for both).
I have selected to link against static multi threaded library and static MFC library. I DO NOT use MFC in any way, I use only JUCE and all the rest is C++ code written by me.
Unfortunately the program does not compile, with 61 linker errors (all pointing to juce code):

1>include_juce_core.obj : error LNK2001: unresolved external symbol __imp__CoInitialize@4
1>include_juce_core.obj : error LNK2001: unresolved external symbol __imp__ShellExecuteW@24
1>include_juce_core.obj : error LNK2001: unresolved external symbol __imp__CoCreateInstance@20
1>include_juce_core.obj : error LNK2001: unresolved external symbol __imp__SHFileOperationW@4
1>include_juce_core.obj : error LNK2001: unresolved external symbol __imp__SHGetSpecialFolderPathW@16
1>include_juce_events.obj : error LNK2001: unresolved external symbol __imp__OleUninitialize@0
1>include_juce_events.obj : error LNK2001: unresolved external symbol __imp__CommandLineToArgvW@8
1>include_juce_events.obj : error LNK2001: unresolved external symbol __imp__OleInitialize@4
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__GetOutlineTextMetricsW@12
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__CreateFontIndirectW@4
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__SetMapMode@8
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__RemoveFontMemResourceEx@4
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__DeleteObject@4
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__GetGlyphOutlineW@28
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__GetGlyphIndicesW@20
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__SetMapperFlags@8
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__DeleteDC@4
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__GetTextMetricsW@8
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__GetDeviceCaps@8
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__EnumFontFamiliesExW@20
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__CreateCompatibleDC@4
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__GetKerningPairsW@12
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__SelectObject@8
1>include_juce_graphics.obj : error LNK2001: unresolved external symbol __imp__AddFontMemResourceEx@16
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__CombineRgn@16
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__CreateBitmap@20
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__SHGetMalloc@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__CoTaskMemAlloc@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__ExtractAssociatedIconW@12
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__RestoreDC@8
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__ExcludeClipRect@20
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__GetObjectW@12
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__DoDragDrop@16
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__GetRegionData@12
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__CreateRectRgn@16
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__RegisterDragDrop@8
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__GetPixel@12
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__SHBrowseForFolderW@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__CreateRectRgnIndirect@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__StretchDIBits@52
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__SHGetPathFromIDListW@8
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__GetSaveFileNameW@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__CreateDIBSection@24
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__GetOpenFileNameW@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__SaveDC@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__RevokeDragDrop@4
1>include_juce_gui_basics.obj : error LNK2001: unresolved external symbol __imp__BitBlt@36
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__VariantClear@4
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__SafeArrayAccessData@8
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__SafeArrayCreateVector@12
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__OleSetContainedObject@8
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__Shell_NotifyIconW@8
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__OleCreate@28
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__SafeArrayUnaccessData@4
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__SysAllocString@4
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__SysFreeString@4
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__VariantInit@4
1>include_juce_gui_extra.obj : error LNK2001: unresolved external symbol __imp__SafeArrayDestroy@4
1>include_juce_opengl.obj : error LNK2001: unresolved external symbol __imp__SetPixelFormat@12
1>include_juce_opengl.obj : error LNK2001: unresolved external symbol __imp__SwapBuffers@4
1>include_juce_opengl.obj : error LNK2001: unresolved external symbol __imp__ChoosePixelFormat@8

If I choose to link against MFC dll everything compiles and runs fine (but only on the PC used to compile the executable). How can I fix the problem, since I do not use MFC in any way?


#2

What setting do you have for Use of MFC (in General > Projects Defaults) exactly? I think that you should leave it to Use Standard Windows Libraries if you are not using MFC at all. Use MFC in a Static Library and Use MFC in a Shared DLL should be only selected when you are using MFC.

Could you also copy-paste the complete Linker > Command Line from Visual Studio? Maybe you are missing dependencies on Ole32.lib, Shell32.lib, Gdi32.lib, etc.


#3

Martin,
I investigated a bit more. As you said the option to link statically or dynamically against the MFC library was not the root cause of the problem (since I do not use the MFC lib anyway!). The problem was that if you tick the option to link statically against MFC, then from your linker command line the following list of system libs will automagically disappear:

“kernel32.lib” “user32.lib” “gdi32.lib” “winspool.lib” “comdlg32.lib” “advapi32.lib” “shell32.lib” “ole32.lib” “oleaut32.lib” “uuid.lib” “odbc32.lib” “odbccp32.lib”

I think this is a Visual Studio bug. If you add them back manually, then you can compile against MFC statically without linker errors … BUT

… the problem is still there!

Your EXE will still NOT be statically linked against those system libs (and possibly others), and it will fail to run on machines OTHER than the one you have compiled the EXE on.

I suspect this has something to do with the fact that the program is a 32 bit executable (my choice, to allow it to run even on “old” 32 bit OS installations), compiled on 64 bit Windows operating system (my development workstation). I try to execute it on another 64 bit windows 10 machine (I have no 32 bit windows installations to test), with the OS freshly installed and almost nothing else. Maybe that on the target machine there are not those “special” libs suitable to allow a 32 bit EXE to run in a 64 bit OS? I do not think so, since other commercial 32 bit apps I downloaded from the internet (i.e.: TeamViewer) are running fine on that machine!
Any idea?


#4

Problem solved.

After a whole saturday of digging, I have found that was my program silently crashing during initialization on any machine other than the development one.
The problem was an hardcoded path to a file resource (quickly introduced to get the darned thing working, to be refined “later”), that was right on the development machine, but wrong on any other. There was also a bold comment: “TODO: FIX THIS ASAP”.
But it was forgotten months ago in the development process, and emerged now during beta testing on non dev PCs.