mongo-cxx-driver の例をコンパイルして実行してみます。Boost-system、boost-thread、boost-regex をリンクする必要があります。コマンドラインでコンパイルすると:
g++ simple_client_demo.cpp -I/home/ssi/mongo-client-install/include -L/home/ssi/mongo-client-install/lib -lmongoclient -lboost_system -lboost_thread -lboost_regex
すべては順調です。しかし、Cmake でコンパイルしようとすると、Signal: SIGSEGV (Segmentation fault)
in /usr/include /c++/4.8/functional(_M_manager(_M_functor, _M_functor, __destroy_functor);
問題がある場合は 2030 行) が表示されます。
Boostをリンクする私のCMakeList.txtの一部:
cmake_minimum_required(VERSION 3.2)
project(test_mongo_c_driver)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories("/home/ssi/mongo-client-install/include")
link_directories("/home/ssi/mongo-client-install/lib")
#link_directories("/usr/lib/x86_64-linux-gnu/")
#add_library(mongoclient)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED OFF)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.54.0 REQUIRED COMPONENTS thread system regex)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
set(SOURCE_FILES
simple_client_demo.cpp
# tutor2.cpp
)
add_executable(test_mongo_c_driver ${SOURCE_FILES})
target_link_libraries(test_mongo_c_driver mongoclient ${Boost_LIBRARIES})
ldd a.out
g++
(コマンドからコンパイル):
linux-vdso.so.1 => (0x00007fffd41b5000)
libboost_system.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0 (0x00007f236fda7000)
libboost_thread.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0 (0x00007f236fb91000)
libboost_regex.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0 (0x00007f236f889000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f236f585000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f236f27f000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f236f068000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f236eca3000)
/lib64/ld-linux-x86-64.so.2 (0x00007f236ffd2000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f236ea9b000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f236e87c000)
libicuuc.so.52 => /usr/lib/x86_64-linux-gnu/libicuuc.so.52 (0x00007f236e503000)
libicui18n.so.52 => /usr/lib/x86_64-linux-gnu/libicui18n.so.52 (0x00007f236e0fc000)
libicudata.so.52 => /usr/lib/x86_64-linux-gnu/libicudata.so.52 (0x00007f236c88e000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f236c68a000)
ldd test_mongo_c_driver
(cmake からコンパイル):
linux-vdso.so.1 => (0x00007ffff7163000)
libboost_thread.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0 (0x00007fc61ea28000)
libboost_system.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0 (0x00007fc61e823000)
libboost_regex.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0 (0x00007fc61e51c000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc61e218000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc61df11000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc61dcfb000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc61d936000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc61ec65000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc61d72d000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc61d50f000)
libicuuc.so.52 => /usr/lib/x86_64-linux-gnu/libicuuc.so.52 (0x00007fc61d196000)
libicui18n.so.52 => /usr/lib/x86_64-linux-gnu/libicui18n.so.52 (0x00007fc61cd8e000)
libicudata.so.52 => /usr/lib/x86_64-linux-gnu/libicudata.so.52 (0x00007fc61b521000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc61b31c000)
これは私の simple_client_demo.cpp です:
#include "mongo/client/dbclient.h" // the mongo c++ driver
#include <iostream>
using namespace std;
using namespace mongo;
int main(int argc, char *argv[])
{
if (argc > 2)
{
std::cout << "usage: " << argv[0] << " [MONGODB_URI]" << std::endl;
return EXIT_FAILURE;
}
mongo::client::GlobalInstance instance;
if (!instance.initialized())
{
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::string uri = argc == 2 ? argv[1] : "mongodb://meowtom.ru:27017";
std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid())
{
std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl;
return EXIT_FAILURE;
}
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
if (!conn)
{
cout << "couldn't connect : " << errmsg << endl;
return EXIT_FAILURE;
}
try
{
unsigned long long count = conn->count("test.users");
cout << "count of exiting documents in collection test.foo : " << count << endl;
conn->remove("test.foo", BSONObj());
BSONObj o = BSON("hello" << "world");
conn->insert("test.users", o);
Query query;
BSONObj ret = conn->findOne("test.users", query);
std::cout << ret.toString() << '\n';
string e = conn->getLastError();
if (!e.empty())
{
cout << "insert #1 failed: " << e << endl;
}
// make an index with a unique key constraint
conn->createIndex("test.foo", IndexSpec().addKeys(BSON("hello" << 1)).unique());
try
{
conn->insert("test.foo", o); // will cause a dup key error on "hello" field
}
catch (const OperationException &)
{
// duplicate key error
}
// cout << "we expect a dup key error here:" << endl;
// cout << " " << conn->getLastErrorDetailed().toString() << endl;
}
catch (DBException &e)
{
cout << "caught DBException " << e.toString() << endl;
return 1;
}
return 0;
}
の出力make VERBOSE=1
:
/usr/local/bin/cmake -H/home/ssi/cppproj/test-mongo-c-driver -B/home/ssi/cppproj/test-mongo-c-driver --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/ssi/cppproj/test-mongo-c-driver/CMakeFiles /home/ssi/cppproj/test-mongo-c-driver/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/ssi/cppproj/test-mongo-c-driver'
make -f CMakeFiles/test_mongo_c_driver.dir/build.make CMakeFiles/test_mongo_c_driver.dir/depend
make[2]: Entering directory `/home/ssi/cppproj/test-mongo-c-driver'
cd /home/ssi/cppproj/test-mongo-c-driver && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/ssi/cppproj/test-mongo-c-driver /home/ssi/cppproj/test-mongo-c-driver /home/ssi/cppproj/test-mongo-c-driver /home/ssi/cppproj/test-mongo-c-driver /home/ssi/cppproj/test-mongo-c-driver/CMakeFiles/test_mongo_c_driver.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/ssi/cppproj/test-mongo-c-driver'
make -f CMakeFiles/test_mongo_c_driver.dir/build.make CMakeFiles/test_mongo_c_driver.dir/build
make[2]: Entering directory `/home/ssi/cppproj/test-mongo-c-driver'
make[2]: Nothing to be done for `CMakeFiles/test_mongo_c_driver.dir/build'.
make[2]: Leaving directory `/home/ssi/cppproj/test-mongo-c-driver'
/usr/local/bin/cmake -E cmake_progress_report /home/ssi/cppproj/test-mongo-c-driver/CMakeFiles 1
[100%] Built target test_mongo_c_driver
make[1]: Leaving directory `/home/ssi/cppproj/test-mongo-c-driver'
/usr/local/bin/cmake -E cmake_progress_start /home/ssi/cppproj/test-mongo-c-driver/CMakeFiles 0