Boost.Filesystem create_directories mangles directory name

好久不见. 提交于 2019-12-23 20:49:36

问题


I am trying to make a directory using Boost.Filesystem (the directory can be provided by the user, so it may be a path with nested directories; all, some, or none of the directories in that path may exist to start). When I run the program, a directory is created, but it is not what I asked for; the string containing the path appears to be getting mangled. I never get the same result twice, but the name of the directory always starts with a colon.

A minimal example:

#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main(int argc, char* argv[]) {
    fs::path path = "junk/morejunk";
    if (!fs::create_directories(path)) {
        std::cerr << "Failed to create directory " << path << ".\n";
    }
    return(0);
}

Running that, I get directories such as :@K%C5?, :%C0)%E0?, and :%C0%E9%93?.

I had some trouble getting Boost to link correctly, but the above program compiles and runs now. In case it's necessary, some information:
-- I'm using a Mac (OSX 10.9.4)
-- GCC and Boost both installed with MacPorts (Boost with the +gcc49 option)
-- GCC version 4.9.2_1
-- Boost version 1.57.0_1
-- my Makefile looks like

CC = /opt/local/bin/g++
FLAGS = -I/opt/local/include -L/opt/local/lib -lboost_system-mt -lboost_filesystem-mt
driver : driver.cpp
    $(CC) $(FLAGS) -o driver driver.cpp

Any suggestions welcome; it's been a while since I've used C++ much, and I'm not very experienced with Boost.


回答1:


Out on a limb, make sure you save your file as ASCII, latin1 or UTF8.

Otherwise you might have undefined behaviour from incompatible library versions.

You could use DYLD_LIBRARY_PATH to get the preferred libraries for boost (the ones which you link against). See also Is it OK to use DYLD_LIBRARY_PATH on Mac OS X? And, what's the dynamic library search algorithm with it?




回答2:


I had the same problem of mangled directory names. I installed boost using brew install boost and gcc using brew install gcc6.

It turns out that the boost was build with the Apple's version of the GCC compiler, and the source file with the original GCC compiler. When I build your source file with Apple's compiler it does work.

Alternatively, build boost yourself with your compiler of choice.

See also the answer on a related question, https://stackoverflow.com/a/4798180/2535529.



来源:https://stackoverflow.com/questions/29423879/boost-filesystem-create-directories-mangles-directory-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!