I am trying to connect to MongoDB in C++. The following code does actually compile. However, when I try to run the program, I get a segmentation fault.
This is what I get after running it in gdb (no change in source code or makefile):
Starting program: /home/nathanw/devel/Linux/mkfarina-cpp/mkfarina 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff69ae700 (LWP 13314)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff69ae700 (LWP 13314)]
0x00007ffff6d79034 in pthread_mutex_unlock () from /lib/x86_64-linux-gnu/libpthread.so.0
#0  0x00007ffff6d79034 in pthread_mutex_unlock () from /lib/x86_64-linux-gnu/libpthread.so.0
#1  0x00007ffff7bca948 in boost::detail::thread_data_base::~thread_data_base() () from /usr/local/lib/libboost_thread.so.1.52.0
#2  0x000000000046c74b in boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<void, mongo::BackgroundJob, boost::shared_ptr<mongo::BackgroundJob::JobStatus> >, boost::_bi::list2<boost::_bi::value<mongo::BackgroundJob*>, boost::_bi::value<boost::shared_ptr<mongo::BackgroundJob::JobStatus> > > > >::~thread_data() ()
#3  0x00007ffff7bc7d39 in thread_proxy () from /usr/local/lib/libboost_thread.so.1.52.0
#4  0x00007ffff6d75e9a in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0
#5  0x00007ffff6aa2cbd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#6  0x0000000000000000 in ?? ()
- Ubuntu 12.04 LTS
- MongoDB 2.2.2
- MongoDB C++ Driver 2.2.2
- boost 1.52.0
#include <cstdlib>
#include <iostream>
// #include <cppcms/application.h>
// #include <cppcms/applications_pool.h>
// #include <cppcms/service.h>
// #include <cppcms/http_response.h>
#include "mongo/client/dbclient.h"
int main( int argc, char** argv ){
    try {
        mongo::DBClientConnection c;
        c.connect( "localhost" );
        std::cout << "connected ok" << std::endl;
    } catch( const mongo::DBException& e ){
        std::cout << "caught " << e.what() << std::endl;
    }
    return 0;
}
CXX = clang++
TARGET = mkfarina
FLAGS = -c -v -00
LIBRARIES = \
    -lbooster \
    -pthread \
    -lmongoclient \
    -lcppcms \
    -lboost_thread \
    -lboost_filesystem \
    -lboost_program_options \
    -lboost_system \
INCLUDE_PATHS = \
    -I/usr/local/include \
    -I/usr/local/include/boost \
    -I/usr/local/include/mongo \
    -I/usr/local/include/cppcms \
    -I/home/nathanw/devel/_include \
LIBRARY_PATHS = \
    -L/usr/lib \
    -L/usr/local/lib \
SOURCES = \
    main.cpp \
OBJECTS = $(SOURCES:.cpp=.o)
$(TARGET): $(OBJECTS)
    $(CXX) $(OBJECTS) $(LIBRARY_PATHS) $(LIBRARIES) -o $(TARGET)
%.o: %.cpp
    $(CXX) $(FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(LIBRARIES) $< -o $@
clean:
    rm -f $(TARGET) $(OBJECTS)
                The code above works fine...
It is most likely you are using different version of boost in code and in the compiled mongodb library.
Make sure:
It is very common case that you mix up different versions of boost also Boost is very sensitive to changes in flags and code/compiler.
For example Ubuntu 12.04 comes with Boost-1.46, so it is likely that you may include the OS specific files and not your version files, same for linking etc. Also you may accidentally use or link with OS mongo-db that was compiler against boost-1.46
This error can happen if you are on OS X 10.9 Mavericks and mongo is using a different implementation of the C++ standard library than boost (libstdc++ vs libc++). To prevent this, when you build the C++ driver with scons be sure to pass:
--osx-version-min=10.9
An example build command might be:
scons --prefix=$HOME/mongo-client-install --extrapath=/usr/local/Cellar/boost/1.55.0_2 install-mongoclient --osx-version-min=10.9
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