2017-08-31 134 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