Compiling C Project with /clr in VS2005

Hello,

I have been using VS2003 to comple C projects with /clr and generate CIL binary, which I use to generate code for an embedded processor.

In VS2005, compiling C projects to CIL is no longer supported. It seems the only possible way is setting the compiler options to compile everything as C++. Now, my problem is whenever I compile even the simplest C files (e.g. int main(){retrun 1;} ) in this way, after all optimizaitons, the output CIL contains a huge amount of seemingly unnecessary code. There are many new funcitons, variables, namespaces, etc. in the output CIL. I have tried every possible option or optimizations. Specifically, I was hoping that by setting "Configuration Properties/Linker/Optimization/References" to "Eliminate Unreferenced Data (/OPT:REF)" would solve this problem, but it has almost no effect.

Is there anyway to prevent the compiler from generating these extra code

Or alternatively, is there anyway to compile simple C files to simple CIL

Thank you.



Answer this question

Compiling C Project with /clr in VS2005

  • Melissa_Janet

    With Visual C++ 2005 you cannot compile C code with any of the /clr variants. We dropped the support that previously existed because a) as far we could tell no one was using it b) it was costing us resources to maintain it and c) why bother - one of the major reasons for compiling with /clr is to use the BCL and other .NET components which you can never access from C.

    Also with Visual C++ 2005 it makes very little sense to compile regular C++ code with /clr:safe - the limitations that /clr:safe imposes are so strict (no pointers for example) that it is next to impossible to write a meaningful C++ program that will compile with /clr:safe.

    Note: if you really do need to compile some C code for the .NET platform then you can always compile it as C++.



  • gpja

    While I don't completely understand the issues, have you tried /clr:pure

    Thanks,

    Ben



  • Scraniel

    Thanks Mike!

    I tested this option, but it limits the C language severly to a point that it is practically not usable! For example, it does not allow initialized global variables, or any unmanaged data structure (e.g. struct).

    With VS2003, I can compile any legacy C code into CIL, but with /clr:safe practically no C code can be compiled.

    Is it possible to disable checking for safe code in the compiler, while forcing it to generate code with /clr:safe

    thank you.


  • TaffyDownUnder

    Interesting approach. That extra code has to do with the C runtime library. If you compile with /clr:safe switch (Safe MSIL Common Language Runtime Support (/clr:safe) in Project properties, Common Language Runtime Support) you should get what you need but this switch does not work with the C runtime library. Given your situation I don't know if this will be an option for you or not.


  • Compiling C Project with /clr in VS2005