6 ms·
A bit of a tangent but is it possible yet to compile C# to native code easily (like calling a normal compiler on the command-line)? Last time I checked I needed
by dataflow 6y ago
A bit of a tangent but is it possible yet to compile C# to native code easily (like calling a normal compiler on the command-line)? Last time I checked I needed to jump through a bunch of hoops through Visual Studio, and create some XML or other nonsense. I don't get why they've made it so hard compared to just compiling managed code.
- DownGoat 6y agoIt is super easy with dotnet core. The new CLI is pretty simple, you use it as a package manager and compiler. Visual Studio also uses the CLI to run and debug dotnet core projects. https://docs.microsoft.com/en-us/dotnet/core/tools/ https://docs.microsoft.com/en-us/dotnet/core/tools/
- MikeTheGreat 6y agoIs it possible to create a single, stand-alone, self-contained .exe? (I'm fine with only building for a single platform - Mac/Win/Linux) Needing to run my programs (which show up as .dll's) using dotnet just feels weird (and it doesn't match my intuition for 'how programs are run' in Windows cmd/etc). I'd be fine with an .exe that's not self contained but at least I run it like a 'normal' exe. :)
- oblio 6y ago> Is it possible to create a single, stand-alone, self-contained .exe? Yes.
- smcl 6y agoYes that is possible, you can produce an .exe which contains your application's assemblies but relies on the appropriate .NET runtime being present on the user's computer. You can also build a .exe which has the .NET runtime bundled, but it can be quite big. I made a simple program that wrote "Hello World" to the console [1] and it was between 46-78MB: https://blog.mclemon.org/no-this-is-what-peak-hello-world-looks-like https://blog.mclemon.org/no-this-is-what-peak-hello-world-lo...
- pjc50 6y agoYeah, it's not really a "real" native binary. You can get it down to 8kb by jettisoning all comforts: https://medium.com/@MStrehovsky/building-a-self-contained-game-in-c-under-8-kilobytes-74c3cf60ea04 https://medium.com/@MStrehovsky/building-a-self-contained-ga... but that's not exactly practical.
- smcl 6y agoWow! Yeah indeed there's a few impractical steps required to go all the way to 8kb, but trimming down to a couple of MB is definitely a huge improvement. I somehow never looked into CoreRT before, I'm gonna need to do so. Thanks for sharing this!
- pjc50 6y agoYou have to set up "dotnet publish" for the project that makes the executable. It then makes a stub loader "your program.exe" which looks and behaves normally. You can then make it "self contained", which bundles the assemblies for you and transparently unpacks them. You can have different publish profiles for the different targets.
- benaadams 6y ago.NET 5.0 can now create true single file that doesn't need any unpacking on first run (except on Windows where the Jit and GC dlls will live or be unpacked separately; but that should be resolved in .NET 6.0)
- uNople 6y agoYes, you can use ILMerge to merge all other DLLS into the main exe/dll: https://stackoverflow.com/questions/8077570/how-to-merge-multiple-assemblies-into-one https://stackoverflow.com/questions/8077570/how-to-merge-mul...
- vborovikov 6y agoIt's possible but along with an .exe you'll get a couple of .dlls. Exe size will be 60+ Mb.
- dataflow 6y agoBut I want it to produce a single EXE and that's it... like the C# compiler has always been capable of doing. Sorry I neglected to mention that part (kind of assumed it was a given). Also, the whole thing is still a stateful operation revolving a .csproj file, which (again) is XML I have to create (sure, it's just one command) and manage/deal with every time. Even to compile a tiny C# file from my editor now I need to make a project for it. With a normal C# or C++ compiler I can just invoke csc.exe or cl.exe and produce the executable in one command from my editor... no intermediate state or project file management required. That's kind of what I was getting at with it being "simple". I didn't just mean the syntax, I also meant the structure of the solution.
- nightski 6y agoYou can still just invoke csc.exe, no project file required - I'm not sure what you are getting at? But it's probably going to be easier to just dotnet new console and get the project file for free. They tried migrating away from csproj but there is just too much ecosystem around it. The XML sounds bad, but in reality they added file globbing and other things to make it so you rarely even have to change it. That and to be honest it just works and is quite readable even though it's the "evil" XML.
- dataflow 6y agocsc.exe doesn't compile to native?
- nightski 6y agoNo, it's always compiled to MSIL. You've always had to use a separate tool (such as NGen or dotnet publish) to do AOT compilation. But there are tradeoffs and AOT doesn't always mean faster performance. But again, if you use the dotnet CLI you can just dotnet publish to create a AOT version of your exe. Just remember it still has the runtime and GC. You are just reducing the work of the JIT. Also AOT does not guarantee you will not JIT and still contains the MSIL. It's just a performance optimization where pre-compiled code will be used when possible.
- scott00 6y agoI think the short answer to your question is "no". For single-file-no-project simplicity your best bet is dotnet-script: https://github.com/filipw/dotnet-script https://github.com/filipw/dotnet-script For native code your best bet is NativeAOT: https://github.com/dotnet/runtimelab/tree/feature/NativeAOT https://github.com/dotnet/runtimelab/tree/feature/NativeAOT I'm not aware of anything that combines the two. As to why: it's not so much that they've made it hard as they haven't made it easy. The reason they haven't made it easy is that it's a fringe use case. The main benefits of AOT are faster startup time, smaller storage requirements, and compliance with the iOS interpreted language ban. Most people who worry about those things don't mind having a project file. They _have_ made some changes that may partly address your concerns. Visual Studio is no longer required to build C#; you can do it purely from the command line. There is also a new project file format. It's still XML, but much simpler. The HelloWorld example[0] from NativeAOT is a good example of how simple it can get. And the command line tools include an easy way of creating basic project files[1], so you don't have to memorize what little boilerplate remains. [0] https://github.com/dotnet/runtimelab/tree/feature/NativeAOT/samples/HelloWorld https://github.com/dotnet/runtimelab/tree/feature/NativeAOT/... [1] https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-ne...
- dataflow 6y agoGotcha, thanks!
- linknoid 6y agoThe C# compiler has always been included with the .NET Framework, it has never required Visual Studio: https://stackoverflow.com/questions/861384/is-it-possible-to-install-a-c-sharp-compiler-without-visual-studio https://stackoverflow.com/questions/861384/is-it-possible-to...
- jussij 6y agoOn Windows using .Net Core makes this fairly trivial. You basically install the .Net Core SDK and then run these commands to create your first executable: dotnet new dotnet build dotnet run And I suspect it is the same on the other platforms Here is an simple console example with more detail: https://www.zeusedit.com/phpBB3/viewtopic.php?f=5&t=8135 https://www.zeusedit.com/phpBB3/viewtopic.php?f=5&t=8135