我按照指示安装了mongo-c和mongocxx。 pkg-config out:
pkg-config --cflags libmongocxx
-I/usr/local/include/mongocxx/v_noabi -I/usr/local/Cellar/mongo-c-driver/1.9.0/include/libmongoc-1.0 -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/Cellar/mongo-c-driver/1.9.0/include/libbson-1.0
CMake的:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
###MONGODB
include_directories(${libmongocxx_INCLUDE_DIRS})
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories("/usr/local/include/mongocxx/v_noabi")
include_directories("/usr/local/include/bsoncxx/v_noabi")
include_directories("/usr/local/include/libmongoc-1.0")
include_directories("/usr/local/include/libbson-1.0")
include_directories("/usr/local/lib")
find_package(Boost REQUIRED COMPONENTS thread system)
add_executable(1MongoTest 1MongoTest.cpp)
基本代码:
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include <string>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
using namespace std;
int main(int argc, char **argv) {
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
mongocxx::database db = client["mydb"];
mongocxx::collection coll = db["test"];
std::cout << "passed " << endl;
return 0;
我跑步时遇到分段错误。
问题在于:
mongocxx::uri uri("mongodb://localhost:27017");
我做错了什么?我正在遵循官方网站上的确切步骤。?
由于
答案 0 :(得分:1)
我找出了问题..
我正在使用c ++ 7。并且它与此编译/构建不兼容。
当我回到c ++时。一切都很顺利......
这是cmake:
add_executable(1MongoTest 1MongoTest.cpp)
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
target_link_libraries(1MongoTest ${LIBMONGOCXX_LIBRARIES})
target_link_libraries(1MongoTest ${LIBBSONCXX_LIBRARIES})
target_include_directories(1MongoTest PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_include_directories(1MongoTest PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})