问题
我正在尝试用 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
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)