文件系统库的编译器错误:clang和g ++ [英] Compiler error with filesystem library: clang and g++

查看:56
本文介绍了文件系统库的编译器错误:clang和g ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用c ++编写一个个人项目,该项目需要访问某些目录中的文件,因此我决定使用 filesystem 库.当我尝试在MacOS和Linux上编译项目时遇到了一些问题.

I am writing a personal project in c++ which needs to access to files in some directories, hence I decided to use the filesystem library. I encountered some problems when I try to compile my project on MacOS and on Linux.

下面的代码段

#include <iostream>
#include <fstream>

int main(){

    std::string path = "Inner";

    std::cout << "Files in " << path << " directory :" << std::endl;

    for (const auto & entry : std::filesystem::directory_iterator(path))
        std::cout << entry.path() << std::endl;


    return 0;

}

当我在MacBook Pro(clang版本11.0.3(clang-1103.0.32.62))上使用

When I compile it on my MacBook Pro (clang version 11.0.3 (clang-1103.0.32.62)) with

g ++ -o test test.cpp -std = c ++ 17 -Wall

一切正常.但是,一旦我转向Linux(Ubuntu 19.04,g ++ 8.3.0),就会收到以下错误消息:

everything works fine. But as soon as I move to Linux (Ubuntu 19.04, g++ 8.3.0) I get the following error:

test.cpp: In function ‘int main()’:
test.cpp:8:33: error: ‘std::filesystem’ has not been declared
  for (const auto & entry : std::filesystem::directory_iterator(path)){

然后我将文件系统库包含在 #include< filesystem> 中:

I include then the filesystem library with #include <filesystem>:

#include <iostream>
#include <fstream>
#include <filesystem>

int main(){

    std::string path = "Inner";

    std::cout << "Files in " << path << " directory :" << std::endl;

    for (const auto & entry : std::filesystem::directory_iterator(path))
        std::cout << entry.path() << std::endl;

    return 0;

}

通过 g ++ -o test test.cpp -std = c ++ 17 -Wall -lstdc ++ fs 进行编译,在Linux上也一切正常(请注意,我必须添加 -lstdc ++ fs ).

compile it via g++ -o test test.cpp -std=c++17 -Wall -lstdc++fs and everything works fine on Linux too (note that I had to add -lstdc++fs).

为什么在MacOS和Linux上会有这种不同的行为?是否取决于编译器?Windows操作系统(我家里没有Windows PC)会发生什么?

Why is there this different behaviour on MacOS and on Linux? Does it depends on the compiler? What happens with Windows OS (I do not have any Windows PC at home)?

我在此处找到了一个相关问题及其答案,但似乎并不能解释第一种情况的原因(使用clang),即使不包含 filesystem 库,一切也可以正常工作.

I found a related question and its answer here, but it does not seem to explain why in the first case (with clang) everything works fine also without including filesystem library.

推荐答案

  1. 使用"g ++"未使用clang,则应使用"clang ++"

  1. Using 'g++' is not using clang you should use 'clang++'

Gcc不应依赖于平台,但可能是不同的版本

Gcc should not be platform dependent but it might be different version

在任何情况下,您都应明确包含所需的头文件,并且std :: filesystem是在< filesystem>"

At any case, you should explicitly include header files needed, and std::filesystem is defined in "<filesystem>"

关于需要添加"lstdc ++ fs"的提示-这暗示着g ++版本实际上是不同的,并且使用了不同的llvm版本.如 https://en.cppreference.com/w/cpp/filesystem 中所述

regarding the need to add "lstdc++fs' - this is a hint that actually g++ version is different and uses different llvm versions. As described in https://en.cppreference.com/w/cpp/filesystem

注释:使用此库可能需要其他编译器/链接器选项.9.1之前的GNU实现需要与 -lstdc ++ fs 链接,而LLVM 9.0之前的LLVM实现需要与-lc ++ fs链接

Notes: Using this library may require additional compiler/linker options. GNU implementation prior to 9.1 requires linking with -lstdc++fs and LLVM implementation prior to LLVM 9.0 requires linking with -lc++fs

这篇关于文件系统库的编译器错误:clang和g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆