繁体   English   中英

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

[英]Linker errors when using 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;
}

当我构建时,出现以下错误:

1>Main.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat@system@boost@@YAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'errno_ecat''(void)" (??__Eerrno_ecat@system@boost@@YAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl boost::filesystem::path_traits::convert(char const *,char const *,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &,class std::codecvt<wchar_t,char,int> const &)" (?convert@path_traits@filesystem@boost@@YAXPEBD0AEAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@AEBV?$codecvt@_WDH@5@@Z) referenced in function "void __cdecl boost::filesystem::path_traits::dispatch<class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &,class std::codecvt<wchar_t,char,int> const &)" (??$dispatch@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@path_traits@filesystem@boost@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@4@AEBV?$codecvt@_WDH@4@@Z)
1>Main.obj : error LNK2019: unresolved external symbol "public: static class std::codecvt<wchar_t,char,int> const & __cdecl boost::filesystem::path::codecvt(void)" (?codecvt@path@filesystem@boost@@SAAEBV?$codecvt@_WDH@std@@XZ) referenced in function "public: __cdecl boost::filesystem::path::path<char const [1]>(char const (&)[1],void *)" (??$?0$$BY00$$CBD@path@filesystem@boost@@QEAA@AEAY00$$CBDPEAX@Z)
1>Main.obj : error LNK2019: unresolved external symbol "unsigned __int64 __cdecl boost::filesystem::detail::file_size(class boost::filesystem::path const &,class boost::system::error_code *)" (?file_size@detail@filesystem@boost@@YA_KAEBVpath@23@PEAVerror_code@system@3@@Z) referenced in function "unsigned __int64 __cdecl boost::filesystem::file_size(class boost::filesystem::path const &)" (?file_size@filesystem@boost@@YA_KAEBVpath@12@@Z)

我正在使用Visual Studio,并且为链接器设置了附加的库路径,以使其包括"C:\\Program Files\\Boost\\boost_1_54_0\\stage\\lib;" 我还设置了包含路径,以查看C:\\Program Files\\Boost\\boost_1_54_0;

谁能帮忙吗? 当我使用boost :: algorithm,boost :: string和boost :: interprocess时,我的代码构建良好

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

/ OUT:“ my_file_path” / MANIFEST / NXCOMPAT /PDB:"my_file_path"/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 / INCREMENTAL / PGD:” my_file_path“ / MANIFESTUAC:” level ='asInvoker 'uiAccess ='false'“ / ManifestFile:” my_file_path“ / ERRORREPORT:PROMPT / NOLOGO / LIBPATH:” C:\\ Program Files \\ Boost \\ boost_1_54_0 \\ stage \\ lib“ / TLBID:1

我在Visual Studio中构建了类似的未解决的外部组件。
解决方案是将使用wchar_t设置为内置。

您与MSCV中的Boost Filesystem Library如何解决LNK1104错误中的问题相同。 答案是相同的:Boost.Filesystem依赖Boost.System,您需要另外与后者库链接。

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

如果正在寻找boost文件系统库,请确保它存在; 如果没有人寻找,则必须在链接器输入中显式添加适当的人,如DietmarKühl在现在已删除的帖子中提到的那样。

另外,由于看起来您是在为x64目标构建,因此请确保您未尝试链接到32位Boost库。

有许多原因可能导致此错误。 原因通常隐藏在错误之后或之后的警告中。 实际的错误消息通常很少有提示。 例如,我的场景显示如下:

在此处输入图片说明

我正在使用VSColorOutput插件,该插件以不同的颜色显示错误和警告。 如您所见,提示显示在警告消息中,

warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64'

这基本上意味着我为x32目标编译了Boost库,但是项目针对x64。 因此,解决方法是删除舞台文件夹并运行以下命令:

b2 variant=debug,release link=static runtime-link=shared threading=multi --with-thread address-model=64

如果没有看到上述警告,则可能要尝试在项目选项>链接器>常规>显示进度中为链接器打开VERBOSE模式。

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

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

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

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

这对我有用:在引用其他标头之前,将#include boost / filesytem.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

暂无
暂无

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

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