Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NuGet Package #1132

Closed
btowntkd opened this issue Jun 15, 2018 · 6 comments
Closed

NuGet Package #1132

btowntkd opened this issue Jun 15, 2018 · 6 comments

Comments

@btowntkd
Copy link

Please release this library as a NuGet package.

@nlohmann
Copy link
Owner

Feel free to make a proposal. Nearly all supported package manager support were provided by the community via pull requests.

@stale

This comment has been minimized.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 18, 2018
@nlohmann nlohmann added pinned and removed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated labels Jul 21, 2018
@hnkb
Copy link

hnkb commented Jan 7, 2019

I have created a NuGet package for single-header distribution and added it to the default NuGet source (nuget.org). The scripts that generates this package can be found at https://github.com/hnkb/nlohmann-json-nuget/. I'll try to keep the NuGet packages up-to-date after each new release.

@nlohmann
Copy link
Owner

nlohmann commented Jan 7, 2019

Very nice! Can you provide some example code how to install the library and compile a simple program? I never used NuGet myself, but it would be good to know.

@hnkb
Copy link

hnkb commented Jan 8, 2019

Probably the easiest way to use NuGet packages is through Visual Studio graphical interface. Just right-click on a project (any C++ project would do) in “Solution Explorer” and select “Manage NuGet Packages…”

image

Now you can click on “Browse” tab and find the package you like to install.

image

Most of the packages in NuGet gallery are .NET packages and would not be useful in a C++ project. Microsoft recommends adding “native” and “nativepackage” tags to C++ NuGet packages to distinguish them, but even adding “native” to search query would still show many .NET-only packages in the list.

Nevertheless, after finding the package you want, just click on “Install” button and accept confirmation dialogs. After the package is successfully added to the projects, you should be able to just build and execute the project without the need for making any more changes to build settings.

A few notes:

  • NuGet packages are installed per project and not system-wide. The header and binaries for the package are only available to the project it is added to, and not other projects (obviously unless we add the package to those projects as well)
  • One of the many great things about your elegant work is that it is a header-only library, which makes deployment very straightforward. In case of libraries which need binary deployment (.lib, .dll and .pdb for debug info) the different binaries for each supported compiler version must be added to the NuGet package. Some library creators cram binary versions for all supported Visual C++ compiler versions in the same package, so a single package will support all compilers. Some others create a different package for each compiler version (and you usually see things like “v140” or “vc141” in package name to clarify which VC++ compiler this package supports).
  • Packages can have dependency to other packages, and in this case, NuGet will install all dependencies as well as the requested package recursively.

What happens behind the scenes

After you add a NuGet package, three changes occur in the project source directory. Of course, we could make these changes manually instead of using GUI:

image

  1. A packages.config file will be created (or updated to include the package name if one such file already exists). This file contains a list of the packages required by this project (name and minimum version) and must be added to the project source code repository, so if you move the source code to a new machine, MSBuild/NuGet knows which packages it has to restore (which it does automatically before each build).

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="nlohmann.json" version="3.5.0" targetFramework="native" />
    </packages>
  2. A packages folder which contains actual files in the packages (these are header and binary files required for a successful build, plus a few metadata files). In case of this library for example, it contains json.hpp:

    image

    This directory should not be added to the project source code repository, as it will be restored before each build by MSBuild/NuGet. If you go ahead and delete this folder, then build the project again, it will magically re-appear!

  3. Project MSBuild makefile (which for Visual C++ projects has a .vcxproj extension) will be updated to include settings from the package.

    image

    The important bit for us here is line 170, which tells MSBuild to import settings from packages\nlohmann.json.3.5.0\build\native\nlohmann.json.targets file. This is a file the package creator created and added to the package (you can see it is one of the two files I created in this repository, the other just contains package attributes like name and version number). What does it contain?

    For our header-only repository, the only setting we need is to add our include directory to the list of AdditionalIncludeDirectories:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <ItemDefinitionGroup>
            <ClCompile>
                <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
            </ClCompile>
        </ItemDefinitionGroup>
    </Project>

    For libraries with binary files, we will need to add .lib files to linker inputs, and add settings to copy .dll and other redistributable files to output directory, if needed.

    There are other changes to the makefile as well:

    1. Lines 165-167 add the packages.config as one of project files (so it is shown in Solution Explorer tree view). It is added as None (no build action) and removing it wouldn’t affect build.

    2. Lines 172-177 check to ensure the required packages are present. This will display a build error if package directory is empty (for example when NuGet cannot restore packages because Internet connection is down). Again, if you omit this section, the only change in build would be a more cryptic error message if build fails.

    Changes to .vcxproj makefile should also be added to project source code repository.

    As you can see, the mechanism NuGet uses to modify project settings is through MSBuild makefiles, so using NuGet with other build systems and compilers (like CMake) as a dependency manager is either impossible or more problematic than useful.

How to create a NuGet package

There is a nuget.exe command line utility which can pack files into a NuGet package (which is a zip file with a certain structure, usually with a .nupkg extension) . After this file is created, we can upload it to a package source, like http://nuget.org/, which is the default location Visual Studio uses; but you can also create local package sources, e.g. if your company does not want to share its packages with outside.

To create a package, we need:

  • Actual header and binary files from the library.
  • A .targets file which contains added settings to MSBuild project file (see previous section)
  • A .nuspec file which contains package name, version, dependency to other packages and list of file to be added to package.

Then call nuget.exe, passing name of the .nuspec file and the package will be created. This can be added as a build step too, if needed.

@nlohmann
Copy link
Owner

nlohmann commented Jan 9, 2019

Thanks for the extensive documentation!!! I shall add a note to the README and link this issue for further information. I shall take your walk-through with the screenshots to the overworked documentation once it is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants