Linker errors when using boost::filesystem?

2019-06-16 05:54发布

I have the following code:

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

int main(){
    const char* file_path = "my_path";
    std::cout << boost::filesystem::file_size(file_path) << std::endl;
}

and when I build I get the following errors:

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)

I am using Visual Studio and I have set the additional library path for my linker to include "C:\Program Files\Boost\boost_1_54_0\stage\lib;". I also have set the include path to look at C:\Program Files\Boost\boost_1_54_0;

Can anyone please help? My code builds fine when I use boost::algorithm, boost::string and boost::interprocess.

EDIT: this is what my linking if done on the command-line looks like:

/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

6条回答
淡お忘
2楼-- · 2019-06-16 06:31

I had similar unresolved externals building in Visual Studio.
The solution was to set use wchar_t to be treated as built-in.

查看更多
Ridiculous、
3楼-- · 2019-06-16 06:34

This worked for me: I put #include boost/filesytem.hpp at the top of my code, before referencing other headers.

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>
查看更多
看我几分像从前
4楼-- · 2019-06-16 06:36

You have the same issue as in How do I resolve LNK1104 error with Boost Filesystem Library in MSCV? and the answer is the same: Boost.Filesystem depends upon Boost.System and you need to link additionally with the latter library.

查看更多
淡お忘
5楼-- · 2019-06-16 06:36

Set the linker's /VERBOSE flag ("Linker | General | Show Progress" = "Display all progress messages (/VERBOSE)" in the IDE). Then look at the output; in the IDE it'll be in the build output directory in a file called <project-name>.log.

If there's a boost filesystem library being looked for, make sure it exists; if there isn't one being looked for then you'll have to add the appropriate one explicitly in the linker inputs as Dietmar Kühl mentioned in a now-deleted post.

Also, since it looks like you're building for an x64 target, make sure you're not trying link against 32-bit boost libraries.

查看更多
该账号已被封号
6楼-- · 2019-06-16 06:40

There are many reasons that this error might occur. The reason is often hidden in the warnings that follows or precedes after the error. Actual error message often has little clue. For example, my scenario shows something like below:

enter image description here

I'm using VSColorOutput plugin that colors errors and warnings differently. As you can see, the hint is shown in warning message that says,

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

This basically means I compiled Boost libraries for x32 target but project is targeting x64. So the fix was to delete the stage folder and run following command:

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

If you are not seeing warnings like above then you might want to try on turning on VERBOSE mode for Linker in project options > Linker > General > Show Progress.

Some other reasons this error message might appear:

  • You haven't compiled your Boost installation at all or not set the path in project options > Linker > General > Additional Library Directories. In this case you might get message:

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

  • Yet another reason this might happen is that Boost might have been configured on your machine or project to not to use auto-generated library names. This is called auto-linking that Boost does by default and that means you don't need to specify actually .lib files explicitly in project options. Instead, Boost auto generates the name of lib file and adds it to linker. If this is turned off, you will get error. It can be turned back on using BOOST_ALL_DYN_LINK macro at project level.

查看更多
forever°为你锁心
7楼-- · 2019-06-16 06:41

The linker error was resolved by adding the following before the header includes:

define BOOST_SYSTEM_NO_DEPRECATED
查看更多
登录 后发表回答