Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9251541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:43:38+00:00 2026-06-18T10:43:38+00:00

The Problem I am trying to connect to MongoDB in C++. The following code

  • 0

The Problem

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.

– Edit –

This is what I get after running it in gdb (no change in source code or makefile):

GDB Run:

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

GDB Where:

#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 ?? ()

Environment

- Ubuntu 12.04 LTS
- MongoDB 2.2.2
- MongoDB C++ Driver 2.2.2
- boost 1.52.0

The Source Code

#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;

}

The Makefile

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:43:39+00:00Added an answer on June 18, 2026 at 10:43 am

    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:

    • Mongo DB uses exactly the same version of boost library and headers that you are using.
    • Make sure that they are looking on exactly the same headers.
    • Make sure libmongoclient is compiled with clang compiler and the boost library is compiled with clang one as well
    • Make sure you link with correct boost versions.
    • Make sure you use the same C++ flags for all compilations (i.e. stuff like C++0x)

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem in my JDBC code. I am trying to connect through
im currently trying to connect CakePHP with ExtJs4 but i face the following problem:
I am having a big problem trying to connect to mysql. When I run:
HI Guys, I have following problem. I'm trying to connect from my Iphone app
I'm trying to connect to mongodb in php. There's no problem if mongodb is
I'm trying to connect a Galaxy 2. The problem is, I can't get the
I have a problem trying to turn my python code into an executable using
I have run into a very interesting problem trying to debug my custom PHP
I had a problem when trying to connect and upload a file to an
ive got a problem when trying to connect to a mysql db for blogengine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.