前言
filesystem的前身是boost
里的boost.filesystem
。后来被引入C++的TS作为可选支持,命名空间在std::experimental::filesystem
。再后来C++17对其做了一些修改后正式引入标准库,命名空间在std::filesystem
。
但是编译过程中出现了一些问题,就·记录下来。
Bug汇总
Bug:error: ‘filesystem’ has not been declared
问题1:由于之前GCC4.8.5中不存在filesystem
文件,因此需要升级GCC、G++版本。升级过程自行百度。注意:尽量用依赖(命令行)形式进行安装,如果用源码安装的话运行时间会很久。
笔者安装的是gcc version 8.4.0 (Ubuntu 8.4.0-1ubuntu1~18.04)
之后切换GCC、G++版本
#update-alternatives是ubuntu系统中专门维护系统命令链接符的工具>
#通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本。
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.X Y(优先级) --slave /usr/bin/g++ g++ /usr/bin/g++-4.X
选择GCC版本(G++选择类似)
sudo update-alternatives --config gcc
直接输入指定版本的编号即可完成切换。
【参考链接:linux下gcc、g++不同版本的安装和切换】
问题2:但是完成GCC,G++版本切换后仍旧报出刚才的错误。
参考了stackoverflow上面的这篇文章。
https://stackoverflow.com/questions/55474690/stdfilesystem-has-not-been-declared-after-including-experimental-filesystem/55474923#55474923
将源代码中的部分代码进行替换
- #include< filesystem> 替换成#include<experimental/filesystem>
- filesystem替换成experimental::filesystem
之后运行一下,又出现了Bug.`(>﹏<)′
undefined reference to std::experimental::filesystem::v1::current_path[abi:cxx11]
参考了这篇文章后顺利解决
https://stackoverflow.com/questions/33149878/experimentalfilesystem-linker-error
需要添加target_link_libraries(项目名 stdc++fs)
。
我在项目CMakeLists.txt的写法
cmake_minimum_required( VERSION 3.10 )
project( AutoStitch )
find_package( OpenCV REQUIRED )
add_executable( AutoStitch autostitch.cpp )
target_link_libraries(AutoStitch ${OpenCV_LIBS} stdc++fs )
顺利完成make