Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile one project into multiple dlls in .NET

I am trying to compile one .Net c# project into multiple dlls. But here is the thing.

This is my project structure.

Bin 
Dir1 
Dir2 
File1.cs 
File2.cs 
myproject.csproj

I want to compile this project into File1.dll and File2.dll. Both File1.dll and File2.dll use code in various .cs files that are in Dir1 and Dir2. Some of these classes in the .cs files present in the sub directories are required by File1.cs and the others by File2.cs. There will be some that are used by both File1.cs and File2.cs.

I used the following:

csc /t:library /out:File1.dll /recurse:*.cs
csc /t:library /out:File2.dll /recurse:*.cs

But the resulting dlls were exact copies of each other with just different file names. Is there a way for me to compile them so that when I inspect each dll in the object browser, it has only those classes from the .cs files in the Dir1 and Dir2 that are referenced within File1.cs for File1.dll and similarly for File2.dll?

I want to do this preferably as a part of a post build step in Visual studio.

Thanks for your time...

EDIT

Thanks for all your responses. There were many suggestions for splitting up the solution into many projects. While that is one way to do it, I am not sure if I want the any dependencies created for the two dlls ie File1.dll and File2.dll. Let me explain..

The problem I see with creating multiple projects is that there will be some code that both File1.cs and File2.cs need. I dont want them to be repeated in both projects. Keeping the common code in a third project would mean a third dll will be created that is required for both File1.dll and File2.dll. I dont want this. I want only File1.dll and File2.dll created which is why I am keeping them both in one project. Any suggestions on how to make this happen?

Thanks again for your replies.

cheers.

like image 313
user20358 Avatar asked Sep 11 '12 12:09

user20358


People also ask

How do I compile DLL in Visual Studio?

On the menu bar, choose File > New > Project to open the New Project dialog box. In the left pane of the New Project dialog box, select Installed > Visual C++ > Windows Desktop. In the center pane, select Dynamic-Link Library (DLL). Enter MathLibrary in the Name box to specify a name for the project.

Can you combine dll files?

You can't really “merge” dll files.

Can you make a DLL with C#?

This tutorial shows, how to build a simple Class Library . DLL in the C# Programming Language. The Class Library . DLL contains program code, data, and resources that can be can used by other programs and are easily implemented into other Visual Studio projects.

How do I create a .NET DLL?

To create a DLL File, click on New project, then select Class Library. Enter your code into the class file that was automatically created for you and then click Build Solution from the Debug menu. P.S. DLL files cannot be run just like normal applciation (exe) files.


3 Answers

You need to split your project up into multiple projects within the same solution if you want separate libraries for each component.

You can have the same file included in multiple projects without creating duplicated copies (Add as link) to avoid issues with duplicated code.

like image 152
PhonicUK Avatar answered Oct 04 '22 00:10

PhonicUK


You could use conditional compilation symbols (#if DLL1, #if DLL2).

Then, in your build step, call msbuild with the /p:DefineConstants option to set those symbols.

MSBUILD /p:DefineConstants=DLL1 ...

basically compiling your project twice with different conditional compilation symbols set.

like image 20
sloth Avatar answered Oct 03 '22 22:10

sloth


Create one project for each DLL you want to produce and add the file specific to each. For example myproject1.csproj containing File1.cs (and other files as needed) and myproject2.csproj containing File2.cs. After that, create a third project containing any common files and reference that shared assembly from the first two.

Alternatively, if you want to include and exclude different pieces of code consider #if statements checking for preprocessor constants.

like image 41
akton Avatar answered Oct 03 '22 23:10

akton