1

I have a big project.

In N.cpp I need to use boost::filesystem::exists(path) to check if the path is valid.

For that, I include <boost/filesystem.hpp>

I get the following error:

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

If I do not include the header file, I get:

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

What is the right way to use boost::filesystem::exists so that the whole thing would compile?

N.cpp

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

.....
Community
  • 1
  • 1
Uylenburgh
  • 1,277
  • 4
  • 20
  • 46
  • You´ll need to show the linker how to find the library, for `boost::filesystem`, only the includes are not enough. I think you are using Windows, right? Try a first read [here](http://www.boost.org/doc/libs/1_55_0/more/getting_started/windows.html). – wesley.mesquita Feb 03 '14 at 14:54
  • @wesley.mesquita The project itself knows where the library is.Boost functions are used all over the place.The problem is with multiple definitions or multiple includes of the same header, I guess. I just don't know how to solve it. – Uylenburgh Feb 03 '14 at 14:58
  • Edit your question with a snippet from your source, may help. Specially your include session. – wesley.mesquita Feb 03 '14 at 15:02
  • Take a look [here](http://stackoverflow.com/questions/19415544/linker-errors-when-attempting-to-link-boostfilesystem-to-debug-msvc11-project), seems to be a duplicated. – wesley.mesquita Feb 03 '14 at 15:31

2 Answers2

5

I solved the problem by adding to N.cpp:

#ifndef BOOST_ALL_DYN_LINK
#   define BOOST_ALL_DYN_LINK
#endif 
#include <boost/filesystem.hpp>
Uylenburgh
  • 1,277
  • 4
  • 20
  • 46
3

If you want to use boost::filesystem you have to include the header file (directly or indirectly via other includes).

The first three errors are linker errors. Some parts of Boost are header-only, i.e. you don't have to compile Boost when you want to use them. Unfortunately filesystem isn't one of them. That means you have to compile Boost like described here. Afterwards you have to tell your linker where it can find the binaries you just created and it should compile well (if you didn't do any other mistakes ;-) ).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexander
  • 250
  • 1
  • 8