简体   繁体   English

不同编译器中的std :: filesystem和std :: experimental :: filesystem问题

[英]std::filesystem and std::experimental::filesystem problem in different compilers

I am writing a library (just for learning) which utilizes std::filesystem. 我正在写一个利用std :: filesystem的库(仅供学习)。 It works fine on MSVC however by default LTS releases of Linux like Ubuntu ships with GCC 6.x and clang in the official repository is 3.8 which doesn't have std::filesystem instead I have to use std::experimental::filesystem. 它可以在MSVC上正常工作,但是默认情况下,Linux的LTS发行版(如Ubuntu附带GCC 6.x)和官方存储库中的clang是3.8,它没有std :: filesystem,而我必须使用std :: experimental :: filesystem。 How may I workaround this problem so that I can support GCC 6, GCC 8+ (where std::filesystem works), Clang 3.8, latest Clang and MSVC? 我如何解决此问题,以便可以支持GCC 6,GCC 8+(其中std :: filesystem工作),Clang 3.8,最新的Clang和MSVC? I am using CMAKE as my build system 我正在使用CMAKE作为构建系统

Conditional compilation may help: 有条件的编译可能有助于:

#if(defined(_MSC_VER) or (defined(__GNUC__) and (7 <= __GNUC_MAJOR__)))
using n_fs = ::std::filesystem;
#else
using n_fs = ::std::experimental::filesystem;    
#endif

"How may I workaround this problem" - by installing a newer compiler/standard library. “如何解决此问题”-通过安装更新​​的编译器/标准库。 Or by not using std::filesystem . 或者不使用std::filesystem

For example, on RedHat and CentOS you can install devtoolset-8 to get access to a newer (and supported) compiler than what the base system provides. 例如,在RedHat和CentOS上,您可以安装devtoolset-8来访问比基本系统提供的更新(受支持)的编译器。 Other Linux distributions may have similar options. 其他Linux发行版可能具有类似的选项。

You can also compile a newer compiler + std lib yourself from source. 您也可以自己从源代码编译较新的编译器+ std lib。 That's in itself not too difficult, but you need to be careful to then also ship the new libraries. 本身并不太困难,但是您还需要小心,然后再交付新库。 And testing compatibility with the base distro libraries etc is on you . 和测试与基发行库等的兼容性是

You can also, as per @VTT's answer, use conditional compilation to fall back to std::experimental::filesystem if that's all your toolchain supports and you are ok with that. 您也可以按照@VTT的回答,如果条件是您的工具链支持的全部内容,那么可以使用条件编译回退到std::experimental::filesystem

Of you don't want to change compiler and using std::experimental::filesystem is not ok (or your compiler/standard library doesn't even support that), then your only option is to use OS native functions. 您不想更改编译器并且使用std::experimental::filesystem不合适(或者您的编译器/标准库甚至不支持该功能),那么您唯一的选择是使用OS本机功能。

I fixed my problem in the following way: 我通过以下方式解决了问题:

I added the following bunch of code in the project's CMakeLists.txt file 我在项目的CMakeLists.txt文件中添加了以下代码

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.4)
        message(FATAL_ERROR "You are on an extremely old version of GCC. Please update your compiler to at least GCC 5.4, preferably latest")
    elseif (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
        message(WARNING "Old Verison of GCC detected. Using Legacy C++ support")
        target_link_libraries(${PROJECT_NAME} -lstdc++fs)
        target_compile_definitions(${PROJECT_NAME} PUBLIC LEGACY_CXX)
    endif()
endif()

And in my CPP file, I did this: 在我的CPP文件中,我这样做是:

#ifdef LEGACY_CXX
#include <experimental/filesystem>
namespace n_fs = ::std::experimental::filesystem;
#else
#include <filesystem>
namespace n_fs = ::std::filesystem;
#endif

Here's the commit if anyone want to refer to. 如果有人要引用, 这里是提交。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪些编译器支持std :: filesystem? - Which compilers support std::filesystem? std::filesystem 和 std::experimental::filesystem 之间的路径连接差异 - Path concat difference between std::filesystem and std::experimental::filesystem 防止用户将目录保留在std :: experimental :: filesystem中 - Preventing user to leave directory in std::experimental::filesystem 使用带有 g++ 9.3.0 的 std::experimental::filesystem - Using std::experimental::filesystem with g++ 9.3.0 包含后未声明“std::filesystem”<experimental filesystem></experimental> - 'std::filesystem' has not been declared after including <experimental/filesystem> C++ VS 错误:<experimental/filesystem> 提供 std::experimental::filesystem 的标头已被 Microsoft 弃用,并将被删除 - C++ an VS error: <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft and will be REMOVED "c++14 std::experimental::filesystem::v1 和 c++17 std::filesystem 之间的区别?" - Differences between c++14 std::experimental::filesystem::v1 and c++17 std::filesystem? 为什么std :: experimental :: filesystem :: path不接受“ *”之类的通配符? - Why does std::experimental::filesystem::path not accept wildcards like “*”? std :: experimental :: filesystem :: perm_options尚未声明 - std::experimental::filesystem::perm_options has not been declared Visual Studio 2017 std :: experimental :: filesystem :: path中的UTF-8支持 - UTF-8 support in Visual Studio 2017 std::experimental::filesystem::path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM