MongoDB C++ 分段错误 [英] MongoDB C++ Segmentation Fault

查看:29
本文介绍了MongoDB C++ 分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C++ 连接到 MongoDB.下面的代码确实可以编译.但是,当我尝试运行该程序时,出现分段错误.

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.

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

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

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

环境

- 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...

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

It is most likely you are using different version of boost in code and in the compiled mongodb library.

确保:

  • Mongo DB 使用完全与您正在使用的相同版本的 boost 库和标头.
  • 确保他们查看的标题完全相同.
  • 确保 libmongoclient 是用 clang 编译器编译的,boost 库也是用 clang one 编译的
  • 确保链接到正确的 boost 版本.
  • 确保对所有编译使用相同的 C++ 标志(即 C++0x 之类的东西)
  • 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)

混合不同版本的 boost 是很常见的情况,而且 Boost 对标志和代码/编译器的变化非常敏感.

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.

例如 Ubuntu 12.04 带有 Boost-1.46,因此您可能包含特定于操作系统的文件而不是您的版本文件,同样用于链接等.此外,您可能不小心使用或链接了操作系统 mongo-db针对 boost-1.46 的编译器

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

这篇关于MongoDB C++ 分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆