C ++/C#解决方案中不可用的任何CPU [英] Any CPU not available in C++/C# solution

查看:231
本文介绍了C ++/C#解决方案中不可用的任何CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含C#和托管C ++项目的解决方案. 它在解决方案平台x64和x86中进行编译.由于它是托管C ++,因此我想创建一个任何CPU"解决方案并摆脱旧的解决方案.

I have a solution that contains C# and managed C++ projects. It compiles in the solution platform x64 and x86. Since it is managed C++ I wanted to create a 'Any CPU' solution and get rid of the old ones.

我将C ++项目链接器设置更改为针对x64和x86强制使用安全IL映像".

I changed the C++ project linker settings to Force Safe IL Image for both x64 and x86.

接下来,我使用配置管理器创建了一个名为任何CPU"的新解决方案平台.接下来,我添加了一个项目平台,也称为任何CPU".

Next, using the Configuration Manager, I created a new solution platform called 'Any CPU'. Next I added a project platform also called 'Any CPU'.

我继续将所有C#项目设置为任何CPU",但是对于C ++,我无法做到这一点.项目平台"Any CPU"不在下拉菜单中,也没有选项"New ...".

I proceeded to set all the C# projects to 'Any CPU', but for the C++ I can't do that. The project platform 'Any CPU' is not in the drop down, and there is also no option 'New...'.

VS对此很赞,因此我将其保持原样并开始构建.令我惊讶的是,尽管C ++的平台是x64,但DLL(来自C ++项目)的结果还是MSIL.编译x32时也会发生同样的情况,生成的DLL位于MSIL中.

VS is adement about it, so I kept it like it was and started a build. To my surprise the result DLL (from the C++ project) was MSIL even though the platform for C++ was x64. Same happens when compiling x32, the resulting DLL is in MSIL.

有什么作用? 为什么我不能将C ++项目设置为任何CPU"?

What gives? Why can't I set the C++ project to 'Any CPU'?

推荐答案

据我所知,您不能在Visual Studio中为C ++/CLI项目创建"AnyCPU"项目类型.但是,您可以 配置C ++/CLI项目(在"Win32"项目类型下),以便它可以作为纯安全的MSIL进行编译,而无需目标平台.这样做将使您的C ++/CLI DLL程序集可与"AnyCPU" C#项目一起使用. IE.它实际上是"AnyCPU",即使它不是Configuration Manager中的实际名称.

As far as I know, you cannot create an "AnyCPU" project type in Visual Studio for a C++/CLI project. However, you can configure your C++/CLI project (under the "Win32" project type) so that it compiles as pure, safe MSIL, without a target platform. Doing so will allow your C++/CLI DLL assembly to be used with an "AnyCPU" C# project. I.e. it's effectively "AnyCPU", even though that's not its actual name in the Configuration Manager.

在"C/C ++"项目设置中:

In the "C/C++" project settings:

  • 公共语言运行时支持:Safe MSIL Common Language RunTime Support (/clr:safe)

在链接器"项目设置中:

In the "Linker" project settings:

  • CLR图像类型:只需确保未将其明确设置为IJWPURE

注意:

  • 通过使用安全"项目类型,一些似乎影响平台类型的编译器和链接器选项将被忽略. IE.您无需遍历并将一切设置为非特定平台类型.就在上面.但是,如果您觉得更好,可以将其他选项设置为适当的值. :)
  • 安全"将阻止使用指针.如果这是一个重要的问题,那么显然可以进行更复杂的过程.请参阅从C ++/CLI项目创建纯MSIL程序集?了解详情.
  • 不要忘记,默认情况下,Visual Studio会创建C#项目,即使它们是"AnyCPU",即使它们在64位OS上执行,也将作为32位进程启动.如果依赖项是x86而不是预期的纯/安全MSIL,则这可能会隐藏平台不匹配的问题.只需注意一点即可(您可以通过在C#项目的构建"项目属性页面中取消选中首选32位"选项来控制它).
  • By using the "safe" project type, a few of the compiler and linker options which appear to affect platform type will be ignored. I.e. you don't have to go through and set everything to a non-specific platform type. Just the above. But you may set the other options to something appropriate, if it makes you feel better. :)
  • "Safe" will prevent the use of pointers. If this is an important issue, it is apparently possible to do albeit with a more complicated process. See Creating a pure MSIL assembly from a C++/CLI project? for details.
  • Don't forget that by default, Visual Studio will create C# projects that even though they are "AnyCPU" and even though they are executed on a 64-bit OS, will start up as a 32-bit process. This can hide platform-mismatch issues, if a dependency is x86 instead of pure/safe MSIL as intended. Just something be aware of (you can control this by unchecking the "Prefer 32-bit" option in the C# project's "Build" project properties page).

这篇关于C ++/C#解决方案中不可用的任何CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆