How does csc know where to look for standard library DLLs?

Viewed 170

When you compile a C# program, using the command line compiler csc.exe, it automatically looks at various .Net standard library DLLs, both the ones always implicitly referenced, and the ones you explicitly reference with -r, such as -r:System.Numerics.dll.

How does it know where to find them? For example, on my Windows machine are currently 19 different versions of System.Numerics.dll (possibly with some duplicates); how does csc know which directory to look in?

If they handle it differently, I am asking about the command line compiler, not about the Visual Studio IDE.

Also, to the extent that they are different, I'm asking about what happens at compile time (where the compiler needs to read the DLLs for e.g. type checking) and not at run time (where the DLLs are loaded in order to actually call the code).

1 Answers

Okay, I ended up writing a script to look through all the environment variables that specify directories, to see which ones contain relevant files: https://github.com/russellw/env-dirs

It seems that the two environment variables specifying directories containing the DLL I used as an example, System.Numerics.dll, are PATH and LIBPATH; presumably the latter is the relevant one here.

On my machine, the relevant entry for the latter is C:\Windows\Microsoft.NET\Framework64\v4.0.30319 which does indeed contain what looks like a quite comprehensive set of relevant DLLs.

Update: while the environment variables do have these properties, apparently the C# compiler does not actually base its search on an environment variable at all, but on something else entirely, as per discussion on Does the C# compiler put library options on the command line before or after default ones?

Related