Max
Max

Reputation: 680

CMake CSharp reference nuget packges

I have a CSharp project configured via CMake, and I have a problem referencing nuget packages. I tried to add them via:

set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_REFERENCES
"../../packages/ExcelDna.Integration.0.34.6/lib/ExcelDna.Integration.dll")

After restoring the nuget packages, I still have to double click the reference in Visual Studio (2015) in order to compile the program successfully. Furthermore, is there a version number independent way to add references to nuget packages? And how is it possible to add ExtensionTargets required by some packages (e.g. ExcelDna.AddIn)?

Upvotes: 2

Views: 1578

Answers (1)

Kevin
Kevin

Reputation: 18323

EDIT: As of CMake 3.15, CMake supports referencing Nuget packages with VS_PACKAGE_REFERENCES. This is a cleaner solution than restoring the Nuget packages manually, and hard-coding the package paths in CMake. The VS_PACKAGE_REFERENCES target property now handles all of that overhead for you.

To add a Nuget package reference to a CMake target, use the package name and package version separated by an underscore _, like this:

set_property(TARGET ${PROJECT_NAME}
    PROPERTY VS_PACKAGE_REFERENCES "ExcelDna.Integration_0.34.6"
)

You can grab any version number in a range, with *, and append multiple packages using a semicolon:

set_property(TARGET ${PROJECT_NAME}
    PROPERTY VS_PACKAGE_REFERENCES "ExcelDna.Integration_0.34.*;ExcelDna.AddIn_1.0.0"
)

You can use VS_DOTNET_REFERENCE_<YourLibrary> to get CMake to find your Nuget package references. Try this:

set_property(TARGET ${PROJECT_NAME} PROPERTY 
    VS_DOTNET_REFERENCE_ExcelDna.Integration 
    ${CMAKE_BINARY_DIR}/packages/ExcelDna.Integration.0.34.6/lib/ExcelDna.Integration.dll
)

Note, the full DLL name must be appended to the VS_DOTNET_REFERENCE_ directive to create the full variable. However, I have not seen a version number independent way to load the Nuget packages, and I've had to manually edit my CMake files to include these packages. You can check out this answer for a more detailed explanation.

Upvotes: 1

Related Questions