简体   繁体   中英

Linking Libraries with NuGet Packages as dependencies in Visual Studio

I have a Console Project LinkExample that contains a main.cpp .

#include <Library/Logger.hpp>
int main()
{
    WriteSomething();
    return 0;
}

The LinkExample links a static Library Project named Library . Library.Lib is linked correctly.

Library contains a Logger.hpp

#pragma once
void WriteSomething();

Logger.cpp

#include <boost/log/trivial.hpp>
#include <Library/Logger.hpp>

void WriteSomething()
{
    BOOST_LOG_TRIVIAL(trace) << "Trace";
}

The NuGet Packages are:

  <?xml version="1.0" encoding="utf-8"?>
  <packages>
    <package id="boost" version="1.63.0.0" targetFramework="native" />
    <package id="boost_log-vc140" version="1.63.0.0" targetFramework="native" />
  </packages>

LinkExample does not build and shows the error LNK1104 for file libboost_log-vc140-mt-gd-1_63.lib .

When linking statically I would want this library to be baked into Library , why does this not happen and how can I fix that?

I am aware that I could add packages to LinkExample but that is not a long term solution.

Adding packages until the solution runs would include these packages.

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="boost" version="1.63.0.0" targetFramework="native" />
  <package id="boost_date_time-vc140" version="1.63.0.0" targetFramework="native" />
  <package id="boost_filesystem-vc140" version="1.63.0.0" targetFramework="native" />
  <package id="boost_log-vc140" version="1.63.0.0" targetFramework="native" />
  <package id="boost_system-vc140" version="1.63.0.0" targetFramework="native" />
  <package id="boost_thread-vc140" version="1.63.0.0" targetFramework="native" />
</packages>

In a larger Project things started falling apart with this approach after switching boost to dynamic linking.

How do I get rid of my root problem that the lib files are linked by my customer library?

Note that the "Link Library Dependencies" setting does nothing here, using this setting would require a project setup that is incompatible with NuGet.

This is the default behavior of Visual studio. We could not use the NuGet Packages as dependencies directly in the LinkExample project, which provide by the linked customer library. The Visual studio only load the files of static Library Project when we build the LinkExample project, those dependencies of static Library Project would not be loaded. So I agree with Jeroen Heier, if you want to use those packages to LinkExample project, you need to add packages to LinkExample.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM