I am trying to write a simple "Hello World" to the console. I created an empty C++ project in Visual C++ 2008, and added this one file:

// main.cpp

using namespace System;

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

I get this error:

Error	1	error C2871: 'System' : a namespace with this name does not exist

I'm able to use the System namespace when I build a Windows Forms application, but not a console application, regardless of whether I use the precompiled headers. What do I need to do to use the System namespace?

Recommended Answers

All 2 Replies

You need to add the appropriate DLL as a reference. You can do that from the UI by right clicking on your project and choosing "References". From there you can add a new reference for .NET. Alternatively you can reference the DLL directly from your code with a #using statement:

#using <mscorlib.dll>

using namespace System;

int main()
{
  Console::WriteLine ( "Hello, world!" );
}
commented: This fixed the problem. +2

You need to add the appropriate DLL as a reference. You can do that from the UI by right clicking on your project and choosing "References". From there you can add a new reference for .NET. Alternatively you can reference the DLL directly from your code with a #using statement:

#using <mscorlib.dll>

using namespace System;

int main()
{
  Console::WriteLine ( "Hello, world!" );
}

Yep, those both both worked. FYI for anyone who runs into this in the future, if after doing the above, you get an error like this:

Error	1	fatal error C1190: managed targeted code requires a '/clr' option

check Configuration Properties -> General -> Common Language Runtime Support in the project properties and make sure that the following IS NOT the option selected:

No Common Language Runtime support

If it is, change it to one of the options that has "/clr" in it. All of them worked for me, though one is deprecated. After making that change, the program worked.

Thanks Narue!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.