Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a reference to an unmanaged C++ project called by a C# project?

One solution (the.sln)

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.

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

All compiles fine.

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.

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.

In Visual Studio 2010:

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

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).

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

like image 682
WaffleSouffle Avatar asked Feb 24 '11 16:02

WaffleSouffle


People also ask

How do you add references to a C++ project?

On the Project menu, click Add References. In Visual C++, click References on the Project menu, and then click Add New Reference. In the Add References dialog box, click the tab that corresponds with the category that you want to add a reference to. In Visual C++, click the Browse tab in the Add References dialog box.

How do I add a project reference in Visual Studio?

Restart Visual Studio and open your app. Right-click on the References or Dependencies node in the project that caused the error and choose Add Reference.


1 Answers

You cannot add a reference to an unmanaged DLL.

Instead, you should make a post-build task to copy the file.

Alternatively, you can add a link to the unmanaged DLL as a file in the C# project, and set Build Action to None and Copy to Output Directory to Copy If Newer.

like image 146
SLaks Avatar answered Sep 19 '22 08:09

SLaks