-1

I have a C# project and I need a C++ native code to interact with the C# code. To do so, I am using a C++/CLI class wrapper that will call a C++ class.

As far as I understand, if a C++ class has a ref behind it's declaration, it's going to be compiled as managed code. And if it doesn't have it, it will be compiled as native code.

Is this assumption correct or do I need to give the compiler further instructions to assure that my class will be native code?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • _"if a C++ class has a ref...it's going to be compiled as managed code"_ - Not sure about that. A c++/CLI `ref class` can contain both native and CLI types. e.g. A `ref class` that has a `void*` pointer in it. http://blogs.msdn.com/b/abhinaba/archive/2012/11/14/c-cli-and-mixed-mode-programming.aspx –  Dec 10 '15 at 22:54
  • Anyone cares to point out why the -1 votes? – Michel Feinstein Dec 10 '15 at 22:55
  • 2
    Could be angry C++ coders emphasizing the "How the expletive deleted should I know? That's not a C++ question." There's no such thing as `ref` in C++. – user4581301 Dec 10 '15 at 22:57
  • I asked a question with the title and tag c++/cli, so it covers both languages and the ref modifier – Michel Feinstein Dec 10 '15 at 23:09
  • I will read that article, thanks @Micky – Michel Feinstein Dec 10 '15 at 23:10
  • Which "both languages"? CLI is not a language, C++/CLI is... – Deduplicator Dec 10 '15 at 23:42

1 Answers1

4

As far as I understand, if a C++ class has a ref behind it's declaration, it's going to be compiled as managed code.

Correct. ref class types cannot be compiled without /clr.

And if it doesn't have it, it will be compiled as native code.

Incorrect. If /clr is in effect (e.g. not disabled by #pragma unmanaged) then the compiler is only generating MSIL (Microsoft intermediate language, the bytecode for .NET).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • But I am calling c++ libraries from inside it... I assumed that if the code is bring able to interact with native libraries, it was supposed to be native... So I guess MSIL is wrapping these function calls? – Michel Feinstein Dec 11 '15 at 00:10
  • _"If /clr is in effect...then the compiler is only generating MSIL"_ - do you mean **/clr:pure**? https://msdn.microsoft.com/en-us/library/k8d11d4s.aspx –  Dec 11 '15 at 00:13
  • Most naive operations have MSIL versions. Not always as efficient, but still functional. – Ben Voigt Dec 11 '15 at 00:14
  • @Micky: There are two potential sources of native (non-MSIL) machine code, which cause a mixed-mode assembly to be produced: code in your project, and static libraries that get linked in. While I understand there are a handful of cases where the compiler generates native machine code even with `/clr`, the use of `/clr:pure` is more about controlling linking than code generation. – Ben Voigt Dec 11 '15 at 01:03
  • Ben, how does the MSIL code interact with native data then? – Michel Feinstein Dec 13 '15 at 22:41
  • 1
    @mFeinstein: via direct memory operations, pretty much the same as using pointers in C# with `/unsafe` – Ben Voigt Dec 13 '15 at 22:42