【发布时间】:2018-09-03 09:37:11
【问题描述】:
我有一个 C++ 程序,我需要当前路径来稍后创建一个文件夹。我的可执行文件的位置是/home/me/foo/bin。这是我运行的:
//Here I expect '/home/me/foo/bin/', but get ''
auto currentPath = boost::filesystem::current_path();
//Here I expect '/home/me/foo/', but get ''
auto parentPath = currentPath.parent_path();
//Here I expect '/home/me/foo/foo2/', but get 'foo2/'
string subFolder = "foo2";
string folderPath = parentPath.string() + "/" + subFolder + "/";
//Here I expect to create '/home/me/foo/foo2/', but get a core dump
boost::filesystem::path boostPath{ folderPath};
boost::filesystem::create_directories( boostPath);
我在 Ubuntu 16.04 上运行,使用与包管理器 Conan 一起安装的 Boost 1.66。
我曾经在不使用柯南的情况下使用以前版本的 Boost(我相信是 1.45)成功运行此程序。 Boost 通常安装在我的机器上。我现在在运行 create_directories( boostPath); 时得到一个核心转储。
两个问题:
- 为什么
current_path()不为我提供实际路径,而是返回和空路径? - 即使 current_path() 什么也没返回,为什么即使我使用
sudo运行它,我仍然会有核心转储?我不是简单地在根目录下创建文件夹吗?
编辑:
运行编译的程序,在两行之间有一些上述变量的cout 输出而不是使用调试模式,通常给我以下输出:
currentPath: ""
parentPath: ""
folderPath: /foo2/
Segmentation fault (core dumped)
但有时(大约 20% 的时间)会给我以下输出:
currentPath: "/"
parentPath: "/home/me/fooA�[oFw�[oFw@"
folderPath: /home/me/fooA�[oFw�[oFw@/foo2/
terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
what(): boost::filesystem::create_directories: Invalid argument
Aborted (core dumped)
编辑 2:
运行conan profile show default 我明白了:
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=5
compiler.libcxx=libstdc++
build_type=Release
[options]
[build_requires]
[env]
【问题讨论】:
-
您确定您没有任何访问(权限)问题吗?
-
@A.Gille,我在使用 VS Code 进行调试时确实有这样的担忧,并使用
sudo run排除它,并在两行之间添加一些cout以在控制台上打印路径。结果是一样的。 -
sudo run是指使用sudo运行编译后的可执行文件吗?尝试直接调用getcwd()并检查errno看看可能出了什么问题。 -
请确保您的
conan install设置与您的构建设置一致。例如,出于向后兼容的原因,默认配置文件(.conan/profiles/defaul 或conan profile show default)使用libcxx=libstdc++。但是您的编译器可能默认使用libcxx=libstdc++11。通常这意味着链接错误,而不是运行时,但请检查以防万一。 -
@drodri,成功了!如果您将在答案中详细说明并添加一些解释性行,我将很乐意接受它:)
标签: c++11 boost path boost-filesystem conan