I am trying to create a pure MSIL assembly from a C++/CLI project using /clr:pure and /clrimagetype:pure flags, however, the output assembly specifically targets x86.
Am I missing anything that might be preventing my project to be compiled as MSIL only?
You can create an AnyCPU dll with C++/CLI, but in the simplest case, you will not be able to use MFC, ATL or the CRT. However, if you just want to write pure managed .NET code in C++/CLI, including managed pointers (which /clr:safe
does not allow), and get the more elaborate code optimization of the C++/CLI compiler, read on:
/clr:pure
. This is on the Configuration Properties
page in Visual Studio 2010. Omit Default Library Name
to Yes /Zl
Incremental Linking
and Link Library Dependencies
Not Set
and the CLR Image Type to Force Pure IL Image /CLRIMAGETYPE:PURE
, but some of these aparently settings aren't honored, since the 32BIT+ flag is still set by the linker in the PE32 header.corflags
step to your build. The best way to do this is to exit Visual Studio and edit your vcxproj file with a text editor. At the bottom of the file, add:<!-- at the bottom of the file... --> <Target Name="AfterBuild"> <Exec Command="corflags $(TargetPath) /32BIT-" /> </Target> </Project>This runs the
corflags
tool to turn off the 32BIT flag in your DLL. Make sure that the corflags.exe
tool is available in your path.#pragma warning(disable:4483) void __clrcall __identifier(".cctor")() { }
That's it, you can now build the AnyCPU dll; it is pure MSIL
by virtue of the 'pure' settings, and it will load either as x64 or x86 thanks to the corflags
adjustment. It is up to you to avoid using any incompatible features, such as Interop, at runtime. However--and this is the difference versus just trivially using /clr:safe
mode (which also produces an AnyCPU library)--you can use unsafe managed pointers to access managed value types.
static int my_arr[] = { 1, 2, 3 };
The linker issues a warning to this effect, warning LNK4210: .CRTMA section exists; there may be unhandled static initializers or terminators
. You can, however, declare them, initialize them yourself, and use them--that is, take their addresses--and read/write them from managed code (if you want to declare such an array as const
, then you'll have to provide empty braces { }
for an initializer and cast a pointer to volatile
to initialize it.):
static int my_arr[3];
Ironically, one way to initialize these native static resources or tables is to copy them, during the module constructor or class static constructor, from a managed variable or resource.
Why bother with native statics, you ask? Because they can be quickly accessed without pinning. One nice thing C++/CLI still does for you here is to silently create a managed value-type (struct) to overlay each of your native statics, so that the IL code can get at them directly with IL pointers, thus keeping the assembly /pure.
[edit: corrected mis-statement regarding "native" pointers in an AnyCPU assembly] [edit: clarify: 'unsafe' C# code in pure assembly uses managed pointers via IL instructions such as ldloca, etc.]
If you are using Visual Studio, you should be able to set the Target Machine project property under the Linker -> Advanced settings.
If what you are looking for is to compile your C++/CLI project as "Any CPU", then you need to compile in /clr:safe and /clrimagetype:safe. See the following link for more information:
http://bytes.com/topic/net/answers/660475-any-cpu-build-c-cli
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With