25

I had a C++ project which was running perfectly. Now I copied the project to another folder and added the project to a WPF application solution.

Now the C++ project is giving me lot of build errors. One of them is "System' : a namespace with this name does not exist".

Below are the lines which creates this error.

#include "stdafx.h"

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;

The above lines are in AssemblyInfo.cpp file. I don't have any experience of C++, so finding it difficult to remove the errors. Any help is appreciated. Below is another error which might be helpful for you to resolve the issue.

managed targeted code requires a '/clr' option

Thanks in advance.

Narendra
  • 3,069
  • 7
  • 30
  • 51
  • Do you intend to write in C++ for .NET or natively? Existence of `System::*` shows clearly, that its a C++/CLI project (not C++/Win32). – Spook May 24 '13 at 10:24
  • 1
    Actually C++ is has some native code and wrapper and WPF does the UI. I know it is C++/CLI project. But why is it giving errors when added to another solution as this does not depend upon any other project. – Narendra May 24 '13 at 10:25
  • 2
    Check in project's preferences, whether the compiler knows, that it's a C++/CLI project, because it seems, that it doesn't :) – Spook May 24 '13 at 10:31
  • @Spook: Can you please tell how to check this? – Narendra May 24 '13 at 10:38
  • 4
    Project properties | Configuration properties | General | Common Language Runtime Support. If the project is a C++/CLI one, there should be `Common Language Runtime Support (/clr)`. – Spook May 24 '13 at 10:39
  • Thanks Spook, it let me end my day with success. – Narendra May 24 '13 at 11:03

3 Answers3

59

Set Common Language RunTime Support to be "Common Language RunTime Support (/clr)" in 2 places in your project properties :

For Visual Studio before 2019:

  • Configuration Properties -> General
  • Configuration Properties -> C/C++ -> General

For Visual Studio 2019:

  • Configuration Properties -> Advanced -> C++/CLI Properties
  • Configuration Properties -> C/C++ -> General
remram
  • 4,805
  • 1
  • 29
  • 42
Mif
  • 632
  • 7
  • 7
3

Mif's answer was a little incomplete for me. If you still have the issue, you'd might want to check if you set the correct C++-version. For me it specifically needed ISO C++17 because the VS default preset ISO C++14 (at least a preset in my version) does not support the required CLI-packages. Thus, still set the Runtime support to Common Language RunTime Support (/clr) and set the correct C++ Language Standard in the Configuration Properties -> General tab.

Note: As of now (October 2021) ISO C++20 is currently not supported in CLI and will also fail to find System - I only found the 17-version to be compatible.

Since its a little easer the visual way, here the screenshots. And also don't forget Yinon_90's hint to have the version as well as dependencies installed!

enter image description here enter image description here enter image description here

Markus
  • 2,265
  • 5
  • 28
  • 54
1

It was not enough for me when I just changed "Common Language RunTime Support (/ clr)".

I had to create the project in VS which would be a dedicated project for CLR: enter image description here

If you cannot see this option, you should add it in the VS installer first: enter image description here

Yinon_90
  • 1,315
  • 12
  • 17