How MSBUILD calls CSC.exe? - c#

I want to know about how msbuild.exe execute a C# application.
As i searched in google i came to know csc.exe,PE File, JIT, IL File. And msbuild.exe internally calls csc.exe to compile a C# Application. So I opened csc.exe in reflector. But There is no call csc.exe from msbuild. So how does msbuild call csc.exe?

Although, EXEs are indeed libraries (like DLLs, EXEs are PE files), they are almost always called by creating a separate process with command-line arguments.
Command-line arguments are passed to the "main" function of the EXE, usually as an array of strings. You could find csc's main function with Reflector.
But, you probably want to know what msbuild passes to csc for a particular build. In that case, just use msbuild's verbosity switch:
msbuild MyProject.csproj /target:rebuild /verbosity:diag
See Obtaining Build Logs with MSBuild.

The call to csc.exe is contained in the according .targets1 script. MSBuild.exe itself is oblivious to which language compiler (c#, vb, ...) you are targeting. This is determined by the solution/project and their references.
The actual call to csc.exe should be located in Microsoft.MSBuild.Tasks.dll in your .NET Framework folder.
1 In case of C# it's Microsoft.CSharp.targets

Related

Compile C# console without dll needed

When I compile my C# Console it comes with an dll, if I remove the dll the application doesn't work, and I need it to work with only an .exe and no .dll files.
https://prnt.sc/t1gu4m
Right Click your Project
Select Publish
Deployment Mode: Self-Contained
Check the Produce single file option
Et voila, you got a single exe with the required dependencies
In addition to Maurice Legoland's answer. If you're using dotnet cli, run:
dotnet publish -p:PublishSingleFile=true --no-self-contained # Other arguments.
Only use the --no-self-contained flag if you want your executable to be dependent on .NET i.e, it cannot run without the user having .NET's CoreCLR. Otherwise simply set PublishSingleFile to true. You can also use a manifest/configuration file. See this for more information.

How does C# compiler search for libraries during compilation?

A C# program uses .Net framework libraries or third party libraries by using directive in the C# code.
When compiling the program using csc,
how shall we specify the search path for the used libraries?
does the compiler search for the used libraries in some search order?
I am comparing csc to gcc and javac in terms of specifying search paths for libraries and searching order. For gcc and javac, there are multiple ways to specify search paths,
command line options to them, such as -L to gcc and -classpath to javac;
environment variables, such as LIBRARY_PATH for gcc and CLASSPATH for javac;
default paths, such as current directory, some system-wide directory
Does csc also have multiple ways, and have a search order between them?
Same questions if compiling the program using msbuild which calls csc.
Thanks.
how shall we specify the search path for the used libraries?
Use the /lib command line option.
Syntax
/lib:dir1[,dir2]
dir1
A directory for the compiler to look in if a referenced assembly is not found in the current working directory (the directory from which you are invoking the compiler) or in the common language runtime's system directory.
dir2
One or more additional directories to search in for assembly references. Separate additional directory names with a comma, and without white space between them.
OP
Same questions if compiling the program using msbuild which calls csc
If you use msbuild you are more than likely using a .proj file so it would use the path setup for your environment I would expect
Tell me more
-lib

Referencing Shell32.dll?

I don't have .NET 4.5 to unzip zip files, so I'm using Shell32 like this. But when I reference the dll like this:
csc.exe /r:"C:\Windows\System32\shell32.dll" ...
I get this error:
fatal error CS0009: Metadata file 'c:\Windows\System32\shell32.dll' could not be opened -- 'An attempt was made to load
a program with an incorrect format. '
Update: Without Visual Studio, just csc.exe.
Shell32 is a COM server that you can use in your C# program. You however have to generate an interop assembly first to convert the type library inside shell32.dll (same idea as .NET metadata) to declarations that the CLR can understand. Either by running Tlbimp.exe or, much simpler, by adding a reference to the DLL in the IDE.
As long as you do this from the command line and don't use msbuild to get a .csproj project file compiled then you have to do the same thing that msbuild does, run tlbimp. For shell32.dll this only has to be done once and you can check-in the interop library in source control so you don't have to do it again. Use /r on the interop library.
Using the IDE or MSBuild.exe are of course the wise choices. Also helps you fall in the pit of success, you really want to use the Embed Interop Types feature so you don't need the interop assembly at runtime and don't have to deploy it. Looking at the build commands generated by MSBuild is useful.
Possibly because you are trying to reference it like a .NET assembly.
I would try to include the assembly using the /lib: argument instead as per...
/lib:dir1[,dir2] Compiler option.

How to install Python for .NET on Windows

I downloaded Python for .NET.
Inside the zip is clr.pyd, nPython.exe, Python.Runtime.dll and 2 debug database files.
I put the clr.pyd and Python.Runtime.dll in my python DLLs dir C:\Python27\DLLs thinking this is all that's needed for installation. I then open up the Python GUI and type import clr and I get:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import clr
SystemError: dynamic module not initialized properly
New to python but not .NET and want to use the CPython and not IronPython. What am I missing in this installation? The readme of Python for .NET says there is an installation for Windows package but all I found was the zip file.
The proper way to load CLR in Python is like this:
Make sure no old stuff is left from Python.NET in Python installation folder (e.g. C:\Python27). In my case I had legacy clr.pyd in one of folders. Note that pip for some old versions did not remove all parts of Python.NET.
Append the directory with Python.NET files (clr.pyd and Python.Runtime.dll) to sys.path
Now you can load CLR the most flexible way without even installing to Python directories!
You can compile from source on github:
pip install git+https://github.com/pythonnet/pythonnet
or use Windows wheels/installers from Christoph Gohlke:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet
PyPI package is available for installation from pip:
https://pypi.python.org/pypi/pythonnet
More installation options such docker, WinPython, conda, nuget, choco are listed here:
https://github.com/pythonnet/pythonnet/wiki/Installation
The correct way to install Python for .NET is to copy ALL the files from the .zip into the directory that contains your .py file.
Then if you execute
python yourfilename.py
you should find that your "import clr" statement works. I have tested this using python 2.7 x86 and pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86.zip
I was able to replicate your error by only copying the clr.pyd file into my working directory.
Note that I was unable to make this work in Python 3.3 x86 or Python 3.3 x64
I actually took matters in to my own hands here and created a Python.Net Chocolatey package.
to install, simply run
cinst pythonnet
at the command line. Maybe this will help users having issues getting this to run.
I don't know why yet but the only way I could get it to work is to copy those 3 files into the C:\Python27\ directory directly.
If you are planning to freeze with py2exe or pyinstall be sure to install the dev version. There is something wrong with the 2.0.0 release when packaged with py2exe, pyinstaller and maybe other freezers. But 2.1.0.dev1 works well. So...
pip install --pre pythonnet
And you'll have to add the pythone.runtime.dll to the bundle (see docs for your preferred bundler).
There is also a problem with how py2exe loads the dll when using bundle_files: 1 (single exe).

.net command-line compiler multiple projects

I need to build a batch file that runs on a directory and digs inside for suitable c# projects to compile. (and compile them of course).
I don't know the name of the projects inside the directory.
I can assume all project are in c# and written in VS2008 and above (if that helps).
setlocal
SET CMD= msbuild.exe
for /R %%d in (*.csproj) do %CMD% %%d
endlocal
This will build each project into individual assemblies, with all default build properties for each project. You can specify additional properties, or an MSBuild config file to use.

Categories