visual c++ - Creating a pure MSIL assembly from a C++/CLI project? -
visual c++ - Creating a pure MSIL assembly from a C++/CLI project? -
i trying create pure msil assembly c++/cli project using /clr:pure , /clrimagetype:pure flags, however, output assembly targets x86.
am missing might preventing project compiled msil only?
you can create anycpu dll c++/cli, in simplest case, not able utilize mfc, atl or crt. however, if want write pure managed .net code in c++/cli, including managed pointers (which /clr:safe
not allow), , more elaborate code optimization of c++/cli compiler, read on:
/clr:pure
. on configuration properties
page in visual studio 2010. on c/c++ panel, set omit default library name
yes /zl
for linker, disable incremental linking
, link library dependencies
on linker's "advanced" page, set target machine not set
, clr image type force pure il image /clrimagetype:pure
, of these aparently settings aren't honored, since 32bit+ flag still set linker in pe32 header. therefore, add together corflags
step build. best way exit visual studio , edit vcxproj file text editor. @ bottom of file, add: <!-- @ bottom of file... --> <target name="afterbuild"> <exec command="corflags $(targetpath) /32bit-" /> </target> </project> runs corflags
tool turn off 32bit flag in dll. create sure corflags.exe
tool available in path. finally, add together stub entry c++/cli source file. module static constructor. worked me place next outside of namespace: #pragma warning(disable:4483) void __clrcall __identifier(".cctor")() { } that's it, can build anycpu dll; pure msil
virtue of 'pure' settings, , load either x64 or x86 corflags
adjustment. avoid using incompatible features, such interop, @ runtime. however--and difference versus trivially using /clr:safe
mode (which produces anycpu library)--you can utilize unsafe managed pointers access managed value types. [edit:] elaborate on ben voight's comment, 1 thing won't work in type of c++/cli anycpu dll utilize of c/c++ initialization syntax initialize unmanaged (i.e. native), static primitives, structs, (or arrays):
static int my_arr[] = { 1, 2, 3 };
the linker issues warning effect, warning lnk4210: .crtma section exists; there may unhandled static initializers or terminators
. can, however, declare them, initialize them yourself, , utilize them--that is, take addresses--and read/write them managed code (if want declare such array const
, you'll have provide empty braces { }
initializer , cast pointer volatile
initialize it.):
static int my_arr[3];
ironically, 1 way initialize these native static resources or tables re-create them, during module constructor or class static constructor, managed variable or resource.
why bother native statics, ask? because can accessed without pinning. 1 nice thing c++/cli still here silently create managed value-type (struct) overlay each of native statics, il code can @ them straight il pointers, keeping assembly /pure.
[edit: corrected mis-statement regarding "native" pointers in anycpu assembly] [edit: clarify: 'unsafe' c# code in pure assembly uses managed pointers via il instructions such ldloca, etc.]
visual-c++ c++-cli clr
Comments
Post a Comment