1

在过去的两天里,我尝试在两台不同的 Windows 10 机器上构建和测试 mongocxx,一台使用 VS2015,另一台使用 VS2017。构建安装没有错误(尽管我最初遇到了 ENABLE_EXTRA_ALIGNMENT 问题)。但是,一旦我尝试从 MongoDB 安装指南 ( http://mongocxx.org/mongocxx-v3/installation/ ) 构建确切的测试代码,就会收到以下链接器错误:

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)

我已经用 boost 1.59、boost 1.68、boost 1.70 进行了重建,并且使用了没有 boost 的 C++17。

我已经测试了带有 mongocxx 3.4.0 和 mongoc 1.14.0 的构建,以及带有 mongoc 1.10.1 的 mongocxx 3.3.1 的构建,这始终是我最终遇到的错误。

我知道 Visual Studio 会看到 mongocxx.lib,因为如果我删除它,我会收到大量链接器错误,而不仅仅是一个。

我让测试程序运行的唯一方法是使用静态库构建 mongocxx,并在测试程序中将它们与 mongo c 驱动程序的共享库链接。但是,这似乎不是正确的解决方案,因为正如安装指南所说:

不支持针对共享 libmongoc 和静态 mongocxx 链接应用程序,也不支持针对静态 libmongoc 和共享 mongocxx 链接。

同样令人讨厌的是,如果我打开 mongocxx.lib 并搜索“?k_default_uri@uri ...”,我会找到它。所以它在里面。但显然有些不对劲。

请注意,如果我像这样注释掉主函数第一行下方的所有内容:

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

然后它编译并运行该部分就好了。

特别是它无法解析的“mongocxx::uri{}”部分

有什么东西是我没有意识到的 32 位构建的吗?或者我没有意识到的在发布模式下构建的东西?我的 mongocxx 和 mongo c 构建都说 Debug/64 位,但也许有些东西我没有看到

以下是我分别针对 c 驱动程序和 cxx 驱动程序的 cmake 命令:

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" ..

在每一个之后,我运行以下命令:

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

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

这些是我的 VS 项目中的相关属性:

<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>

我在 stackoverflow 上发现的唯一相关问题是:Compiler error with new MongoDB C++ driver,他们得出的结论是“它与错误编译和安装的 bson 和 mongocxx 库有关”。我可以很好地编译这些库,但它们仍然会导致这个错误。所以也许我以某种方式错误地编译它们?我真的迷路了,这证明是在多年的编程中,这是我第一次发布到 stackoverflow。

4

0 回答 0