如何将引用添加到由C#项目称为非托管C ++项目? [英] How do I add a reference to an unmanaged C++ project called by a C# project?

查看:253
本文介绍了如何将引用添加到由C#项目称为非托管C ++项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个解决方案(the.sln)

One solution (the.sln)

一个C ++项目(mycppproject.vcxproj在2010or mycppproject.vcproj 2008年),它编译一个本机DLL导出一些功能(S)。在调试这个建立C:\\输出\\调试\\ mycppproject_d.dll和发布此建立C:\\输出\\发布\\ mycppproject.dll

One C++ project (mycppproject.vcxproj in 2010or mycppproject.vcproj in 2008) which compiles a native DLL exporting some function(s). In debug this builds c:\output\Debug\mycppproject_d.dll and in release this builds c:\output\Release\mycppproject.dll.

一个C#控制台应用程序(mycsharpconsole.csproj)包含的PInvoke调用到DLL。

One C# console application (mycsharpconsole.csproj) containing PInvoke calls into the DLL.

全部编译罚款。

当我建,我希望能够从CSHARP项目中添加一个参考CPP DLL项目,以便它可以从相应的目录中复制相应的文件到\\ BIN \\ Debug目录的CSHARP项目建成成。

When I build, I would like to be able to add a reference from the csharp project to the cpp DLL project so that it can copy the appropriate file from the appropriate directory into the \bin\Debug directory the csharp project is built into.

这应该是可能的,因为IDE知道一切有了解的DLL被建在哪里,并在C#应用程序被建造。

This should be possible, since the IDE knows everything there is to know about where the DLL gets built, and where the C# application gets built.

在Visual Studio 2010中:

In Visual Studio 2010:

我试过依赖关系...在CSHARP项目,并增加对mycppproject的依赖,但没有效果。

I've tried "Dependencies..." on the csharp project and adding a dependency on mycppproject, but that has no effect.

我试过添加引用...在CSHARP项目,并增加了CPP项目的引用,但我得到一个警告消息目标框架版本为项目mycppproject比目前更高的项目目标框架版本。你想反正添加此引用到你的项目? (是/否/取消)。

I've tried "Add Reference..." on the csharp project and adding a reference to the cpp project, but I get a warning message 'The Target Framework version for the project "mycppproject" is higher than the current project Target Framework version. Would you like to add this reference to your project anyway?' (Yes/No/Cancel).

单击是产生错误信息不能被添加到mycppproject参考。

Clicking "Yes" produces the error message "A reference to mycppproject" could not be added."

推荐答案

我会按照 Slaks第二个答案 ..

[...]你可以添加一个链接到非托管DLL如C#项目中的文件,并设置生成操作为None,并复制到输出目录复制如果较新。

...后面的my评论,调试和发布区分构建(即使是有点hackish的,因为它需要你手动编辑C#项目文件)

... followed by my comment, to differentiate between Debug and Release builds (even if is a little bit "hackish", since it requires you to manually edit the C# project file)

打开你的C#项目的csproj文件,用文本编辑器和搜索所有YourNativeCppProject.dll事件(不带.DLLsubfix,因此,如果您添加PDB文件作为链接也一样,你会发现出现了多次),并使用宏改变包括路径,例如:包括=$(SolutionDir)$(ConfigurationName)\\ YourNativeCppProject.dll

PS:如果你看一下属性(F4),VS显示你调试的路径,即使你切换到发布配置,但如果你编译,你会看到复制到输出DLL是发布版本*

PS: if you look at the properties (F4), VS shows you the Debug's path even if you switch to the Release configuration, but if you compile, you'll see that the dll copied to output is the release version*

这篇关于如何将引用添加到由C#项目称为非托管C ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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