Search code examples
c#visual-studiodependenciessharpdxtargets

Use SharpDX compiled from source?


I want to make use of the SharpDX library, a must-have is that I can compile the sourcecode myself. I can compile the latest source code from their GitHub with ease using the Net40 configuration. Now I would like to use the compiled project in my own project, I had found the following to do so: https://github.com/sharpdx/SharpDX/issues/379

This resulted in me doing the following steps:

  • Download and extract the SharpDX sourcecode to "./SharpDX"
  • Open the file "./SharpDX/SharpDX.sln"
  • Build the SharpDX source code with configuration Net40 in the configuration manager
  • Create file "./MyProject/Common.targets" with the following content:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
        <!-- set the location of SharpDX source code -->
        <SharpDXLocation Condition="'$(SharpDXLocation)' == ''">..\SharpDX</SharpDXLocation>
        <!-- the new property to include references automatically (as it was in old SharpDX.targets) -->
        <SharpDXIncludeReferences>true</SharpDXIncludeReferences>
        <!-- (optional) override DirectX version -->
        <SharpDXDirectXVersion>DirectX11</SharpDXDirectXVersion>
        <!-- disable usage of signed assemblies, as you won't have the needed private key to build them -->
        <SharpDXNoSigned>true</SharpDXNoSigned>
        </PropertyGroup>
    
        <Import Project="$(SharpDXLocation)\Build\SharpDX.targets" />
        <Import Project="$(SharpDXLocation)\Build\SharpDX.Toolkit.targets" />
    </Project>
    
  • Create file "./MyProject/SharpDXHelper.csproj" with the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BuildTarget">
        <Import Project="Common.targets" />
        <Target Name="BuildTarget">
            <Message Text="Building command for SharpDXHelper" Importance="high" />
        </Target>
        <Target Name="Rebuild">
            <Message Text="Rebuilding command for SharpDXHelper" Importance="high" />
        </Target>
        <Target Name="Clean">
            <Message Text="Cleaning command for SharpDXHelper" Importance="high" />
        </Target>
    </Project>
    
  • Open the file "./MyProject/SharpDXHelper.csproj" with Visual Studio.

If I expand the "References" part of the "./MyProject/SharpDXHelper.csproj" I see all the required references with a warning icon, the warnings are like the following:

Warning 13 The referenced component 'SharpDX' could not be found. SharpDXHelper

I don't understand what I have done wrong, why does it not find the correct components? I realize the "Toolkit" part is gone, and I expect those to fail loading, but I don't expect the others to fail.

To my understanding, I should be able to use the SharpDX library without issues if I add the "SharpDXHelper.csproj" as a dependency to any other project, am I correct on this one?

An image of the resulting problem can be found here: http://puu.sh/dfmP4/c6ac1165ed.png


Solution

  • To my understanding, I should be able to use the SharpDX library without issues if I add the "SharpDXHelper.csproj" as a dependency to any other project, am I correct on this one?

    Not really.

    A referenced project is not evaluated as an imported project (using Import task).

    • Referencing a Project B1 from a Project A1: the project B1 is considered as a separable and compilable project. All the properties and targets defined by B1 are not visible in A1.
    • Importing a project B1 into A1 is like inlining the project B1 into A1. All properties and targets defined in B1 are visible in A1 (and overriding everything that is defined before the inclusion point)

    This is a reason why we use ".targets" extension for imported projects to differentiate them from referenced projects.

    Though It may be possible to develop such a referenced Project B1 that would be able to be "copied local" by A1 when compiling A1, but this would require to dig into how .NET/C# referenced projects are getting cross-compiled in order to do this. This is feasible, but quite a challenge to develop.

    I recommend you to stick with the Import solution.