MongoDB C++分段错误

Nat*_*ehr 4 c++ segmentation-fault mongodb

问题

我试图用C++连接到MongoDB.以下代码实际上编译.但是,当我尝试运行程序时,我遇到了分段错误.

- 编辑 -

这是我在gdb中运行它后得到的结果(源代码或makefile没有变化):

GDB运行:

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
Run Code Online (Sandbox Code Playgroud)

GDB在哪里:

#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 ?? ()
Run Code Online (Sandbox Code Playgroud)

环境

- Ubuntu 12.04 LTS
- MongoDB 2.2.2
- MongoDB C++ Driver 2.2.2
- boost 1.52.0
Run Code Online (Sandbox Code Playgroud)

源代码

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

}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

Art*_*yom 6

上面的代码很好......

您很可能在代码和编译的mongodb库中使用不同版本的boost.

确保:

  • Mongo DB使用与您正在使用的完全相同的boost库和标头版本.
  • 确保他们正在查找完全相同的标题.
  • 确保使用clang编译器编译libmongoclient,并使用clang编译boost库
  • 确保链接正确的增强版本.
  • 确保为所有编译使用相同的C++标志(即C++ 0x之类的东西)

很常见的情况是混合使用不同版本的boost,Boost对标志和代码/编译器的更改非常敏感.

例如,Ubuntu 12.04附带Boost-1.46,因此很可能您可能包含操作系统特定文件而不是您的版本文件,链接等也是如此.您也可能意外地使用或链接OS mongo-db,这是针对boost的编译器-1.46