包括Boost文件系统头

问题描述 投票:0回答:2

我有一个大项目。

N.cpp我需要使用boost::filesystem::exists(path)来检查路径是否有效。

为此,我加入了<boost/filesystem.hpp>

我收到以下错误:

Error    2    error LNK2005: "public: enum boost::filesystem::file_type __cdecl boost::filesystem::file_status::type(void)const " (?type@file_status@filesystem@boost@@QEBA?AW4file_type@23@XZ) already defined in N.obj    D:\MProject\DA\boost_filesystem-vc100-mt-gd-1_53.lib(boost_filesystem-vc100-mt-gd-1_53.dll)    DA

Error    1    error LNK2005: "public: __cdecl boost::filesystem::path::~path(void)" (??1path@filesystem@boost@@QEAA@XZ) already defined in N.obj    D:\MProject\DAboost_filesystem-vc100-mt-gd-1_53.lib(boost_filesystem-vc100-mt-gd-1_53.dll)    DA

Error    3    error LNK1104: cannot open file 'libboost_filesystem-vc100-mt-gd-1_53.lib'    D:\MProject\DA\LINK    DA

如果我不包含头文件,我得到:

Error    2    error C3861: 'exists': identifier not found    D:\MProject\DA\ThirdParty\N.cpp    108    1    DA
Error    1    error C2653: 'boost' : is not a class or namespace name    D:\MProject\DA\ThirdParty\N.cpp    108    1    DA

什么是使用boost::filesystem::exists的正确方法,以便整个事情可以编译?

N.cpp

#include <boost/filesystem.hpp>
.....
CHECK( boost::filesystem::exists(*i), std::string("file ").append(*i).append(" does not exist").c_str() );

.....
c++ boost header lnk2005
2个回答
4
投票

我通过添加到N.cpp解决了这个问题:

#ifndef BOOST_ALL_DYN_LINK
#   define BOOST_ALL_DYN_LINK
#endif 
#include <boost/filesystem.hpp>

3
投票

如果要使用boost::filesystem,则必须包含头文件(直接或间接通过其他包含)。

前三个错误是链接器错误。 Boost的某些部分只是标题,即你不想在使用它们时编译Boost。不幸的是,文件系统不是其中之一。这意味着您必须像编辑here一样编译Boost。之后你必须告诉你的链接器它可以找到你刚刚创建的二进制文件,它应该编译好(如果你没有做任何其他错误;-))。

© www.soinside.com 2019 - 2024. All rights reserved.