C++ 使用boost::filesystem时链接器错误?

C++ 使用boost::filesystem时链接器错误?,c++,visual-studio,boost,linker,boost-filesystem,C++,Visual Studio,Boost,Linker,Boost Filesystem,我有以下代码: #include <iostream> #include <boost\filesystem.hpp> int main(){ const char* file_path = "my_path"; std::cout << boost::filesystem::file_size(file_path) << std::endl; } #包括 #包括 int main(){ const char*file_path

我有以下代码:

#include <iostream>
#include <boost\filesystem.hpp>

int main(){
    const char* file_path = "my_path";
    std::cout << boost::filesystem::file_size(file_path) << std::endl;
}
#包括
#包括
int main(){
const char*file_path=“my_path”;
std::cout Main.obj:错误LNK2019:未解析的外部符号“类boost::系统::错误\u类别const和\uu cdecl boost::系统::通用\u类别(void)”(?通用_category@system@boost@@YAAEBVerror_category@12@XZ)在函数“void\uu cdecl boost::system::`errno\u ecat'(void)的动态初始值设定项”中引用(??\u Eerrno)_ecat@system@boost@@YAXXZ)
1> Main.obj:错误LNK2019:未解析的外部符号“void\uu cdecl boost::filesystem::path\u traits::convert(char const*,char const*,class std::basic\u string&,class std::codevt const&”)(?convert@path_traits@filesystem@boost@@YAXPEBD0AEAV?$basic_string@_WU?$char_traits@_W@性病病毒$allocator@_W@2@@std@@AEBV$codecvt@_WDH@5@@Z)在函数“void\uuu cdecl boost::filesystem::path\u traits::dispatch(类std::basic\u string const&,类std::basic\u string&,类std::codecvt const&)”中引用(??$dispatch@V?$basic_string@_WU?$char_traits@_W@性病病毒$allocator@_W@2@@std@@@path_traits@filesystem@boost@@YAXAEBV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@AEAV?$basic_string@_WU?$char_traits@_W@性病病毒$allocator@_W@2@@4@AEBV?$codecvt@_WDH@4@@Z)
1> Main.obj:错误LNK2019:未解析的外部符号“public:static class std::codecvt const&_cdecl boost::filesystem::path::codecvt(void)”(?codecvt@path@filesystem@boost@@萨埃布$codecvt@_WDH@std@@XZ)在函数“public:uu cdecl boost::filesystem::path::path(char const(&)[1],void*)中引用(??$?0$比00)$$CBD@path@filesystem@boost@@QEAA@AEAY00$$CBDPEAX@Z)
1> Main.obj:错误LNK2019:未解析的外部符号“unsigned uu int64 uu cdecl boost::filesystem::detail::file_size(类boost::filesystem::path const&,类boost::system::error_code*)”(?文件_size@detail@filesystem@boost@@嗯_KAEBVpath@23@PEAVerror_code@system@3@@Z)在函数中引用“unsigned uuu int64 uuuu cdecl boost::filesystem::file_size(类boost::filesystem::path const&)”(?file_size@filesystem@助推@@YA_KAEBVpath@12@@Z)
我正在使用Visual Studio,我已经为我的链接器设置了额外的库路径,以包含
“C:\Program Files\Boost\Boost\u 1\u 54\u 0\stage\lib;”
。我还设置了包含路径以查看
C:\Program Files\Boost\Boost\u 1\u 54\u 0;

有人能帮忙吗?当我使用boost::algorithm、boost::string和boost::interprocess时,我的代码构建得很好

编辑:这是我在命令行上完成链接时的样子:

/OUT:“我的文件路径”/MANIFEST/NXCOMPAT/PDB:“我的文件路径” /DYNAMICBASE“kernel32.lib”“user32.lib”“gdi32.lib”“winspool.lib” “comdlg32.lib”“advapi32.lib”“shell32.lib”“ole32.lib”“oleaut32.lib” “uuid.lib”“odbc32.lib”“odbccp32.lib”/DEBUG/MACHINE:X64 /增量/PGD:“我的文件路径”/MANIFESTUAC:“level='asInvoker' uiAccess='false'/ManifestFile:“我的文件路径”/ERRORREPORT:PROMPT /NOLOGO/LIBPATH:“C:\ProgramFiles\Boost\Boost\u 1\u 54\u 0\stage\lib” /投标编号:1


设置链接器的
/VERBOSE
标志(“链接器|常规|显示进度”=“在IDE中显示所有进度消息(/VERBOSE)”),然后查看输出;在IDE中,它将位于名为
.log
的文件的生成输出目录中

如果有正在查找的boost文件系统库,请确保它存在;如果没有正在查找的文件系统库,则必须在链接器输入中显式添加相应的文件系统库,正如Dietmar Kühl在一篇现已删除的帖子中提到的那样


另外,由于看起来您是为x64目标构建的,请确保您没有尝试针对32位boost库进行链接。

您遇到了与中相同的问题,答案是相同的:boost.Filesystem依赖于boost.System,您还需要与后一个库进行链接。

我在中构建了类似的未解决外部问题Visual Studio。

解决方案是将use wchar\t设置为内置。发生此错误的原因有很多。原因通常隐藏在错误后或之前的警告中。实际的错误消息通常没有什么线索。例如,我的场景显示如下:

我使用的是VSColorOutput插件,它以不同的颜色显示错误和警告

warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64'
这基本上意味着我为x32目标编译了Boost库,但project的目标是x64。因此修复方法是删除stage文件夹并运行以下命令:

b2 variant=debug,release link=static runtime-link=shared threading=multi --with-thread address-model=64
如果您没有看到上述警告,那么您可能希望尝试在“项目选项”>“链接器”>“常规”>“显示进度”中打开链接器的详细模式

出现此错误消息的其他一些原因:

  • 您根本没有编译Boost安装,或者没有在“项目选项”>“链接器”>“常规”>“附加库目录”中设置路径。在这种情况下,您可能会收到以下消息:

    致命错误LNK1104:无法打开文件“libboost_system-vc90-mt-gd-1_62.lib”

  • 发生这种情况的另一个原因是,Boost可能已在您的计算机或项目上配置为不使用自动生成的库名称。这称为Boost默认情况下使用的自动链接,这意味着您不需要在项目选项中显式指定.lib文件。相反,Boost自动生成库名称文件并将其添加到链接器。如果关闭此选项,则会出现错误。可以在项目级别使用BOOST\u ALL\u DYN\u LINK宏将其重新打开

这对我来说很有效:在引用其他头文件之前,我将#include boost/filesystemm.hpp放在代码的顶部

Link errors:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <Windows.h>

//quantlib
#include <ql/quantlib.hpp>

//boost
#include <boost/timer.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
---------------------------------------------
No Link errors:
#include <iostream>
#include <boost/filesystem.hpp>
#include <fstream>
#include <stdio.h>
#include <Windows.h>

//quantlib
#include <ql/quantlib.hpp>

//boost
#include <boost/timer.hpp>
#include <boost/lexical_cast.hpp>
链接错误:
#包括
#包括
#包括
#包括
//量子线
#包括
//促进
#包括
#包括
#包括
---------------------------------------------
无链接错误:
#包括
#包括
#包括
#包括
#包括
//量子线
#包括
//促进
#包括
#包括
链接器错误
Link errors:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <Windows.h>

//quantlib
#include <ql/quantlib.hpp>

//boost
#include <boost/timer.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
---------------------------------------------
No Link errors:
#include <iostream>
#include <boost/filesystem.hpp>
#include <fstream>
#include <stdio.h>
#include <Windows.h>

//quantlib
#include <ql/quantlib.hpp>

//boost
#include <boost/timer.hpp>
#include <boost/lexical_cast.hpp>
define BOOST_SYSTEM_NO_DEPRECATED