Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

Compiling Managed C++ Code In Visual Studio

Icon Leave Comment
Quick note: I did these steps with VS 2005, so I cannot confirm or deny their applicability with any other version. However, it "should" be basically the same.


There was a thread in the C/C++ forum yesterday regarding compilation errors with some Managed C++ code. I consider Managed C++ to be that alien fetus thing begging to be killed in that one Alien movie, but since there is some of this crap code floating around the world you may need to know how to set up your environment to accommodate it.

Managed C++'s definition:

Quote

Managed Extensions for C++ or just Managed C++ is a now deprecated Microsoft's set of deviations from C++, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the .NET Framework. These extensions allowed C++ code to be targeted to the Common Language Runtime (CLR) in the form of managed code as well as continue to interoperate with native code. Managed C++ was not a complete standalone, or fully-fledged programming language.

"Managed" refers to managed code that it is run in, or managed by, the .NET virtual machine that functions as a sandbox for enhanced security in the form of more runtime checks, such as buffer overrun checks. Additionally, applications written in Managed C++ compile to CIL — Common Intermediate Language — and not directly to native CPU instructions like regular C++ applications do.


It's bridge between C++ and C# (before C# became one of the "cool kids") which is now abandoned. It's been replaced by C++/CLI. However, you aren't here for a history lesson, so let's get to the actual process:

To illustrate the problem(s) you might encounter, open up an empty project in Visual Studio and copy this into a file and try to build:

#using <mscorlib.dll>

using namespace System;

int main(){
	Console::Write("Hello World!");
	return 0;
}



You'll probably get a message similar to this:

Quote

1>.\Main.cpp(1) : fatal error C1190: managed targeted code requires a '/clr' option


CLR stands for Common Language Runtime. To resolve this issue, follow the following steps:

Step one: You need to set /clr to your command line.

Right click on the project
Go to properties->Configuration properties->C/C++->Command Line

In the box below type "/clr" no quotes

When you try to build now you'll get messages similar to this:

Quote

1>cl : Command line error D8016 : '/MT' and '/clr' command-line options are incompatible


If any variety of these happen, do steps 2 and 3.

Step two: You need to change the debugger type from auto or w/e you have to "managed only"

Right click on the project
Go to properties->Configuration properties->Debugging->Debugger Type

Change to "Managed Only

Step 3:
You need to change the Runtime Library from Multithreaded to Multithreaded DLL

Right click on the project
Go to properties->Configuration properties->C/C++->Code Generation->Runtime Library

Change to "Multi-threaded DLL /MD"


The reason for steps 2 and 3 is because there are compile line issues between various settings. There are a few other options, but at this point and for this program it is irrelevant.


Hopefully you found this helpful. If at all possible if you're going to use CLR/.NET go with C# as it's supported and has tons of resources around the web. However, I'm sure there was quite a bit of managed C++ code written before the plug was pulled, so hoop jumping is often necessary. Good luck!

0 Comments On This Entry

 

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    21 user(s) viewing

    21 Guests
    0 member(s)
    0 anonymous member(s)