Question

I'm very new to C# and have more knowledge in the field of C++ and Java. When compiling a C++ or Java project, I am used to that compilation is performed for each source file on its own. In C++, and additional step to link all the object files into one library/exe/dll is taken afterwards.

I see several advantages in this method, but I can not find a way to realize it in C# using the mono dmcs compiler. Assume I have two files each with a single class.

OptionsSet.cs

interface OptionsSet {

    // 

}

DefaultOptionsSet.cs

class DefaultOptionsSet : OptionsSet {

    // 

}

I can successfully compile this into a library by invoking

dmcs mylib/OptionsSet.cs mylib/DefaultOptionsSet.cs -target:library -out:mylib.dll

But I do not want to recompile all source files when I changed a single file! Doing the following:

dmcs mylib/DefaultOptionsSet.cs -target:library -out:mylib/DefaultOptionsSet.dll

yields

mylib\DefaultOptionsSet.cs(15,27): error CS0246: The type or namespace name `OptionsSet' could not be found. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings

I know I can add assembly references with the -r option, but what if the assembly reference was not yet compiled?

In Java, my Makefile would look like this:

SOURCE_DIRS = mylib
SOURCES = $(foreach dir,$(SOURCE_DIRS),$(wildcard $(dir)/*.java))
CLASSES = $(SOURCES:java=class)

compile: $(CLASSES)

mylib/%.class: mylib/%.java
    javac $< -classpath .

But I can not directly translate this to build my C# library.

Was it helpful?

Solution

C# does not support to do what you want to do, there is only a single step between source files (.cs) and the final assemblies (.dll and .exe).

Have in mind that a C# compiler is in general a lot faster than a C/C++ compiler for the same amount of source code (among other things this is because the C# doesn't have to read megabytes of header files for each source file), so generally compilation speed is not an issue.

If you really want what you can do is to split your work into several assemblies (.dll files), compile each separately, and then just reference those assemblies when building your main executable. However unless your project is really big, you'll spend more time implementing this split than the time you'd save from building less when done.

I'd recommend not to worry about long compile-times unless you actually have a problem with long compile-times.

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