且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

boost :: filesystem的分段错误

更新时间:2023-11-11 23:28:34

过去,在没有使用与您的程序相同的 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.