2017-08-31 112 views
0

我有一行代码,我包含boost-filesystem 1.64库,我很想删除,所以我可以删除依赖Boost完全从我的程序。删除依赖boost :: filesystem :: current_path()

行本身:

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

我在寻找一个替代品,给了我一个std::string currentPath,与编译器的Visual C++LLVM在Windows和Linux的作品。如果可能的话也用于GCC。

这可能是真的,我还没有看够难,但我仍然在学习C++并且实际上对标准库没有太多了解。所以我在问这个问题。

目前的解决办法是依靠:

std::experimental::filesystem::current_path(); 
+2

你应该考虑升级到C++ 17,所以你可以使用['标准:: filesystem :: current_path()'](http://en.cppreference.com/w/cpp/experimental/fs/current_path),它被添加到标准库中。 – tambre

+2

@tambre:C++ 17尚不存在,许多编译器不完全支持FileSystem API的C++ 17版本。这可能在一两年内可行。 –

+1

依赖并不是一件坏事。 Boost是相当模块化的,所以你不会仅仅为了获得文件系统功能而包括整个boost。他们可能会做得更好,确保它可以在Windows和Linux上工作,而不是另一种选择,并且很容易迁移到C++ 17。 – wally

回答

1

我建议你把source code from Boost,适应/ decrustify它,继续前进。这只不过是getcwd()(POSIX)和GetCurrentDirectoryW(Windows)的封装。当std::filesystem::current_path()变得广泛可用时,您可以稍后将其丢弃。

如果你想知道BOOST_POSIX_API如何设置(在方法中引用),看看这个片断:

# if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin 
# define BOOST_WINDOWS_API 
# else 
# define BOOST_POSIX_API 
# endif 
+0

这似乎是最坚固的方法。我要去做这个。 也因为@stanthomas所作的评论。 我见过其他的解决方案''getcwd()'这看起来不便携,而且对我来说还挺好的。 那时我相信'boost :: filesystem'具有最强劲的黑客功能,将它包装成一个漂亮的字符串。 – blipman17