0

I have a single line of code for which I include the boost-filesystem 1.64 library which I'd love to remove, so I can remove the dependency on Boost altogether from my program.

The line itself:

std::string currentPath = boost::filesystem::current_path().string();

I'm searching for a replacement that gives me a std::string currentPath that works on Windows and Linux with the compilers Visual C++ and LLVM. If possible also for GCC.

It's probably true that I haven't looked hard enough, but I am still learning C++ and don't actually know a lot about the standard library. Therefore I'm asking this question.

The current solution is to rely on:

std::experimental::filesystem::current_path();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
blipman17
  • 523
  • 3
  • 23
  • 2
    You should consider upgrading to C++17, so you could use [`std::filesystem::current_path()`](http://en.cppreference.com/w/cpp/experimental/fs/current_path), which was added to the standard library. – tambre Aug 31 '17 at 18:57
  • 2
    @tambre: C++17 does not exist yet, and many compilers do not fully support the C++17 version of the FileSystem API. This might be viable in a year or two. – Nicol Bolas Aug 31 '17 at 18:59
  • 1
    The dependency is not a bad thing. Boost is fairly modular, so you're not including the whole of boost just to get the filesystem functions. They probably do a better job of making sure it works with Windows and Linux than another option would do and it is easy to migrate to C++17. – wally Aug 31 '17 at 19:01
  • I won't be upgrading to c++ 17 anytime soon. I did however found std::experimental::filesystem::current_path(). I haven't decided what I'm gonna do with that though. – blipman17 Aug 31 '17 at 19:01
  • @rex I'm okay with including boost, it's just a hastle to set up with compilers and everything. I might do it later, but not now. I just want to write my program right now. – blipman17 Aug 31 '17 at 19:02
  • @NicolBolas It's on the final ballot, so it's basically set in stone. Both MSVC and GCC implement it under the experimental namespace, but both work fairly well (albeit GCC requires an additional linker flag to enable it). – tambre Aug 31 '17 at 19:02
  • I've used wxStandardPaths for this kind of work, you might be able to include a lean wxCore dependency only. –  Aug 31 '17 at 19:02
  • 1
    How about using the command line arguments for a quick fix? – wally Aug 31 '17 at 19:04
  • I'm more interested in LLVM than in GCC since it looks easyer to verify if everything is working correctly if you can compile on windows and linux with thesame compiler. I might go with the std::experimental::filesystem::current_path() approach. – blipman17 Aug 31 '17 at 19:04
  • @rex ehh... I don't know. I plan for this to run as a daemon/service. – blipman17 Aug 31 '17 at 19:06
  • 1
    You can add namespace fs = boost::filesystem; or fs=std::experimental::filesystem; or namespace fs = std::tr2::sys; based on your compiler and rest of the code will the same like fs::path directory = sdir; – Artemy Vysotsky Aug 31 '17 at 19:29
  • Comment from Stephen Lavavej (MSFT) : "Filesystem is tricky – ... we will not be able to ship it in 2017 Updates" - here https://blogs.msdn.microsoft.com/vcblog/2017/03/07/visual-studio-2017-for-c-developers-you-will-love-it/ So, for Visual C++, std::filesystem is some way off and Boost is the preferred option today. – stanthomas Aug 31 '17 at 22:09

1 Answers1

1

I suggest you take the source code from Boost, adapt/decrustify it and move on. It is not much more than a wrapper around getcwd() (POSIX) and GetCurrentDirectoryW (Windows). You can throw it out later when std::filesystem::current_path() becomes broadly available.

If you wonder how BOOST_POSIX_API is set (referenced in the method), have a look at this snippet:

# if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
#   define BOOST_WINDOWS_API
# else
#   define BOOST_POSIX_API 
# endif
ypnos
  • 50,202
  • 14
  • 95
  • 141
  • That seems like the sturdiest approach. I'm going for this one. Also because of the remark @stanthomas made. I've seen other solutions with `getcwd()` which didn't look portable and kinda hacky to me. At that moment I trust `boost::filesystem` to have the sturdiest hack that wraps it into a nice string. – blipman17 Sep 01 '17 at 09:22