编译filesystem时出现错误fatal error: filesystem: No such file or directory
据说gcc8可以解决,但gcc --version发现我的版本是7.5
解决方法如下:
1.头文件修改
#include <filesystem>改为#include <experimental/filesystem>
#include <experimental/filesystem>
如果编译出现error: 'std::filesystem' has not been declared,看步骤2
2.命名空间修改
namespace fs = std::filesystem改为namespace fs = std::experimental::filesystem;
namespace fs = std::experimental::filesystem;
3.cmakefile修改|编译添加依赖库stdc++fs
target_link_libraries(xxx_xx
${catkin_LIBRARIES}
stdc++fs --添加
)
target_link_libraries(xxx_xx
${catkin_LIBRARIES}
stdc++fs
)
再编译后解决。
使用如下:
fs::path p(obj_path);
if (!fs::exists(p)) {
std::cout << obj_path << " not exists!" << std::endl;
bool c_ok = fs::create_directories(obj_path);
if (c_ok) std::cout << obj_path << " created!" << std::endl;
}
参考1:ubuntu(21):fatal error: filesystem: No such file or directory,fatal error: filesystem: 没有那个文件或目录-CSDN博客参考2:fatal error: filesystem: 没有那个文件或目录_fatal error: filesystem: no such file or directory-CSDN博客