1

For the past two days I have tried to build and test mongocxx on two different Windows 10 machines, one using VS2015 and the other using VS2017. The builds install without errors (though I had initial issues with ENABLE_EXTRA_ALIGNMENT). However, once I try to build the exact test code from the MongoDB installation guide (http://mongocxx.org/mongocxx-v3/installation/), I get the following linker error:

error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const mongocxx::v_noabi::uri::k_default_uri" (?k_default_uri@uri@v_noabi@mongocxx@@2V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)

I have rebuilt with boost 1.59, boost 1.68, boost 1.70, and using C++17 with no boost.

I have tested from builds with mongocxx 3.4.0 with mongoc 1.14.0, and mongocxx 3.3.1 with mongoc 1.10.1, and this is always the error I end up at.

I know Visual Studio sees mongocxx.lib, because if I remove it I get a ton of linker errors instead of just one.

The one and ONLY way I got the test program to run was to build mongocxx with static libraries, and in the test program link them along with the shared libraries for the mongo c driver. However, that doesn't seem like the right solution because as the installation guide says:

Linking an application against both shared libmongoc and static mongocxx is not supported, nor is linking against both static libmongoc and shared mongocxx.

What is also annoying is that if I open up mongocxx.lib and search for "?k_default_uri@uri..." I find it. So its in there. But something is wrong apparently.

Note that if I comment out everything below the first line in the main function like so:

#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;
    }
    */
}

Then it compiles and runs that part just fine.

It is specifically the "mongocxx::uri{}" part that it is unable to resolve

Is something being built as 32 bit that I don't realize? Or something being built in release mode that I don't realize? My mongocxx and mongo c builds all say Debug/64 bit, but maybe there is something I'm not seeing

Here are my cmake commands for the c driver and cxx driver respectively:

cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Debug" "-DENABLE_EXTRA_ALIGNMENT=OFF" "-DCMAKE_INSTALL_PREFIX=C:\<path removed>\mongo-c-driver" "-DCMAKE_PREFIX_PATH=C:\<path removed>\mongo-c-driver" ..

cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Debug" "-DBSONCXX_POLY_USE_BOOST=1" "-DCMAKE_INSTALL_PREFIX=C:\<path removed>\mongo-cxx-driver" "-DCMAKE_PREFIX_PATH=C:\<path removed>\mongo-c-driver" "-DBOOST_ROOT=C:\<path removed>\boost_1_68_0" ..

And after each of these I run the following:

msbuild.exe /p:Configuration=Debug ALL_BUILD.vcxproj

msbuild.exe /p:Configuration=Debug INSTALL.vcxproj

And these are the relevant properties from my VS project:

<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\..\libraries\boost_1_68_0;$(ProjectDir)..\..\..\libraries\mongo-cxx-driver\include\mongocxx\v_noabi;$(ProjectDir)..\..\..\libraries\mongo-c-driver\include\include\libmongoc-1.0;$(ProjectDir)..\..\..\libraries\mongo-c-driver\include\libbson-1.0;$(ProjectDir)..\..\..\libraries\mongo-cxx-driver\include\bsoncxx\v_noabi</AdditionalIncludeDirectories>
<PreprocessorDefinitions>MONGOCXX_STATIC;BSONCXX_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\..\libraries\boost_1_68_0\stage\lib;$(ProjectDir)..\..\..\libraries\mongo-c-driver\lib;$(ProjectDir)..\..\..\libraries\mongo-cxx-driver\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>mongocxx.lib;bsoncxx.lib;mongoc-static-1.0.lib;bson-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

The only relevant question I found on stackoverflow is this one: Compiler error with new MongoDB C++ driver and they concluded "It had to do with incorrectly compiled and installed libraries for bson and mongocxx". I can compile the libraries just fine, yet they still result in this error. So maybe I am compiling them incorrectly somehow? I'm truly lost, and the proof of this is that in years of programming this is the first time I have posted to stackoverflow.

M H
  • 41
  • 1
  • 6
  • I found one resolution to this issue: define the variable myself. I added const std::string mongocxx::v_noabi::uri::k_default_uri = "mongodb://localhost:27017"; to the start of my code and now the test program works. However, this isn't really a "solution" so much as a quick fix and doesn't establish why this object isn't found in the library. – M H Jun 14 '19 at 17:16
  • Ok so I have a better solution that I will be using, I don't know if it solves the problem in terms of dynamic linking but I built the cxx connector with static libraries (using -DBUILD_SHARED_LIBS=OFF) and then placed all of these in my additional dependencies: mongocxx-static.lib bsoncxx-static.lib mongoc-static-1.0.lib bson-static-1.0.lib ws2_32.lib Secur32.lib Crypt32.lib BCrypt.lib Dnsapi.lib And now I can build and run the test program without issue. – M H Jun 14 '19 at 19:21

0 Answers0