使用< filesystem>的链接错误; C ++ 17中的成员 [英] Link errors using <filesystem> members in C++17

查看:352
本文介绍了使用< filesystem>的链接错误; C ++ 17中的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu 16.04上使用gcc 7.2,我需要使用C ++ 17中的新文件系统库.即使确实存在一个称为实验/文件系统"的库,我也无法使用其任何成员.例如,当我尝试编译该文件时:

I'm using gcc 7.2 on Ubuntu 16.04, and I need to use the new filesystem library from C++17. Even though there is indeed a library called experimental/filesystem, I can't use any of its members. For example, when I try to compile this file:

#include <iostream>
#include <string>
#include <experimental/filesystem>
using namespace std;
namespace fs = std::experimental::filesystem::v1;

int main(){
    fs::path p1 = "/usr/share/";
}

我收到一个看起来像这样的编译错误:

I get a compilation error which looks like this:

$ g++-7 test.cpp -std=c++17
/tmp/ccfsMnlG.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [12], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [12])':
test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA12_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA12_cS3_EERKT_]+0x73): undefined reference to `st
d::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status

我做错了什么?我认为代码没有任何问题,因为我只是从网站上复制粘贴了代码.我使用的是错误版本的gcc吗?另外,为什么在C ++ 17中为什么需要<experimental/filesystem>而不是仅仅<filesystem>?预先感谢.

What am I doing wrong? I don't think there's anything wrong with the code, since I just copy-pasted it from a website. Am I using the wrong version of gcc? Also, why do I need <experimental/filesystem> instead of just <filesystem> in C++17? Thanks in advance.

推荐答案

添加标志-lstdc++fs:

$ g++-7 test.cpp -std=c++17 -lstdc++fs

gcc 7.2仅支持C ++ 17实验性filesystem命名空间.我不知道,也许gcc 7.3已经支持std filesystem名称空间.

gcc 7.2 supports C++17 experimental filesystem namespace only. I do not know, maybe gcc 7.3 supports std filesystem namespace already.

这篇关于使用&lt; filesystem&gt;的链接错误; C ++ 17中的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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