1

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?

2
Contributors
2
Replies
12
Views
8 Years
Discussion Span
Last Post by VernonDozier
1

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!" );
}
Votes + Comments
This fixed the problem.
0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.