Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

You need multiple projects to do this. There's nothing stopping you from referencing the same CS file from more than one project.

Also, if it makes sense to do so, both the projects can exist under the one solution, if you need the logical grouping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top