C++ 如何使用 MSCV 中的 Boost 文件系统库解决 LNK1104 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/470840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:30:56  来源:igfitidea点击:

How do I resolve LNK1104 error with Boost Filesystem Library in MSCV?

c++visual-c++boostfilesystemslinker

提问by Scott

I am having trouble getting my project to link to the Boost (version 1.37.0) Filesystem lib file in Microsoft Visual C++ 2008 Express Edition. The Filesystem library is nota header-only library. I have been following the Getting Started on Windowsguide posted on the official boost web page. Here are the steps I have taken:

我无法将我的项目链接到 Microsoft Visual C++ 2008 Express Edition 中的 Boost(版本 1.37.0)文件系统 lib 文件。文件系统库不是一个只有头文件的库。我一直在关注官方 boost 网页上发布的Windows 入门指南。以下是我采取的步骤:

  1. I used bjam to build the complete set of lib files using:

    bjam --build-dir="C:\Program Files\boost\build-boost" --toolset=msvc --build-type=complete
    
  2. I copied the /libsdirectory (located in C:\Program Files\boost\build-boost\boost\bin.v2) to C:\Program Files\boost\boost_1_37_0\libs.

  3. In Visual C++, under Project > Properties > Additional Library DirectoriesI added these paths:

    • C:\Program Files\boost\boost_1_37_0\libs
    • C:\Program Files\boost\boost_1_37_0\libs\filesystem\build\msvc-9.0express\debug\link-static\threading-multi

    I added the second one out of desperation. It is the exact directory where libboost_system-vc90-mt-gd-1_37.libresides.

  4. In Configuration Properties > C/C++ > General > Additional Include DirectoriesI added the following path:

    • C:\Program Files\boost\boost_1_37_0
  5. Then, to put the icing on the cake, under Tools > Options VC++ Directories > Library files, I added the same directories mentioned in step 3.

  1. 我使用 bjam 构建完整的 lib 文件集:

    bjam --build-dir="C:\Program Files\boost\build-boost" --toolset=msvc --build-type=complete
    
  2. 我将/libs目录(位于C:\Program Files\boost\build-boost\boost\bin.v2复制C:\Program Files\boost\boost_1_37_0\libs。

  3. 在 Visual C++ 中,在Project > Properties > Additional Library Directories 下,我添加了以下路径:

    • C:\Program Files\boost\boost_1_37_0\libs
    • C:\Program Files\boost\boost_1_37_0\libs\filesystem\build\msvc-9.0express\debug\link-static\threading-multi

    无奈之下,我加了第二个。它是libboost_system-vc90-mt-gd-1_37.lib所在的确切目录。

  4. Configuration Properties > C/C++ > General > Additional Include Directories 中,我添加了以下路径:

    • C:\Program Files\boost\boost_1_37_0
  5. 然后,为了锦上添花,在Tools > Options VC++ Directories > Library files 下,我添加了在步骤 3 中提到的相同目录。

Despite all this, when I build my project I get the following error:

尽管如此,当我构建我的项目时,我收到以下错误:

fatal error LNK1104: cannot open file 'libboost_system-vc90-mt-gd-1_37.lib'

Additionally, here is the code that I am attempting to compile as well as a screen shot of the aformentioned directory where the (assumedly correct) lib file resides:

此外,这里是我试图编译的代码以及(假设正确的)lib 文件所在的上述目录的屏幕截图:

#include "boost/filesystem.hpp"   // includes all needed Boost.Filesystem declarations
#include <iostream>               // for std::cout
using boost::filesystem;          // for ease of tutorial presentation;
                                  //  a namespace alias is preferred practice in real code

using namespace std;

int main()
{
    cout << "Hello, world!" << endl;

    return 0;
}

采纳答案by Scott

Ferruccio's answer contains most of the insight. However, Pukku made me realize my mistake. I am posting my own answer to give a full explanation. As Ferruccio explained, Filesystem relies on two libraries. For me, these are:

Ferruccio 的回答包含了大部分见解。然而,Pukku让我意识到了我的错误。我发布了我自己的答案以给出完整的解释。正如 Ferruccio 解释的那样,文件系统依赖于两个库。对我来说,这些是:

  • libboost_system-vc90-mt-gd-1_37.lib
  • libboost_filesystem-vc90-mt-gd-1_37.lib
  • libboost_system-vc90-mt-gd-1_37.lib
  • libboost_filesystem-vc90-mt-gd-1_37.lib

I must not have noticed that when I supplied the directory for libboost_filesystem-vc90-mt-gd-1_37.lib, the error output changed from

我一定没有注意到,当我为libboost_filesystem-vc90-mt-gd-1_37.lib提供目录时,错误输出从

fatal error LNK1104: cannot open file 'libboost_filesystem-vc90-mt-gd-1_37.lib'

to

fatal error LNK1104: cannot open file 'libboost_system-vc90-mt-gd-1_37.lib'

Causing me to think that the error was persisting. This lead me to post some rather inaccurate information. Also, after reading that Filesystem requires two libraries, I now see the significance of the keyword stagefor the bjam command. Supplying

让我认为错误仍然存​​在。这导致我发布一些相当不准确的信息。此外,在阅读了 Filesystem 需要两个库之后,我现在看到了 bjam 命令的关键字stage的重要性。供应

bjam --build-dir="C:\Program Files\boost\build-boost" --toolset=msvc --build-type=complete stage

Causes bjam to place an additional directory, aptly named stage, in the boost_1_37_0directory. This folder contains a folder named /lib, which has copies of all of the lib files in one place. This is convenient for Visual C++ because you can supply it with this single directory and it will take care of all of the dependencies.

导致 bjam在boost_1_37_0目录中放置一个额外的目录,恰当地命名为stage。此文件夹包含一个名为/lib的文件夹,该文件夹在一个位置包含所有 lib 文件的副本。这对 Visual C++ 来说很方便,因为您可以为它提供这个单一目录,它会处理所有依赖项。

回答by Ferruccio

boost::filesystem is dependent on boost::system, so you need both paths.

boost::filesystem 依赖于 boost::system,所以你需要两个路径。

Part of the problem is you're using the boost libs out of the build directories instead of the install directory (the boost build process should create both). The install/lib directory has all the libs so you only need to specify one path.

部分问题是您正在使用构建目录中的 boost 库而不是安装目录(boost 构建过程应该同时创建两者)。install/lib 目录包含所有库,因此您只需指定一个路径。

The boost build process builds each library in its own directory. At the end it copies all those .lib files into one common lib directory.

boost 构建过程在其自己的目录中构建每个库。最后,它将所有这些 .lib 文件复制到一个公共 lib 目录中。

Since you didn't specify an install directory as part of your build command (with --prefix=...), I believe the default is C:\Boost. Check to see if that directory is there and if so use C:\boost\include\ boost-1_37 for your include path and C:\boost\lib for your library path.

由于您没有在构建命令中指定安装目录(使用 --prefix=...),我相信默认值为 C:\Boost。检查该目录是否存在,如果存在,则使用 C:\boost\include\ boost-1_37 作为包含路径,使用 C:\boost\lib 作为库路径。

回答by david

I think the real original problem is related to the default boost build process on windows which expects static linking of a library which will have a name beginning libboost_sytem<etc..>. The macro you need is

我认为真正的原始问题与 windows 上的默认 boost 构建过程有关,该过程期望静态链接具有 name 的库beginning libboost_sytem<etc..>。你需要的宏是

#define BOOST_SYSTEM_DYN_LINK

which makes ensures that the Boost.System library is dynamically linked. The dynamic library name is boost_system<etc...>as apposed the the static library libboost_sytem<etc...>

这确保了 Boost.System 库是动态链接的。动态库名称boost_system<etc...>与静态库相同libboost_sytem<etc...>

回答by dvsdimas

Last answer is right. But you should find boost config file $BOOST\config\user.hpp and uncomment this directive #define BOOST_ALL_DYN_LINK. Now it begin use dynamic link with boost and it should works.

最后一个答案是对的。但是你应该找到 boost 配置文件 $BOOST\config\user.hpp 并取消注释这个指令#define BOOST_ALL_DYN_LINK。现在它开始使用带有 boost 的动态链接,它应该可以工作。

回答by Tyler Brock

I had this same problem, what you need to do is add the "lib" directory under the top level boost folder to the library path in Visual C++.

我遇到了同样的问题,您需要做的是将顶级boost文件夹下的“lib”目录添加到Visual C++中的库路径中。

This most definitely solved the issue for me.

这绝对为我解决了这个问题。

回答by Reunanen

The error you have posted complains about file libboost_system-vc90-mt-gd-1_37.lib, but in the directory you have only libboost_filesystem-vc90-mt-gd-1_37.lib, right?

您发布的错误抱怨文件 libboost_system-vc90-mt-gd-1_37.lib,但在您的目录中只有 libboost_文件system-vc90-mt-gd-1_37.lib,对吗?

Look for libboost_system-vc90-mt-gd-1_37.lib. If you find it, add the corresponding directory to the library search path. If you don't find it, see if you have boost_system-vc90-mt-gd-1_37.lib instead (like I do), and try copying that to the desired filename.

查找 libboost_system-vc90-mt-gd-1_37.lib。如果找到,将相应目录添加到库搜索路径中。如果你没有找到它,看看你是否有 boost_system-vc90-mt-gd-1_37.lib 代替(像我一样),然后尝试将它复制到所需的文件名。

回答by gimpf

The bjam command line should have built all versions of all libraries. Still, when you build with

bjam 命令行应该已经构建了所有库的所有版本。尽管如此,当你用

bjam --build-dir="C:\Program Files\boost\build-boost" --toolset=msvc --build-type=complete stage

(note the stageat the end) all libraries are copied to a common libs/ folder, so that MSVC's autolinking feature works when you only add this libs/ folder to your library path.

(注意stage最后)所有库都被复制到一个公共的 libs/ 文件夹中,这样当您只将此 libs/ 文件夹添加到您的库路径时,MSVC 的自动链接功能就会起作用。

I do not know if bjam without stagestill copies all those files to a single folder. If not, execute such a stagebuild to do this. If they are, well, sorry, configuration seems correct, maybe a minor typing error somewhere?

我不知道 bjam without 是否stage仍将所有这些文件复制到一个文件夹中。如果没有,请执行这样的stage构建来执行此操作。如果他们是,好吧,对不起,配置似乎是正确的,也许是某个地方的小打字错误?