Copy native dependencies locally in a Visual Studio project - c++

I have a mixed mode C++ project producing a managed dll assembly exporting some CLR classes (call it Managed.dll). This project is using a native dll, (call it Native.dll).
When I reference the Managed.dll from another project producing Client.exe, everything works as expected, except than I need to manually copy the Native.dll in the same folder as Client.exe.
If there a way to convince VS to copy locally (in the bin folder of Client.exe) not only Managed.dll but Native.dll as well?
I have tried to include Native.dll as a dependency assembly in the manifest but this didn't help.
Edit
Managed.dll is going to be a redistributable assembly. It will be installed in a folder in "C:\Program Files.....". When a developer using Visual Studio adds a reference to Managed.dll, Native.dll should be also copied in the \bin folder of his project.

There are several ways to tell the VS to copy dlls to the destination folder:
1.Add the dll as a resource of the project. And tell the VS to copy it if the dll is newer
2.Add a new project that reference to the dll project, and set the OutDir to the folder you want. This project does nothing but copy the dll.
3.Use a PostBuildEvent in vcxproj file
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
</ClCompile>
<Link>
</Link>
<PostBuildEvent>
<Command>
echo off
mkdir "$(ProjectDir)..\..\bin\$(Configuration)\"
copy "$(OutDir)xxx.dll" "$(ProjectDir)..\..\lib\$(Configuration)\"
echo on
</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
4.Use a PreBuildEvent in vcxproj file
5.Use CustomBuild in vcxproj file
<ItemGroup>
<CustomBuild Include="..\..\xxx.dll">
<FileType>Document</FileType>
<Command>
call mkdir "$(OutDir)" 2>nul &
copy /Y "..\..\xxx.dll" "$(OutDir)xxx.dll"
</Command>
<Message>Copying xxx.dll to $(OutDir)\xxx.dll</Message>
<Outputs>$(OutDir)\xxx.dll</Outputs>
</CustomBuild>
</ItemGroup>
6.Use a makefile and copy dll in makefile. and use nmake to build
7.Write a bat file that do the copy job, and invoke the bat file as in 3-6
8.Use script such as python, which can also download the dll from internet. And invoke the py file as in 3-6.
9.Other build tools can help too, such as gradle
10.Make it a NuGet plugin
11.Sometimes I just write a bat, and execute the bat manually.
Update 01 (Self extract dll example):
1.Add you native dll as resource of managed dll
2.Add this init() method
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace DllSelfExtract
{
public class SelfExtract
{
public static void Init()
{
String managedDllPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
String nativeDllPath = managedDllPath.Replace("file:///", "").Replace("DllSelfExtract.DLL", "TestDll.dll");
if(!File.Exists(nativeDllPath))
{
Stream dllIn = Assembly.GetExecutingAssembly().GetManifestResourceStream("DllSelfExtract.TestDll.dll");
if (dllIn == null) return;
using (Stream outFile = File.Create(nativeDllPath))
{
const int sz = 4096;
byte[] buf = new byte[sz];
while (true)
{
int nRead = dllIn.Read(buf, 0, sz);
if (nRead < 1)
break;
outFile.Write(buf, 0, nRead);
}
}
}
//LoadLibrary Here
}
}
}
3.In project that use your managed dll, invoke init() method first
SelfExtract.Init();
Update 02 (NuGet example):
1.Create a new NuGet project
2.Place the managed assemblies in the /lib directory
3.Place the non-managed shared libraries and related files in the /build subdirectory and rename all non-managed *.dll to *.dl_
4.Add a custom .targets file in the /build subdirectory with something like the following contents :
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<AvailableItemName Include="NativeBinary" />
</ItemGroup>
<ItemGroup>
<NativeBinary Include="$(MSBuildThisFileDirectory)*">
<TargetPath></TargetPath>
</NativeBinary>
</ItemGroup>
<PropertyGroup>
<PrepareForRunDependsOn>
$(PrepareForRunDependsOn);
CopyNativeBinaries
</PrepareForRunDependsOn>
</PropertyGroup>
<Target Name="CopyNativeBinaries" DependsOnTargets="CopyFilesToOutputDirectory">
<Copy SourceFiles="#(NativeBinary)"
DestinationFiles="#(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).dll')"
Condition="'%(Extension)'=='.dl_'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
<Copy SourceFiles="#(NativeBinary)"
DestinationFiles="#(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).%(Extension)')"
Condition="'%(Extension)'!='.dl_'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
</Target>
</Project>
5.Add build rule for build folder in Package.nuspec
<files>
<file src="lib\" target="lib" />
<file src="tools\" target="tools" />
<file src="content\" target="content" />
<file src="build\" target="build" />
</files>
6.Build the package
7.In your other C# project just add this NuGet package.

Using /ASSEMBLYLINKRESOURCE option in linker properties seems to be the simplest solution. It makes Visual Studio consider the native dll as a part of the assembly. Also, according to documentation provided by Microsoft, allows the native dll to be installed in the Global Assembly Cache
To set this linker option in a Visual C++ project:
Right click on the project name and select Properties
Select the Linker folder
In the Input property page find the Assembly Link Resource option
Write the file name of the native assembly e.g. MyNative.dll
You will need a Post Build event to copy the native dll to the output folder.
Referencing the managed assembly from any other Visual Project, forces the native dll to be copied together with the managed assembly in the /bin folder.

Related

Dependent project cannot see headers from a library in refferences in Visual Studio

I am learning how to make a static library. I started with windows and Visual Studio.
The directory structure looks like this:
- MyLibraryProject
- include
- MyLibraryProject
- MyLibraryHeader.h
- src
- MyLibrarySource.cpp
- build
- MyLibraryProject.vcxproj
- MyDependentProject
- main.cpp
- MyDependentProject.vcxproj
MyLibraryProject.vcxproj has the following settings:
Setting
Value
Configuration type
Static library (.lib)
Additional Include Directories
$(MSBuildThisFileDirectory)../include/MyLibraryProject
MyDependentProject.vcxproj has no special settings, except I added MyLibraryProject onto refferences, the image features actual names I used:
If I use relative paths in main.cpp, I can build the project - the static linking works just fine and it runs:
#include "../MyLibraryProject/include/MyLibraryProject/MyLibraryHeader.h"
However, I want to include the headers like this:
// fatal error C1083: Cannot open include file: 'MyLibraryProject/MyLibraryHeader.h': No such file or directory
#include <MyLibraryProject/MyLibraryHeader.h>
And that just does not work. I also tried to use property sheet but couldn't get that to work either. I've been searching the internet, but generally found claims that if you add a reference, both headers and static libs will work.
Here's the full repository, if you're willing to take a look. Or ask in the comments if there's information missing.
Project references do not provide the dependent project with any information about headers. The most flexible way to do this instead (in Visual Studio) are property sheets. I created a file MyLibraryProject/build/MyLibraryProjectDependency.props:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
And I added it to MyDependentProject.vcxproj in Property explorer in Visual Studio. This solved the issues and headers are now seen on the path I want them.

Custom Build Files changes do not trigger project rebuild in VS 2017

In my C++ VS project I added a custom target to compile shader files and set it as a initial target. This is the project xml
<Project InitialTargets="CompileShaders" DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
..... Normal Default VS C++ content here ......
<ItemGroup>
<GLSLShader Include="SPIR-V\canvas2D.vert" />
<GLSLShader Include="SPIR-V\canvas2D.frag" />
</ItemGroup>
<Target Name="CompileShaders" Inputs="#(GLSLShader)" Outputs="SPIR-V\shaders_bytecode.h" >
<PropertyGroup>
<OriginalFileName>%(GLSLShader.Filename)%(GLSLShader.Extension)</OriginalFileName>
</PropertyGroup>
<Message Text="Start Compiling GSLANG #(GLSLShader) " />
<Message Condition="'$(VULKAN_SDK)'==''" Text="Error, cant find environment variable VULKAN_SDK, Make sure that the Lunar Vulkan SDK is installed" />
<Message Condition="'$(VULKAN_SDK)'!=''" Text="$(VULKAN_SDK)\Bin\glslangValidator.exe %(GLSLShader.Filename)%(GLSLShader.Extension) -V --vn $([System.String]::Copy('%(GLSLShader.Filename)%(GLSLShader.Extension)').Replace('.','_')) -o %(GLSLShader.Filename)%(GLSLShader.Extension).h" />
<Exec Condition="'$(VULKAN_SDK)'!=''" Command="$(VULKAN_SDK)\Bin\glslangValidator.exe %(GLSLShader.Filename)%(GLSLShader.Extension) -V --vn $([System.String]::Copy('%(GLSLShader.Filename)%(GLSLShader.Extension)').Replace('.','_')) -o %(GLSLShader.Filename)%(GLSLShader.Extension).h" WorkingDirectory="$(ProjectDir)\SPIR-V" />
<Exec Condition="'$(VULKAN_SDK)'!=''" Command="del shaders_bytecode.h" WorkingDirectory="$(ProjectDir)\SPIR-V" />
<Exec Condition="'$(VULKAN_SDK)'!=''" Command="type %(GLSLShader.Filename)%(GLSLShader.Extension).h >> shaders_bytecode.h" WorkingDirectory="$(ProjectDir)\SPIR-V" />
<Exec Condition="'$(VULKAN_SDK)'!=''" Command="del %(GLSLShader.Filename)%(GLSLShader.Extension).h" WorkingDirectory="$(ProjectDir)\SPIR-V" />
</Target>
</Project>
if I change any .cpp .h file and build the solution, the shaders are compiled together with the rest of the project, but if I change only the shader files (i.e SPIR-V\canvas2D.vert) the project is not built. VS says that the project is up to date.
Now the strange thing, If I run the project using msbuild on the terminal ouside VS, the shader files changes are enough to trigger the rebuild. Go figure....!!!
It looks like something related to how VS build projects. It is outside the msbuild.
Aha!! Found it in this article. It turns out visual studio bypass msbuild and uses some other criteria to verify if a project is up to date or not. If the criteria fails it then runs the msbuild on that project.
To override the visual studio behavior set the property DisableFastUpToDateCheck as true in the Globals property group of your project's xml:
<PropertyGroup Label="Globals">
<!-- Other Global Property Settings -->
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
And it is done... I wonder if there is a away to tell Visual Studio FastUpToDateCheck Mechanism to also pay attention to the custom build files ????? The solution above will suffice for now.

Include path relative to props file in visual studio

I'm trying to create property file with include path to use in all my c++ project.
Here is repository structure.
/
/Libs
/Libs2
A.h
B.h
/Sln1
Sln1.sln
Proj1.vcxproj
Sln2.sln
Proj2.vcxproj
Props.props
I want use property file (Props.props) to add the following include path to both projects ( C:\\Libs;C:\\Libs\Libs2).
Currently I have macro in my property file:
Name Value
ProjRoot C:\<path to rep root>
And I use it in include string: $(ProjRoot)\Libs;$(ProjRoot)\Libs\Libs2
The problem with this solution is hardcoded absolute path in macro value. If my repository will be cloned on another drive I will have to change it manually.
Can I use path relative to property file in macro value?
I.e.:
Name Value
ProjRoot ./
Where ./ will resolve to path of Props.props file in all projects which will use this property file.
I cannot use $(SolutionDir) and $(ProjectDir) because there are may solutions and projects in different nesting level so path relative to them would not work.
Thank you.
Do do this one should manually edit props file and include the following:
<PropSheetPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</PropSheetPath>
This will create property PropSheetPath with property file folder.
Found the answer here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/2817cae7-3a71-4701-839a-9bf47af7c498/property-sheets-macro-to-reference-location-of-property-sheet?forum=vcgeneral
Just to improve previous answer... Here how it looks as a full example (I'm using a bunch of small property sheets to add third party libraries in a modular way). This is an example for adding paths to include folders for C++ compiler and library folders for linker to add CEGUI library into project (debug version, I use separate prop sheet for Release).
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros">
<PropSheetPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</PropSheetPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Language)'=='C++'">
<CAExcludePath>$(PropsheetPath)..\..\install\windows\Debug\include\cegui-0;$(CAExcludePath)</CAExcludePath>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>CEGUI_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(PropsheetPath)..\..\install\windows\Debug\include\cegui-0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(PropsheetPath)..\..\install\windows\Debug\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>DbgHelp.lib;CEGUIBase-0_Static_d.lib;CEGUICommonDialogs-0_Static_d.lib;CEGUICoreWindowRendererSet_Static_d.lib;CEGUIExpatParser_Static_d.lib;CEGUIOpenGLRenderer-0_Static_d.lib;CEGUITGAImageCodec_Static_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

How to add C++ library in a .NET Core project

How is the way to add a C++ Library in a .NET Core project (Class Library). I tried creating a nuget package but doesn't work. I got this error:
An unhandled exception of type 'System.DllNotFoundException' occurred in "NameOfDll.dll"
When I add the nuget package the project.json add the following reference:
"dependencies": {
"Core": "1.0.0-*",
"NETStandard.Library": "1.6.0",
"NameOfDll.dll": "1.0.0"
},
dependencies attribute in project.json specifies package dependencies, because of this NuGet handles NameOfDll.dll as a package ID, but not as a dll name.
To add native C++ dlls into you NuGet package for .xproj library you should do the following steps:
Put your NameOfDll.dll in \lib directory near MyClassLibrary.xproj
Open project.json file and add there:
"packOptions": {
"files" : {
"includeFiles" : "lib/NameOfDll.dll"
}
}
Execute dotnet pack
NameOfDll.dll will be included into NuGet package under the path lib\NameOfDll.dll.
To add native C++ dlls into your NuGet package for .csproj library you should do the following steps:
I assume you have managed project with name MyClassLibrary.csproj.
Create new NuGet package for you class library with nuget spec command.
Create \lib, \build, \content and \tools directories near MyClassLibrary.nuspec.
Copy all your native dlls into the \build folder.
Change extension of copied native dlls to .native.
Create MyClassLibrary.targets with the following content inside \build folder:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<AvailableItemName Include="NativeBinary" />
</ItemGroup>
<ItemGroup>
<NativeBinary Include="$(MSBuildThisFileDirectory)*">
<TargetPath></TargetPath>
</NativeBinary>
</ItemGroup>
<PropertyGroup>
<PrepareForRunDependsOn>
$(PrepareForRunDependsOn);
CopyNativeBinaries
</PrepareForRunDependsOn>
</PropertyGroup>
<Target Name="CopyNativeBinaries" DependsOnTargets="CopyFilesToOutputDirectory">
<Copy SourceFiles="#(NativeBinary)"
DestinationFiles="#(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).dll')"
Condition="'%(Extension)'=='.native'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
<Copy SourceFiles="#(NativeBinary)"
DestinationFiles="#(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).%(Extension)')"
Condition="'%(Extension)'!='.native'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
</Target>
</Project>
Hint: The .targets content is taken from this question.
The above .targets file will be injected on an installation of the NuGet package into the target project file.
Add the following lines into your MyClassLibrary.nuspec:
<files>
<file src="lib\" target="lib" />
<file src="tools\" target="tools" />
<file src="content\" target="content" />
<file src="build\" target="build" />
</files>
Execute nuget pack MyClassLibrary.csproj to build your NuGet package.
Here is a one-click way to do it in .net core 2. I used it in my website and it works.
Right click your project in Solution Explorer, select Add->Existing Item, browse and choose your dll (select show all extensions). Then your dll will appear in Solution explorer.
In the Solution Explorer, right-click your dll -> Properties. Set build action: Content, and Copy to Output Directory: Copy if newer (or always copy).
It's done. Use your dll in your code staightforward like this:
[DllImport("my_dll.dll", CallingConvention = CallingConvention.Cdecl)]

What is the $(PackageConfiguration) variable in a Visual Studio project?

I ran into a linker error (Couldn't open sqlite3.lib) when making a WinRT application. The funny thing is, it only happens in a new configuration that I made (Master, as opposed to Debug or Release). I cloned the new configuration from Release, so it should be identical except for a few preprocessor defines. I found the following entry as a default in my "Library Directories" section under "VC++ Directories"
$(FrameworkSDKRoot)..\v8.1\ExtensionSDKs\SQLite.WinRT81\3.8.0.2\DesignTime\$(PackageConfiguration)\$(PlatformTarget)
However, I can't find any information on what the PackageConfiguration variable actually expands to. I guessed it might be Debug / Release but the folders at that location on the file system are Debug and Retail. If I add another entry with "Retail" instead of $PackageConfiguration then I can build the program properly, but it seems strange. Does anyone know how this variable is defined?
This value comes from the SQLite.WinRT81.props file located in the SQLite extension SDK which is usually installed here:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1\ExtensionSDKs\SQLite.WinRT81\3.8.2\DesignTime\CommonConfiguration\neutral
On my machine the contents look like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PackageConfiguration Condition="'$(Configuration)' == ''">Debug</PackageConfiguration>
<PackageConfiguration Condition="'$(Configuration)' == 'Debug'">Debug</PackageConfiguration>
<PackageConfiguration Condition="'$(Configuration)' == 'Release'">Retail</PackageConfiguration>
</PropertyGroup>
<PropertyGroup>
<IncludePath>$(FrameworkSDKRoot)\..\v8.1\ExtensionSDKs\SQLite.WinRT81\3.8.2\DesignTime\CommonConfiguration\Neutral;$(IncludePath)</IncludePath>
<LibraryPath>$(FrameworkSDKRoot)\..\v8.1\ExtensionSDKs\SQLite.WinRT81\3.8.2\DesignTime\$(PackageConfiguration)\$(PlatformTarget);$(LibraryPath)</LibraryPath>
<PropertySheetDisplayName>SQLite.WinRT81, 3.8.2</PropertySheetDisplayName>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>sqlite3.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>