├── .gitignore ├── Demo.sln ├── Demo ├── Demo.cpp └── Demo.vcxproj ├── GenSymbolsHeader.py ├── Include └── LinkAllSymbols.h ├── LICENSE ├── MyLib ├── Dog.cpp ├── Dog.h ├── Fish.cpp ├── Fish.h ├── MyLib.vcxproj ├── MyLib.vcxproj.filters ├── Turtle.cpp └── Turtle.h └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | 198 | Debug/ 199 | Release/ 200 | -------------------------------------------------------------------------------- /Demo.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30501.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Demo", "Demo\Demo.vcxproj", "{A79C07FA-C603-4815-A9F9-79F45BCCB402}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MyLib", "MyLib\MyLib.vcxproj", "{7ED105A5-DF77-4E77-B222-370FEDB5A69D}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Win32 = Debug|Win32 13 | Release|Win32 = Release|Win32 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A79C07FA-C603-4815-A9F9-79F45BCCB402}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {A79C07FA-C603-4815-A9F9-79F45BCCB402}.Debug|Win32.Build.0 = Debug|Win32 18 | {A79C07FA-C603-4815-A9F9-79F45BCCB402}.Release|Win32.ActiveCfg = Release|Win32 19 | {A79C07FA-C603-4815-A9F9-79F45BCCB402}.Release|Win32.Build.0 = Release|Win32 20 | {7ED105A5-DF77-4E77-B222-370FEDB5A69D}.Debug|Win32.ActiveCfg = Debug|Win32 21 | {7ED105A5-DF77-4E77-B222-370FEDB5A69D}.Debug|Win32.Build.0 = Debug|Win32 22 | {7ED105A5-DF77-4E77-B222-370FEDB5A69D}.Release|Win32.ActiveCfg = Release|Win32 23 | {7ED105A5-DF77-4E77-B222-370FEDB5A69D}.Release|Win32.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /Demo/Demo.cpp: -------------------------------------------------------------------------------- 1 | // Test.cpp : Defines the entry point for the console application. 2 | // 3 | 4 | #include 5 | #include 6 | #include "LinkAllSymbols.h" 7 | 8 | // For test /OPT:NOREF Option 9 | int UnusedFunction() 10 | { 11 | printf("Never b called"); 12 | return 0; 13 | } 14 | 15 | int _tmain(int argc, _TCHAR* argv[]) 16 | { 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Demo/Demo.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {A79C07FA-C603-4815-A9F9-79F45BCCB402} 15 | Win32Proj 16 | Demo 17 | 18 | 19 | 20 | Application 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | $(SolutionDir)$(Configuration)\$(ProjectName)\ 45 | 46 | 47 | false 48 | $(SolutionDir)$(Configuration)\$(ProjectName)\ 49 | 50 | 51 | 52 | NotUsing 53 | Level3 54 | Disabled 55 | _SCL_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 56 | true 57 | ../Include;../MyLib 58 | MultiThreadedDebug 59 | 60 | 61 | Console 62 | true 63 | 64 | 65 | 66 | 67 | 68 | Level3 69 | NotUsing 70 | MaxSpeed 71 | true 72 | true 73 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 74 | true 75 | ../Include;../MyLib 76 | 77 | 78 | Console 79 | true 80 | true 81 | true 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | {7ed105a5-df77-4e77-b222-370fedb5a69d} 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /GenSymbolsHeader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2015 coderzh 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | """ 25 | 26 | import argparse 27 | import subprocess 28 | import re 29 | 30 | regex = re.compile(r"\s+.*\s([\?_]+.*)") 31 | 32 | exclude = [] 33 | 34 | def gen_header_file_for_lib(lib_path, header_path): 35 | cmd = ['dumpbin.exe','/linkermember:1', lib_path] 36 | lines = execute_command(cmd) 37 | symbols = find_matches(lines, regex, exclude) 38 | 39 | with open(header_path, 'w') as f: 40 | header_guard = "LINK_ALL_SYMBOLS_H_" 41 | f.write("#ifndef " + header_guard + '\n') 42 | f.write("#define " + header_guard + '\n') 43 | f.write("// Generated by GenLinkerSymbols.py. Do not modify! \n\n") 44 | 45 | for symbol in symbols: 46 | pragma_line = '#pragma comment(linker, "/include:' + symbol + '")' 47 | f.write(pragma_line + '\n') 48 | f.write("\n#endif // " + header_guard + '\n') 49 | 50 | print("Link symbols count: %s" % len(symbols)) 51 | 52 | 53 | def execute_command(cmd): 54 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 55 | (std_out, std_err) = p.communicate() 56 | if p.returncode != 0: 57 | raise subprocess.CalledProcessError(p.returncode, cmd) 58 | 59 | std_out_lines = std_out.splitlines() 60 | return std_out_lines 61 | 62 | def find_matches(lines, regex, exclude_list): 63 | def match(line): 64 | m = regex.match(line) 65 | if m: 66 | return m.group(1).split()[0] 67 | return None 68 | 69 | def exclude_filter(line): 70 | if not line: 71 | return False 72 | 73 | for exclude in exclude_list: 74 | if line.find(exclude) >= 0: 75 | return False 76 | return True 77 | 78 | matches = filter(exclude_filter, map(match, lines)) 79 | return list(set(matches)) 80 | 81 | 82 | if __name__ == '__main__': 83 | parser = argparse.ArgumentParser(description='Gen linker all symbols header file') 84 | parser.add_argument('lib', help='.lib file path') 85 | parser.add_argument('header', help='output .h file path') 86 | args = parser.parse_args() 87 | gen_header_file_for_lib(args.lib, args.header) 88 | -------------------------------------------------------------------------------- /Include/LinkAllSymbols.h: -------------------------------------------------------------------------------- 1 | #ifndef LINK_ALL_SYMBOLS_H_ 2 | #define LINK_ALL_SYMBOLS_H_ 3 | // Generated by GenLinkerSymbols.py. Do not modify! 4 | 5 | #pragma comment(linker, "/include:?Download@@YAHXZ") 6 | #pragma comment(linker, "/include:??_C@_08EMEDHABH@Dog?5run?4?$AA@") 7 | #pragma comment(linker, "/include:??_C@_0M@KEAKLOKJ@Turtle?5run?4?$AA@") 8 | #pragma comment(linker, "/include:?Run@Dog@@QAEXXZ") 9 | #pragma comment(linker, "/include:?Foo@@YAHXZ") 10 | #pragma comment(linker, "/include:??4Turtle@@QAEAAV0@ABV0@@Z") 11 | #pragma comment(linker, "/include:?Run@Turtle@@QAEXXZ") 12 | #pragma comment(linker, "/include:?Run@Fish@@QAEXXZ") 13 | #pragma comment(linker, "/include:?FishRun@@YAXXZ") 14 | 15 | #endif // LINK_ALL_SYMBOLS_H_ 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 coderzh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MyLib/Dog.cpp: -------------------------------------------------------------------------------- 1 | #include "Dog.h" 2 | #include 3 | 4 | void Dog::Run() 5 | { 6 | printf("Dog run."); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /MyLib/Dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H_ 2 | #define DOG_H_ 3 | 4 | class Dog 5 | { 6 | public: 7 | void Run(); 8 | }; 9 | 10 | int Foo() 11 | { 12 | return 1; 13 | } 14 | 15 | #endif // DOG_H__ 16 | -------------------------------------------------------------------------------- /MyLib/Fish.cpp: -------------------------------------------------------------------------------- 1 | #include "Fish.h" 2 | 3 | void Fish::Run() 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /MyLib/Fish.h: -------------------------------------------------------------------------------- 1 | #ifndef FISH_H_ 2 | #define FISH_H_ 3 | 4 | #include 5 | 6 | void FishRun() 7 | { 8 | 9 | } 10 | 11 | class Fish 12 | { 13 | public: 14 | Fish() 15 | { 16 | printf("new fish\n"); 17 | } 18 | 19 | void Run(); 20 | void Do() 21 | { 22 | 23 | } 24 | }; 25 | 26 | #endif // FISH_H__ 27 | -------------------------------------------------------------------------------- /MyLib/MyLib.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {7ED105A5-DF77-4E77-B222-370FEDB5A69D} 15 | Win32Proj 16 | MyLib 17 | 18 | 19 | 20 | StaticLibrary 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | StaticLibrary 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | false 44 | $(SolutionDir)$(Configuration)\$(ProjectName)\ 45 | 46 | 47 | $(SolutionDir)$(Configuration)\$(ProjectName)\ 48 | 49 | 50 | 51 | NotUsing 52 | Level3 53 | Disabled 54 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 55 | true 56 | ../Include 57 | MultiThreadedDebug 58 | 59 | 60 | Windows 61 | true 62 | 63 | 64 | 65 | 66 | python ..\GenSymbolsHeader.py $(OutDir)$(TargetName)$(TargetExt) ..\Include\LinkAllSymbols.h 67 | 68 | 69 | 70 | 71 | Level3 72 | NotUsing 73 | MaxSpeed 74 | true 75 | true 76 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 77 | true 78 | ../Include 79 | 80 | 81 | Windows 82 | true 83 | true 84 | true 85 | MyLib.def 86 | 87 | 88 | python ..\GenSymbolsHeader.py $(OutDir)$(TargetName)$(TargetExt) ..\Include\LinkAllSymbols.h 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /MyLib/MyLib.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {36b536e5-42ed-49dd-92cd-820c3f9f66f3} 6 | 7 | 8 | 9 | 10 | src 11 | 12 | 13 | src 14 | 15 | 16 | src 17 | 18 | 19 | 20 | 21 | src 22 | 23 | 24 | src 25 | 26 | 27 | src 28 | 29 | 30 | -------------------------------------------------------------------------------- /MyLib/Turtle.cpp: -------------------------------------------------------------------------------- 1 | #include "Turtle.h" 2 | #include 3 | 4 | void Turtle::Run() 5 | { 6 | printf("Turtle run."); 7 | } 8 | 9 | int Download() 10 | { 11 | return 1; 12 | } 13 | 14 | bool Upload(const char* file) 15 | { 16 | return true; 17 | } 18 | -------------------------------------------------------------------------------- /MyLib/Turtle.h: -------------------------------------------------------------------------------- 1 | #ifndef TURTLE_H_ 2 | #define TURTLE_H_ 3 | 4 | class __declspec(dllexport) Turtle 5 | { 6 | public: 7 | void Run(); 8 | }; 9 | 10 | int Download(); 11 | 12 | static bool Upload(const char* file); 13 | 14 | #endif // TURTLE_H__ 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | When linking a static library against an executable, unreferenced symbols are normally discarded. Under Unix where we use gcc, we can pass the flag **--whole-archive** to the linker ld, which makes ld not discard any objects. 2 | 3 | When under Windows Visual Studio, there is no equivalent to GNU ld option --whole-archive. The linker option /OPT:NOREF also doesn't work. 4 | 5 | There are two ways to force link specify symbols from static library。 6 | 7 | * Linker Option use /INCLUDE xxx 8 | * Add code : #pragma comment(linker, "/include:xxx") 9 | 10 | Use #pragma is more flexible. So we can use **dumpbin** tool to list all symbols, then generate Header File use #pragma comment(linker, "/inculde:xxx") to inlude all symbols. 11 | 12 | *Make sure **dumpbin.exe** be executed in Developer Command Prompt* 13 | 14 | Or write a batch command file, with the code(if installed Visual Studio 2013): 15 | 16 | ``` 17 | @echo off 18 | if defined VS120COMNTOOLS ( 19 | call "%VS120COMNTOOLS%\vsvars32.bat") 20 | ``` 21 | 22 | The main **GenSymbolsHeader.py** execute dumpbin.exe, use regex expression to match all symbols, then generate Header File. 23 | 24 | To automated generate LinkAllSymbols.h, in static library Post-Build Event, add this: 25 | 26 | ``` 27 | python ..\GenSymbolsHeader.py $(OutDir)$(TargetName)$(TargetExt) ..\Include\LinkAllSymbols.h 28 | ``` 29 | 30 | --------------------------------------------------------------------------------