tags:

views:

139

answers:

2

I suppose you cannot simply compile a C++ application with a C++/CLI compiler. I am wondering if it would be difficult. Has anybody tried this, and if so: were there a lot of modifications needed?

+4  A: 

The situation is a bit like compiling C as C++. Most C will compile as C++, but is a long ways from what you'd think of as exemplary C++, so chances are that you'd want to modify it (often quite a bit) before you used it.

Likewise, most C++ will compile as C++/CLI, but chances are that you'd rather not really use it that way.

Jerry Coffin
+2  A: 

This will work well for almost any native C++ code. It will get translated to IL, just like managed code, and get JIT compiled at runtime. You can freely call managed code, thanks to C++ interop. The only unmanaged code construct I know that can't be compiled is the __fastcall keyword. There's an issue with the const keyword, it is imperfectly simulated with attributes, but that's only an issue when you import metadata. Just keep using header files like you do now.

A .NET assembly is capable of storing machine code as well as IL. You can take advantage of this by selectively turning off IL generation for cases where the compiler has trouble or when you suspect generated code isn't optimal. Wrap your code with a #pragma:

#pragma managed(push,off)
void CompiledToMachineCode() {
  // etc..
}
#pragma managed(pop)

You will also need to use this #pragma when you #include headers for .libs that were compiled separately without the /clr compile option.

Hans Passant