Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C# manually while targetting .NET 3.5 with Roslyn(csc.exe)

Tags:

c#

.net

csc

I'm using csc/csc2.exe to manually compile an application. I need to reference .NET 3.5 dlls, however it seems that the compiler automatically adds .NET 4.0 dlls as well (which is causing a conflict).

I'm manually referencing the desired version of mscorlib, and other system dlls. Compilation from within Visual Studio succeeds, however compilation of the same response file manually from command prompt fails.

/nostdlib+
/platform:AnyCPU
/errorendlocation
/highentropyva-
/reference:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll"
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll"
/reference:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll"
/debug+
/debug:full
/out:obj\Debug\Target.exe
/ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset"
/target:exe
/utf8output
Program.cs Properties\AssemblyInfo.cs

I'm getting the following errors for every referenced assembly:

error CS1703: Multiple assemblies with equivalent identity have been imported: 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.dll' and 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll'. Remove one of the duplicate references.

What causes the compiler to include .NET 4.0 dlls, and what can I do to prevent it from including them?

UPDATE: I cannot use MSBUILD, because I already have a response file (not a project file) from an application. The application used an outdated version of a compiler, and I'm trying to replace their compiler with a newer one that supports C#6. I've successfully done this task with Mono MCS compiler, but cannot get the same to work with Roslyn. I cannot use the MCS compiler, because it doesn't yet support all of C#6 features.

The only way to use MSBUILD would be to parse the response file back into a C# project file. This seems like over-engineering to me.

like image 543
Ilya Suzdalnitski Avatar asked Mar 23 '15 16:03

Ilya Suzdalnitski


Video Answer


1 Answers

Add /noconfig option to command line and it should work as expected.

The /noconfig option tells the compiler not to compile with the csc.rsp file, which is located in and loaded from the same directory as the csc.exe file.

The csc.rsp file references all the assemblies shipped with the .NET Framework. The actual references that the Visual Studio .NET development environment includes depend on the project type.

Source

Usual contents of csc.rps (v4.0.30319) :

/r:Accessibility.dll
/r:Microsoft.CSharp.dll
/r:System.Configuration.dll
/r:System.Configuration.Install.dll
/r:System.Core.dll
/r:System.Data.dll
/r:System.Data.DataSetExtensions.dll
/r:System.Data.Linq.dll
/r:System.Data.OracleClient.dll
/r:System.Deployment.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceModel.dll
/r:System.ServiceModel.Web.dll
/r:System.ServiceProcess.dll
/r:System.Transactions.dll
/r:System.Web.dll
/r:System.Web.Extensions.Design.dll
/r:System.Web.Extensions.dll
/r:System.Web.Mobile.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.Workflow.Activities.dll
/r:System.Workflow.ComponentModel.dll
/r:System.Workflow.Runtime.dll
/r:System.Xml.dll
/r:System.Xml.Linq.dll
like image 149
Pavel Krymets Avatar answered Sep 30 '22 07:09

Pavel Krymets