简体   繁体   English

将库与 NuGet 包链接为 Visual Studio 中的依赖项

[英]Linking Libraries with NuGet Packages as dependencies in Visual Studio

I have a Console Project LinkExample that contains a main.cpp .我有一个包含main.cpp的控制台项目LinkExample

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

The LinkExample links a static Library Project named Library . LinkExample链接名为Library的静态库项目。 Library.Lib is linked correctly. Library.Lib已正确链接。

Library contains a Logger.hpp库包含一个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: NuGet 包是:

  <?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 . LinkExample不会构建并显示文件libboost_log-vc140-mt-gd-1_63.lib的错误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?静态链接时,我希望将此库烘焙到Library ,为什么不会发生这种情况,我该如何解决?

I am aware that I could add packages to LinkExample but that is not a long term solution.我知道我可以向LinkExample添加包,但这不是一个长期的解决方案。

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.在一个更大的项目中,在将 boost 切换为动态链接后,这种方法开始崩溃。

How do I get rid of my root problem that the lib files are linked by my customer library?我如何摆脱 lib 文件由我的客户库链接的根本问题?

Note that the "Link Library Dependencies" setting does nothing here, using this setting would require a project setup that is incompatible with NuGet.请注意,“链接库依赖项”设置在这里没有任何作用,使用此设置需要一个与 NuGet 不兼容的项目设置。

This is the default behavior of Visual studio.这是 Visual Studio 的默认行为。 We could not use the NuGet Packages as dependencies directly in the LinkExample project, which provide by the linked customer library.我们不能直接在 LinkExample 项目中使用 NuGet 包作为依赖项,它由链接的客户库提供。 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. Visual Studio 在构建LinkExample 项目时只加载静态库项目的文件,不会加载静态库项目的那些依赖项。 So I agree with Jeroen Heier, if you want to use those packages to LinkExample project, you need to add packages to LinkExample.所以我同意 Jeroen Heier 的观点,如果你想在 LinkExample 项目中使用这些包,你需要将包添加到 LinkExample。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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