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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With