Building mongo-cxx-driver 3.7.0 on Windows with MSVC

Hey,
I am trying to build mongo-cxx-driver on windows and hitting some issues. Has anyone been able to build the latest release(r3.7.0)? I didn’t have an issue with r3.6.7.

The issue seems to be related with pollyfill but not sure what changed as yet.

Usually build with polyfill disabled but it seems in that case bsoncxx automatically enables polyfill.

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=C:_deps\mongodb -DCMAKE_CXX_FLAGS=’/Zc:__cplusplus’ -DCMAKE_CXX_STANDARD=17 …

01:50:38  -- Auto-configuring bsoncxx to use C++17 std library polyfills since C++17 is active and user didn't specify otherwise

01:50:38  bsoncxx version: 3.7.0

Which leads to this error

82>C:\conan\.conan\data\mongo-cxx-driver\3.7.0\nemtech\stable\package\ba80356c2e9b1444ad652db7a781f16d357b0c49\include\bsoncxx\v_noabi\bsoncxx/stdx/make_unique.hpp(66,1): fatal error C1189: #error: "Cannot find a valid polyfill for make_unique" [C:\tmp\_build\extensions\mongo\tests\test\tests.catapult.test.mongo.vcxproj]

Now I am trying to enable polyfill using boost

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=C:\Users\wayon\code\symbol\symbol\client\catapult\_deps\mongodb -DBOOST_ROOT=C:\Users\wayon\code\symbol\symbol\client\catapult\_deps\boost -DCMAKE_CXX_STANDARD=17 -DBSONCXX_POLY_USE_BOOST=1 ..

This leads to unresolve symbol for boost. :thinking:

     Creating library C:/Users/wayon/code/symbol/symbol/client/catapult/_deps/source/mongo-cxx-driver/_build/src/mongocxx/Debug/mongocxx-mocked.lib and object C:/Users/wayon/code/symbol/symbol/client/catapult/_deps/source/mongo-cxx-driver/_build/src/mongocxx/Debug/mong
  ocxx-mocked.exp
collection.obj : error LNK2019: unresolved external symbol "void __cdecl boost::throw_exception(class std::exception const &)" (?throw_exception@boost@@YAXAEBVexception@std@@@Z) referenced in function "public: class mongocxx::v_noabi::result::bulk_write & __cdecl boost
::optional<class mongocxx::v_noabi::result::bulk_write>::value(void)& " (?value@?$optional@Vbulk_write@result@v_noabi@mongocxx@@@boost@@QEGAAAEAVbulk_write@result@v_noabi@mongocxx@@XZ) [C:\Users\wayon\code\symbol\symbol\client\catapult\_deps\source\mongo-cxx-driver\_bu
ild\src\mongocxx\mongocxx_mocked.vcxproj]
change_stream.cpp.obj : error LNK2001: unresolved external symbol "void __cdecl boost::throw_exception(class std::exception const &)" (?throw_exception@boost@@YAXAEBVexception@std@@@Z) [C:\Users\wayon\code\symbol\symbol\client\catapult\_deps\source\mongo-cxx-driver\_bu
ild\src\mongocxx\mongocxx_mocked.vcxproj]
C:\Users\wayon\code\symbol\symbol\client\catapult\_deps\source\mongo-cxx-driver\_build\src\mongocxx\Debug\mongocxx-mocked.dll : fatal error LNK1120: 1 unresolved externals [C:\Users\wayon\code\symbol\symbol\client\catapult\_deps\source\mongo-cxx-driver\_build\src\mongo
cxx\mongocxx_mocked.vcxproj]

This is with Visual Studio 2019 and Visual Studio 2022

I could successfully built mongo-cxx-driver 3.7.0 on Windows 11 with Visual Studio 2022.

1. Setup Build
cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_FLAGS="/Zc:__cplusplus" -DCMAKE_PREFIX_PATH=C:\mongo-c-driver -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver

2. Execute Build
cmake --build . --target install

Please make sure the /Zc:__cplusplus flag is correctly set.

Thanks @Rishabh_Bisht for the quick reply.
When I build with using your cmake command, tests are failing to build with the error below. :thinking:

Can you tell me which VS development env are you using?

cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_FLAGS="/Zc:__cplusplus" -DCMAKE_PREFIX_PATH=C:\mongo-c-driver -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver ..
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22621.
-- The CXX compiler identification is MSVC 19.34.31933.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: C:/Python310/python.exe (found version "3.10.3")
C:\Users\wayon\code\wayonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\etc\calc_release_version.py:29: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.version import LooseVersion
-- No build type selected, default is Release
-- The C compiler identification is MSVC 19.34.31933.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Auto-configuring bsoncxx to use C++17 std library polyfills since C++17 is active and user didn't specify otherwise
bsoncxx version: 3.7.0
found libbson version 1.23.1
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Failed
mongocxx version: 3.7.0
found libmongoc version 1.23.1
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Found Threads: TRUE
-- Build files generated for:
--      build system: Visual Studio 17 2022
--      instance: C:/Program Files/Microsoft Visual Studio/2022/Community
--      instance: x64
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/wayon/code/wayonb/monorepo/client/catapult/_deps/source/mongo-cxx-driver/_build
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\ostream(305,1): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc [C:\Users\wayon\code\wayonb\monorepo\client\catapu
lt\_deps\source\mongo-cxx-driver\_build\src\bsoncxx\test\test_bson.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\ostream(298,1): message : while compiling class template member function 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_trai
ts<char>>::operator <<(unsigned int)' [C:\Users\wayon\code\wayonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\_build\src\bsoncxx\test\test_bson.vcxproj]
C:\Users\wayon\code\wayonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\src\bsoncxx/test_util/to_string.hh(53,57): message : see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostrea
m<char,std::char_traits<char>>::operator <<(unsigned int)' being compiled [C:\Users\wayon\code\wayonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\_build\src\bsoncxx\test\test_bson.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\ostream(671,75): message : see reference to class template instantiation 'std::basic_ostream<char,std::char_traits<char>>' being compiled [C:\Users\wayon\code\wa
yonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\_build\src\bsoncxx\test\test_bson.vcxproj]
  Generating Code...
C:\Users\wayon\code\wayonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\src\bsoncxx\test\bson_builder.cpp(1705): fatal  error C1001: Internal compiler error. [C:\Users\wayon\code\wayonb\monorepo\client\catapult\_deps\source\mongo-cxx-driver\
_build\src\bsoncxx\test\test_bson.vcxproj]
  (compiler file 'D:\a\_work\1\s\src\vctools\Compiler\Utc\src\p2\main.c', line 224)
   To work around this problem, try simplifying or changing the program near the locations listed above.
  If possible please provide a repro here: https://developercommunity.visualstudio.com
  Please choose the Technical Support command on the Visual C++
   Help menu, or open the Technical Support help file for more information
  Building Custom Rule C:/Users/wayon/code/wayonb/monorepo/client/catapult/_deps/source/mongo-cxx-driver/src/mongocxx/test/CMakeLists.txt
  client_helpers.cpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\ostream(482,1): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc [C:\Users\wayon\code\wayonb\monorepo\client\catapu
lt\_deps\source\mongo-cxx-driver\_build\src\mongocxx\test\test_client_side_encryption_specs.vcxproj]

Looking at the last logs it would seem like the build succeed. The only reason checked for an error cause cmake exited with error code of 1 and my script will stop on error.

An internal compiler error is an issue with the compiler, not sure what we can do here. But I’d be happy to raise a ticket with our engineering team to get this investigated more thoroughly.
In the meantime, try commenting the test that’s causing this issue, unless you need the tests as well.

I used VS 2022 Community as well.

Thanks @Rishabh_Bisht for raising a ticket.

I am more curious of why you are not seeing this error. I have built with Windows 11 and Windows Server 2022 with both VS 2019 and VS 2022 and always get this error. Thinking I am missing something in my environment but not sure what.

Also, do you know why if I set the -DCMAKE_CXX_FLAGS="/Zc:__cplusplus" flag correctly, why is ployfill getting configured for bsoncxx? Is it required?

Upon a deeper look, I found out I wasn’t building tests. I come across similar error as yours when building tests. This needs investigation.

For polyfill question
Auto-configuring bsoncxx to use C++17 std library polyfills since C++17 is active and user didn't specify otherwise means the C++17 standard library features are used to satisfy the C++17 polyfill. This is expected. It is enabled when -DCMAKE_CXX_STANDARD=17 flag is set.

Thanks @Rishabh_Bisht, glad you were able to repo the issue when build tests. :+1:

Pollyfill question
Yes, I saw the cmake file and BSONCXX_POLY_USE_STD is defined in this case.
The issue seems to be that for make_unique, this define is not used to include the STD header. maybe something is missing?

When trying to build I am getting the error below
bsoncxx/stdx/make_unique.hpp(66,1): fatal error C1189: #error: "Cannot find a valid polyfill for make_unique"

Hey @Rishabh_Bisht, Do you have any information on the ployfill error?

The make_unique should come as part of <memory> header once the _cplusplus flag is set correctly. I am not sure what’s going wrong here.

Can you try to dump and check __cplusplus macro - if it’s set correctly or not?

Thanks @Rishabh_Bisht. I missed that. For some reason, we have gotten the our code to build without setting the __cplusplus macro.

@Rishabh_Bisht setting the __cplusplus macro worked with building our code.
Thanks for the help :+1:

Glad to hear that, @Wayon_Blair !

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.