7

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.

user20358
  • 14,182
  • 36
  • 114
  • 186
  • You need to specify what `.cs` files you want to contribute to each `dll`. Though you may find that since these are in one project, you have dependencies you did not expect. – Oded Sep 11 '12 at 12:55
  • Thanks Oded. But that would mean I would need to potentially change my script everytime the project contents change, right? Or at the very least manually analyze the entire project for changes. Is there no switch that can automatically link the referenced class from whichever files/folders they are in? I want the new files if any to be referenced every time I build the project in VS without me having to include them every single time.. – user20358 Sep 11 '12 at 14:41
  • That's right. Or use a different build system from the built in one, though you would need to keep it synchronized. Breaking up the project into several would be easiest. – Oded Sep 11 '12 at 14:43
  • what do you mean by different build system? I'm not sure I know what that means... – user20358 Sep 11 '12 at 14:53
  • 1
    `cmake` and `nant` are two examples. Visual Studio comes with MSBuild to compile and build applications. It doesn't _have_ to be used. – Oded Sep 11 '12 at 14:54
  • Also, do you have any examples that show how to reference multiple .cs files that I would want to contribute to each dll? – user20358 Sep 11 '12 at 15:35
  • http://msdn.microsoft.com/en-us/library/78f4aasd.aspx – Oded Sep 11 '12 at 15:36

4 Answers4

11

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • Probably the _easiest_ way to do it (with Visual Studio), but certainly not the only. – Oded Sep 11 '12 at 12:56
  • Indeed, although regardless it would be the *correct* way to do it. – PhonicUK Sep 11 '12 at 13:19
  • Thanks..that was quick :) ..so when adding as a link, would it matter in which project those files physically are? If File1.CSProj has all the files and File2.CSProj has links to the required files then it would still be ok right? Is this like a very common thing to do? .. adding as link I mean. I wouldn't want to bend the rules of good coding practices to get this done. Thanks again. – user20358 Sep 11 '12 at 15:00
  • Nope, doesn't matter which one they actually exist in. I'd probably be inclined to add them to the 'main' project in this scenario. Linking like this is fairly common so it's not bending too many rules. – PhonicUK Sep 11 '12 at 15:47
  • @PhonicUK - sorry I'm a little late to the party, but is this as simple as right clicking on the solution and going Add -> New Project? – user2635088 Nov 20 '15 at 09:03
4

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.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • If you set these up as part of the project configuration, this will also get you the benefit of any dependency or compilation errors while working in the IDE. – StingyJack Sep 11 '12 at 15:06
2

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.

akton
  • 14,148
  • 3
  • 43
  • 47
  • Thanks Akton. The problem 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 that is required for both File1.dll and File2.dll. I dont want this. I want only File1.dll and File2.dll which is why I am keeping them both in one project – user20358 Sep 11 '12 at 14:45
1

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.

Sepster
  • 4,800
  • 20
  • 38
  • Updated my question... Also, how do you go about referencing the same CS file from more than one project? – user20358 Sep 11 '12 at 14:54
  • If you have a solution with multiple projects, right click one of the projects -> Add... -> Existing Item ... -> browse to the pre-existing .CS file you want to add. Confirmed this works in VS2010 and pretty sure it works in previous versions. – Sepster Sep 13 '12 at 01:39