Visual Studio CLR project
so I wanted to try my first CLR project in Visual C++. So I created console project, but since every tutorial I found about CLR programming was using C#, or windows forms, I just tried writ开发者_StackOverflow中文版ing standart c++ Hello Word app using iostream (I think code isnt needed in this case) but I though it will give me some compile error, since iostream uses precompiled functions, and CLR app compiles into MSIL. I assumed that CLR programming using C++ means just using C++ syntax, but different functions for I/O and so. So, basicly, what I want to ask is, can any native console C++ app be compiled into MSIL and run by .NET framework?
You are going to be frustrated if you try to learn about managed programming by taking a classic C++ app featuring cout
and lots of STL, and build it /clr
. You are supposed to be able to compile any valid C++ app /clr but that doesn't mean that the result will be worthwhile. A quick tip if you insist on going that way - turn off precompiled headers.
If you have a C# tutorial for some sort of console application, you can translate it into C++/CLI as you go. The MSDN documentation can help you since it usually has examples in VB, C#, and C++/CLI, and you will pick up the substitution pattern pretty quickly. So if your tutorial in C# features Console.WriteLine
, your C++/CLI version will have Console::WriteLine
. If the C# code has Employee e = new Employee()
, your C++/CLI code will have Employee^ e = gcnew Employee()
and so on. This approach will bring you to learning C++/CLI more quickly than starting with "classic C++" and trying to CLR-ify it later.
That said, C++/CLI is really not for writing console and winforms apps. It's for interop. So move on pretty quickly to writing libraries that call into large existing C++ libraries (include the header, link to the lib) and then some UI (windows, web, wcf, whatever) in VB or C# that use your new interop library for simpler access to the functionality of the old C++ code. If you have no intention of ever doing that, why are you learning C++/CLI?
Also there are C++/CLI books - I have several in my office.
can any native console C++ app be compiled into MSIL and run by .NET?
Most code can see http://msdn.microsoft.com/en-us/library/aa712815.aspx for the exceptions.
Your application will still use native code thought. There's also /clr:pure if you want to ensure only CLR code is used.
精彩评论