📜 ⬆️ ⬇️

Build a Visual Studio project into a single file using ILMerge

app.exe, d1.dll d2.dll = app.exe Typically, the result of compiling a project is the assembly of the project, as well as its dependencies (Referenced Assemblies). However, it is sometimes necessary that the result be a single file, a single file, independent of other assemblies. For example, a simple utility that can be copied anywhere and it will work.

Example

Relatively speaking after:
     compile App \ App.csproj  
     dir App \ bin \ Release 
It turns out:
     App.exe
     dep1.dll
     dep2.dll 
We need only one self-sufficient
     App.exe 
That is, it contains dep1.dll and dep2.dll

On Habré there is already a solution with embedding dependencies in resources, here I will show how to do this using ILMerge and Post Build Event in Visual Studio.
')

Sources
Instruments

ILMerge - A program from Microsoft Research, which actually has the required functionality.
merge_all.bat - we will use it in the Post-build event.

Training

Download and install ILMerge .
Put %PROGRAMFILES%\ILMerge\ILMerge.exe in the ${SolutionDir}ILMerge\
In the same place create the file merge_all.bat
Add a line to [Project-> Properties-> Build Events-> Post-build event]:
"$(SolutionDir)\ILMerge\merge_all.bat" "$(SolutionDir)" "$(TargetPath)" $(ConfigurationName)

Content merge_all.bat

All builds from $ output will merge into one. If the Debug configuration is in $ output \ Output, if Release, then as a result there will be only one file in $ output. Information on how the merge went and what problems were written in Visual Studio Output. Comments explain what's going on inside. For example, in this case, the choice of the .NET 4 platform occurs.

 @ECHO OFF rem # set .NET version and output folder name set net="v4, C:\Windows\Microsoft.NET\Framework\v4.0.30319" set output=Output rem # process arguments set ILMergeSolution=%1ILMerge\ILMerge.exe rem # determine programm files of x86 for 32 and 64 Platform IF EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES(x86)% IF NOT EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES% rem # if ILMerge.exe not in the $(SolutionDir)ILMerge\ rem # then try to use installed in prorgammFiles IF EXIST %ILMergeSolution% set ILMerge="%ILMergeSolution%" IF NOT EXIST %ILMergeSolution% set ILMerge=%prorgammFiles%\Microsoft\ILMerge\ILMerge.exe set target_path=%2 set target_file=%~nx2 set target_dir=%~dp2 set ConfigurationName=%3 rem # set output path and result file path set outdir=%target_dir%%output% set result=%outdir%\%target_file% rem # print info @echo Start %ConfigurationName% Merging %target_file%. @echo Target: %target_path% @echo target_dir: %target_dir% @echo Config: %ConfigurationName% rem # recreate outdir IF EXIST "%outdir%" rmdir /S /Q "%outdir%" md "%outdir%" rem # run merge cmd @echo Merging: '"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"' "%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll" rem # if succeded IF %ErrorLevel% EQU 0 ( rem # clear real output folder and put there result assembly IF %ConfigurationName%==Release ( del %target_dir%*.* del %target_dir%*.dll del %target_dir%*.pdb del %target_dir%*.xml del %target_dir%*.* copy %result% %target_dir% rmdir /S /Q %outdir% set result=%target_path% @echo Result: %target_file% "-> %target_path%" ) ELSE ( @echo Result: %target_file% "-> %result%" ) set status=succeded set errlvl=0 ) ELSE ( set status=failed set errlvl=1 ) @echo Merge %status% exit %errlvl% 


UPD:
License

From the ILMerge website:
Commercial use permitted:
The language of ILMerge's license has been raised by many questions. In a nutshell: commercial use is permitted, redistribution is not. I am not a lawyer.

That is, it is possible to build in the assembly process, but it is impossible to distribute it with your product.

The question of the licenses of the dll being merged remains open.

Minuses

Similar tools

Materials on the topic

Source: https://habr.com/ru/post/126089/


All Articles