boost :: filesystem的分段错误 [英] Segmentation fault with boost::filesystem
问题描述
我刚刚在Mac上安装了Boost.(已安装MacPorts,sudo端口安装增强功能)在XCode中,我添加了标题搜索路径(/opt/local/include)和库搜索路径(/opt/local/lib),并将库添加到了Build Phases-Link Binary With Libraries(libboost_filesystem-mt.a,libboost_filesystem-mt.dylib),libboost_system-mt.a,libboost_system-mt.dylib).现在,我尝试构建并运行此代码
#include< iostream>#include< string>#include< boost/filesystem.hpp>int main() {std :: string文件名;std :: cin>>文件名;std :: cout<<boost :: filesystem :: exists(filename);返回0;}
在键入任何路径的情况下,我都遇到了Segmentation Fault:在调用exist()时为11.
我做错了什么?安装boost时有任何错误吗?
过去,在没有使用与您的程序相同的 CXXFLAGS
构建boost的情况下,我也遇到过类似的问题.这是一组伪完整的引导程序指令.
<代码>#配置,构建和安装boost./bootstrap.sh \--prefix = $ {PWD}/.local \--with-libraries = ...,文件系统,..../b2 \-q \-d2 \-j4 \--debug-configuration \--disable-filesystem2 \--layout =已标记\--build-dir = $ {PWD}/obj \cxxflags =-v -std = c ++ 11 -stdlib = libc ++" \linkflags =-stdlib = libc ++" \链接=共享\线程=多\安装
其中的重要部分是 cxxflags
和 linkflags
.以我的经验,这是最常见的,因为macports无需使用 -stdlib = libc ++
即可进行编译,但这在使用 -std = c ++11
.常见的症状包括gdb中的随机回溯,这表明在深埋在boost库/模板深处的特定结构内的指针存在问题.
从以上内容可以看出,我将boost的本地副本构建到每个项目的目录中(例如 $ {PWD}/.local
),然后在运行期间针对本地版本进行链接开发,直到打包为止(此时我将静态链接或执行其他操作).
#在GNUmakefile中LOCAL_DIR = $ {PWD}/.localINC_DIR = $ {LOCAL_DIR}/includeLIB_DIR = $ {LOCAL_DIR}/libCPPFLAGS=-I"${INC_DIR}"CXXFLAGS = -std = c ++ 11 -stdlib = libc ++LDFLAGS = -stdlib = libc ++ -L"$ {LIB_DIR}"MYPROG_SRCS = myprog.cppMYPROG_OBJS = $(MYPROG_SRCS:.cpp = .o)%.o:%.cpp%.hpp$ {CXX} $ {CXXFLAGS} $ {CPPFLAGS} -c -o $ @ $<myprog:$ {MYPROG_OBJS}$ {CXX} $ {LDFLAGS} -o $ @ $ ^ $ {LIBS}
底线:您的 CPPFLAGS
和 LDFLAGS
必须在boost和程序之间匹配.
I just installed boost on my mac. (Installed MacPorts, sudo port install boost) In XCode I added Header Search Path (/opt/local/include) and Library Search Path (/opt/local/lib) and added libraries into Build Phases - Link Binary With Libraries (libboost_filesystem-mt.a, libboost_filesystem-mt.dylib, libboost_system-mt.a, libboost_system-mt.dylib). Now I trying to build and run this code
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
int main() {
std::string filename;
std::cin >> filename;
std::cout << boost::filesystem::exists(filename);
return 0;
}
And with any path typed I got Segmentation Fault: 11 when calling exists().
What i did wrong? Is any mistakes when installing boost?
I've run in to similar problems in the past when boost isn't built with the same CXXFLAGS
as your program. Here's a pseudo-complete set of bootstrap instructions.
# Configure, build, and install boost
./bootstrap.sh \
--prefix=${PWD}/.local \
--with-libraries=...,filesystem,...
./b2 \
-q \
-d2 \
-j4 \
--debug-configuration \
--disable-filesystem2 \
--layout=tagged \
--build-dir=${PWD}/obj \
cxxflags="-v -std=c++11 -stdlib=libc++" \
linkflags="-stdlib=libc++" \
link=shared \
threading=multi \
install
The important part there is the cxxflags
and linkflags
. In my experience, it's most often because macports compiles without using -stdlib=libc++
but that's required when using compiling C++11
code using -std=c++11
. Common symptoms include random backtraces in gdb that indicate something is a problem with a pointer inside of a particular struct buried deep within a boost library/template.
As you can tell from the above, I build a local copy of boost in to a per-project directory (e.g. ${PWD}/.local
) and then link against the local version during development until it's time to package (at which point I statically link or do something else).
# In a GNUmakefile
LOCAL_DIR=${PWD}/.local
INC_DIR=${LOCAL_DIR}/include
LIB_DIR=${LOCAL_DIR}/lib
CPPFLAGS=-I"${INC_DIR}"
CXXFLAGS=-std=c++11 -stdlib=libc++
LDFLAGS=-stdlib=libc++ -L"${LIB_DIR}"
MYPROG_SRCS=myprog.cpp
MYPROG_OBJS=$(MYPROG_SRCS:.cpp=.o)
%.o : %.cpp %.hpp
${CXX} ${CXXFLAGS} ${CPPFLAGS} -c -o $@ $<
myprog: ${MYPROG_OBJS}
${CXX} ${LDFLAGS} -o $@ $^ ${LIBS}
Bottom line: your CPPFLAGS
and LDFLAGS
need to match between boost and your program.
这篇关于boost :: filesystem的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!