What I need to do:
1. Create an application in that allows a user to define a function
Now, I need to compile it to .dll . After compilation, the .dll should be loaded into another C# application which will call that function.
Firstly, you don't need to compile C to use pointers; C# has those too... and the odds are, if you can compile and load an unsafe DLL file, you can use the unsafe keyword to leverage them. Your solution will likely be much cleaner if you stick to using one language.
As for the error message, it's not clear whether you're using Turbo C++ (which is no more a C compiler than a rock is a hammer) or Tiny C, but either way __declspec(dllexport) is a Microsoft extension. Perhaps you could try using Microsofts compiler (which isn't a very nice compiler, either), if you wish to use a Microsoft extension... Particularly if you're going to attempt to use such a DLL from another Microsoft product; They're going to expect a compatible ABI!
Related
Why does ILSpy generate a source even though it was developed with C ++ Windows Application?
https://imgur.com/a/nAAnV
developing it with C ++↑
enter image description here
↑In order not to display like Noah CSGO?
Most probably you've missed the fact, that you can write in C++ and compile it into MSIL assembly and run that as .net managed code.
Go and see - in Microsoft Visual Studio for C++ there's a compiler switch /clr:pure or other flags (msdn: https://msdn.microsoft.com/library/k8d11d4s.aspx).
When you use this mode, you will be able to use special markers to define that specific classes should be compiled as CLR managed classes. If you take care and write everything properly, /clr:pure will emit a normal 100%-managed assembly. I'm tempted to say "everything as if in C#" but that's not true, generated code will be different, but nevertheless, everything will work, metadata will be there, etc. ILSpy will decompile that just like any assembly generated by C#, VB.Net or F#, but probably the code/names/etc will be a bit more mangled.
Sidenote: If you invest in some real decompiler like Reflector, they can often take a C# assembly and decompile it as-if-written-in-managed-C++ and vice versa..
Furthermore, if you don't care about "pure" msil, you can even mix native and managed code. Half of the code in native C++, other half in managed, just take some care regarding pointers and references and cleanups, and compiler will happily emit for you a mixed assembly - here I have no idea if ILSpy will manage to show you anything. Probably it will show you the managed part and show the native part as being there but as not-disassemble-able.
Finally, it's also https://learn.microsoft.com/en-us/dotnet/framework/app-domains/how-to-build-a-multifile-assembly which ILSpy probably also supports.
I need to use a program made in C for a C# project , and for this I need to generate a DLL in Visual Studio 2013, anyone have any tutorial or know how to do it?
I want make a DLL for a structs and read/write functions in C, cheers guys.
You have to use p/invokes. I think this is very often discussed here. Take a look at: Access a method from a DLL from C# program
DLL is probably overkill for what you are doing. You can just write a C procedure and P/Invoke it using CDecl. The problem is that you need to know a whole lot for this. If you are simply trying to iterate over an array fast for a performance critical section, you are better off using structs in C#, slapping unsafe on your method, and then using pointers and addresses to do what you want to do. Code that looks a whole lot like C/C++ is perfectly legal in C#.
See a reference here: MSDN : Unsafe Pointers
Also fished out a somewhat dated reference showing how to P/Invoke the visual c Runtime printf function as an example. Keep in mind that things get really hard when you need to give a pointer to a function and when you need to read offsets etc. to pass around structs. You'll need to pin anything you pass into the method to stop it from being moved by the garbage collector, which will also have performance implications.
MSDN: CDecl P/Invoke example
I have a C# app that gets ahead of time compiled to the native iOS code using monotouch (Xamarin)
Some of the libraries I link in use generics. However, it turns out that this method of compiling causes significant code bloat because it uses the C++ style of template code generation generating functions for List<int>, List<string> etc.
What I want is the Java style of generics where generics are used for compile time checking but at runtime the code only contains functions for List and not for each of the templated types.
Note: This is not an issue with using C# in the .Net CLR, as explained here. The issue arises because code is compiled AOT to the native binary instead of intermediate language. Moreover runtime type checking for generic methods is fairly useless since the binary is native.
Question: How do I disable generics, i.e. replace all occurrences of List<T> with List, during compilation? Is this even possible?
This is not possible.
On Java it's possible because they don't have value types, only classes (you can emulate this behavior by not using value types yourself, only use List<object> (or an object subclass), in which case the AOT compiler will only generate one instantiation of List).
You're also not entirely correct saying that it's not an issue with the .NET CLR; the difference between the .NET CLR and Xamarin's AOT compiler is that the AOT compiler can't wait until execution time to determine if a particular instantiation is needed or not (because iOS doesn't allow executable code to generated on the device), it needs to make sure every possible instantiation is available. If your app on the .NET CLR happened to need every possible generic instantiation at runtime, then you'd have a similar problem (only it would show up as runtime memory usage, not executable size, and on a desktop that's usually not a problem anyway).
The supported way of solving your problem is to enable the managed linker for all assemblies (in the project's iOS Build options, set "Linker behavior" to "All assemblies"). This will remove all the managed code from your app you're not using, which will in most cases significantly reduce the app size.
You can find more information about the managed linker here: http://developer.xamarin.com/guides/ios/advanced_topics/linker/
If your app is still too big, please file a bug (http://bugzilla.xamarin.com) attaching your project, and we'll have a look and see if we can improve the AOT compiler somehow (for instance we already optimize value types with the same size, so List<int> and List<uint> generate only one instantiation).
Is there a way to compile a C++ file from C#, assuming the compiler (G++ or VC++) is installed on the computer?
You should be able to take the same compiler call that you would make in the command line and recreate it using the Process class. The MSDN page has examples of how to use Process.
Yes you would just invoke the c++ compiler with the proper arguments. However there are tools custom built to do just this Make and msbuild for example so I would doubt that a new tool would better serve you.
For business reasons, I want to create a C# application that would take a C++ file / snippet as input, compile it (probably invoking a C++ compiler under the hood) and output compilation results.
Do you know how this could be done?
Thanks in advance.
Using CL.exe
Look in to the Process class.
It provides all of the functionality required to start an external application, including a compiler.
Now, depending on the compiler you choose, you will need to specify the start arguments of the process carefully in order to compile in a predictable way.
Most compilers support command-line parameters.
You just need to build the right command and execute it through the shell like advised here.
che
If you want to not just build a single file, but a whole .vcproj file - check the command line parameters for devenv.exe. If I remember correct it is:
devenv.exe /build my.vcproj
You can shell out to any number of command-line C++ compilers (like gcc) using Process.Start.
You could do this by calling any C++ compiler on the command line. I'm sure the compilation results can be redirected so that you can grab it after the compile finished.
If you are trying to achieve something like SnippetCompiler for C++, you might want to look at one of the C/C++ scripting languages. I have always liked CInt.
Sure its possible, it is actually common practice nmake and other make-like utilities call the compiler all the time.
OTOH if you are thinking of deploying this solution to a customer you may be in for a bit of rough ride cause of all the details like settings, location where files are, how to react on the result, license, which c++ compiler to deploy etc.
Another option which is much simpler (IMHO) is instead of C++ to invoke the C# compiler directly from your C# program for compiling a C# snippet. Files can then stay in memory and you don't need to save any files before you compile. There are numerous examples on the web that show how to do that. Another advantage with this solution is that you already have the C# compiler there so there is no need to install a C++ compiler as well.