[C++] Corrupted output using MongoDB C++ driver

265 views
Skip to first unread message

Guillaume

unread,
Jul 6, 2017, 6:17:16 PM7/6/17
to mongodb-user


I am trying to install and use MongoDB C++ driver on Windows 10 Pro.

I use Boost v. 1.64.0 downloaded from the official website.

First, I have installed the MongoDB C driver v. 1.6.3 following the official instructions.

Then, I have installed the MongoDB C++ driver v. r3.1.1 following the official instructions and using the following cmake command:

cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DCMAKE_PREFIX_PATH=c:\mongo-c-driver -DBOOST_ROOT=C:\boost_1_64_0 -DBSONCXX_POLY_USE_BOOST=1 -DLIBBSON_DIR=C:\mongo-c-driver -DLIBMONGOC_DIR=C:\mongo-c-driver


With Microsoft Visual Studio Community 2017 v. 15.2 Release, I have entered the configuration properties shared here:

Left click over your project item in Solution Explorer, and choose Properties menu;
On VC++ Directory page, add this entries into Include Directories list:
  • $(ProjectDir)third-party\boost_1_56_0
  • $(ProjectDir)third-party\mongo-cxx-driver\include\mongocxx\v_noabi
  • $(ProjectDir)third-party\mongo-cxx-driver\include\bsoncxx\v_noabi
  • $(ProjectDir)third-party\mongo-c-driver\include\libmongoc-1.0
  • $(ProjectDir)third-party\mongo-c-driver\include\libbson-1.0

On VC++ Directory page, add this entries into Library Directories list:
  • $(ProjectDir)third-party\mongo-cxx-driver\lib
  • $(ProjectDir)third-party\mongo-c-driver\lib
  • bsoncxx.lib
  • libbsoncxx.lib
  • libmongocxx.lib
  • mongocxx.lib
On Linker/Input page, add this entries into Additional Dependencies:

The code in question is an example given by the official instructions:

    #include <iostream>
   
    #include <bsoncxx/builder/stream/document.hpp>
    #include <bsoncxx/json.hpp>
   
    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
   
    int main(int, char**) {
        mongocxx::instance inst{};
        mongocxx::client conn{ mongocxx::uri{} };
   
        bsoncxx::builder::stream::document document{};
   
        auto collection = conn["testdb"]["testcollection"];
        document << "hello" << "world";
   
        collection.insert_one(document.view());
        auto cursor = collection.find({});
   
        for (auto&& doc : cursor) {
            std::cout << bsoncxx::to_json(doc) << std::endl;
        }
    }

After I copy/paste the .dlls from C and C++ MongoDB drivers into the root folder of my application, it compiles successfully as a release x64 solution.

However, the console outputs strange symbols, then crashes. I guess the issue comes from bsoncxx. Indeed, I have the very same problem with this minimal code:   

    #include <iostream>
   
    #include <bsoncxx/builder/basic/document.hpp>
    #include <bsoncxx/json.hpp>
   
    int main(int, char**) {
        bsoncxx::builder::basic::document doc{};
        doc.append(bsoncxx::builder::basic::kvp("test", 1));
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }


Thank you for your help.

Andrew Morrow

unread,
Jul 7, 2017, 7:46:59 AM7/7/17
to mongod...@googlegroups.com
On Thu, Jul 6, 2017 at 10:58 AM, Guillaume <lethu...@gmail.com> wrote:



I am trying to install and use MongoDB C++ driver on Windows 10 Pro.

I use Boost v. 1.64.0 downloaded from the official website.

First, I have installed the MongoDB C driver v. 1.6.3 following the official instructions.

Then, I have installed the MongoDB C++ driver v. r3.1.1 following the official instructions and using the following cmake command:

cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DCMAKE_PREFIX_PATH=c:\mongo-c-driver -DBOOST_ROOT=C:\boost_1_64_0 -DBSONCXX_POLY_USE_BOOST=1 -DLIBBSON_DIR=C:\mongo-c-driver -DLIBMONGOC_DIR=C:\mongo-c-driver

All of the above looks fine. However, could you please include the msbuild invocation you used to compile and install the driver after running CMake?

 


With Microsoft Visual Studio Community 2017 v. 15.2 Release, I have entered the configuration properties shared here

As an FYI, our current testing for the driver does not include VS 2017, only VS 2015. It is possible that you are finding novel (mis-)behavior.

 

Left click over your project item in Solution Explorer, and choose Properties menu;
On VC++ Directory page, add this entries into Include Directories list:
  • $(ProjectDir)third-party\boost_1_56_0
Is this a typo, or are you really building your project against a different version of boost? If so, it is very unlikely to work. You should build your application and the driver against the same version of boost.
I assume you are doing this so you don't need a PATH set?



 
it compiles successfully as a release x64 solution.

However, the console outputs strange symbols, then crashes. I guess the issue comes from bsoncxx. Indeed, I have the very same problem with this minimal code:   

    #include <iostream>
   
    #include <bsoncxx/builder/basic/document.hpp>
    #include <bsoncxx/json.hpp>
   
    int main(int, char**) {
        bsoncxx::builder::basic::document doc{};
        doc.append(bsoncxx::builder::basic::kvp("test", 1));
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }


Thank you for your help.

My suspicion here is that you have a mismatch somewhere between the application, boost, and the C++ driver regarding which version of boost or the runtime library you are using, though it isn't obvious to me where, unless it is the boost version thing above. Please double check that all components are using the same runtime library (dynamic vs static, single vs multi-threaded, debug vs retail). Mismatches here are a common cause of strange and hard to diagnose runtime errors, because the layout of types like std::string is inconsistent across function call boundaries.

Thanks,
Andrew

Guillaume

unread,
Jul 7, 2017, 8:36:50 AM7/7/17
to mongodb-user
Thank you very much for your quick reply.

All of the above looks fine. However, could you please include the msbuild invocation you used to compile and install the driver after running CMake?

In Visual Studio 2017 Developer Command Prompt v15.0.26430.15, I invoked:

msbuild.exe ALL_BUILD.vcxproj

(There were errors due to a missing "
#include <assert.h>" in mock.hh. It was fixed easily. I re-ran the command successfully).

Then,


msbuild.exe INSTALL.vcxproj

No errors.

As an FYI, our current testing for the driver does not include VS 2017, only VS 2015. It is possible that you are finding novel (mis-)behavior.

Indeed. Do you know if/when you plan to include VS 2017 in your testing?


Is this a typo, or are you really building your project against a different version of boost? If so, it is very unlikely to work. You should build your application and the driver against the same version of boost.

This is a typo: my VS configuration correctly refers to my version of Boost (C:\boost_1_64_0).


I assume you are doing this so you don't need a PATH set?

This is the way I usually do it, but there is probably a better practice. I would be grateful to any advice in this matter.

My suspicion here is that you have a mismatch somewhere between the application, boost, and the C++ driver regarding which version of boost or the runtime library you are using, though it isn't obvious to me where, unless it is the boost version thing above. Please double check that all components are using the same runtime library (dynamic vs static, single vs multi-threaded, debug vs retail). Mismatches here are a common cause of strange and hard to diagnose runtime errors, because the layout of types like std::string is inconsistent across function call boundaries.

I will do my best to re-check everything. If I find something useful, I will share it here.

Andrew Morrow

unread,
Jul 7, 2017, 9:05:40 AM7/7/17
to mongod...@googlegroups.com
On Fri, Jul 7, 2017 at 8:36 AM, Guillaume <lethu...@gmail.com> wrote:
Thank you very much for your quick reply.

All of the above looks fine. However, could you please include the msbuild invocation you used to compile and install the driver after running CMake?

In Visual Studio 2017 Developer Command Prompt v15.0.26430.15, I invoked:

msbuild.exe ALL_BUILD.vcxproj

(There were errors due to a missing "
#include <assert.h>" in mock.hh. It was fixed easily. I re-ran the command successfully).

Then,


msbuild.exe INSTALL.vcxproj

No errors.
Thanks, that all looks reasonable.

 


As an FYI, our current testing for the driver does not include VS 2017, only VS 2015. It is possible that you are finding novel (mis-)behavior.

Indeed. Do you know if/when you plan to include VS 2017 in your testing?

Unfortunately, I do not. While I was the original lead on the mongocxx project, I'm not longer directly involved. I suggest filing a CXX ticket in JIRA requesting that it be added to the testing matrix: https://jira.mongodb.org/browse/CXX

 


Is this a typo, or are you really building your project against a different version of boost? If so, it is very unlikely to work. You should build your application and the driver against the same version of boost.

This is a typo: my VS configuration correctly refers to my version of Boost (C:\boost_1_64_0).

Thanks for clarifying.

 


I assume you are doing this so you don't need a PATH set?

This is the way I usually do it, but there is probably a better practice. I would be grateful to any advice in this matter.

I wish I knew of a better way. It is very unfortunate that Microsoft doesn't offer an analogue to $ORIGIN and $PATH. I suspect there may be some magic involving manifests, but I've never dug in deep enough to find out for sure either way.

 

My suspicion here is that you have a mismatch somewhere between the application, boost, and the C++ driver regarding which version of boost or the runtime library you are using, though it isn't obvious to me where, unless it is the boost version thing above. Please double check that all components are using the same runtime library (dynamic vs static, single vs multi-threaded, debug vs retail). Mismatches here are a common cause of strange and hard to diagnose runtime errors, because the layout of types like std::string is inconsistent across function call boundaries.

I will do my best to re-check everything. If I find something useful, I will share it here.

I definitely recommend checking every aspect of this as carefully as possible. Another facet where these things can get mixed up is between the static and dynamic builds of the C and C++ driver and your application. Make sure you are really linking against the dynamic things all the way through. Some improvements have been made in this area but I don't think they have made it to a stable release yet.

 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/d8c45418-d761-4877-a3c9-d84edb8cc27c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages