Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-4376

Build errors on Windows when missing C99 features

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 1.21.1
    • Component/s: None
    • None

      Summary

      There are issues building with Visual Studio 2012 and quite likely other compilers not supporting certain C99 features.

      The issues caused by stdbool.h not being defined are easy to fix.
      For them, I opened this PR https://github.com/mongodb/mongo-c-driver/pull/980

      For other issues, I only have a partial solution.

      Environment

      I'm using:

      • mongo-c-driver 1.21.1 (tag)
      • Windows Server 2019 Standard, x64, latest updates
      • Visual Studio Professional 2012 Update 5
      • CMake 3.23.1

      Visual Studio 2012 is not in the continuous build, but it is supported according to the doc

      Building on Windows requires Windows Vista or newer and Visual Studio 2010 or newer.

      How to Reproduce

      Download https://github.com/mongodb/mongo-c-driver/archive/refs/tags/1.21.1.zip
      Extract the contents to C:\MyTempDir
      Rename C:\MyTempDir\mongo-c-driver-1.21.1 to C:\MyTempDir\mongo-c-driver

      Create a new folder C:\MyTempDir\mongo-c-driver\cmake-VS2012-x64
      From that folder, run this cmd

      cmake -G "Visual Studio 11 2012 Win64" -LH -DBUILD_VERSION=1.21.1 -DCMAKE_CONFIGURATION_TYPES="Debug;Release" -DCMAKE_INSTALL_PREFIX=C:/MyTempDir/mongo-c-driver/cmake-VS2012-x64 -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_PREFIX_PATH=C:/MyTempDir/mongo-c-driver/cmake-VS2012-x64 -DCMAKE_VERBOSE_MAKEFILE=ON -DENABLE_BSON=ON -DENABLE_EXAMPLES=OFF -DENABLE_ICU=OFF -DENABLE_SASL=OFF -DENABLE_SNAPPY=OFF -DENABLE_SSL=OFF -DENABLE_STATIC=ON -DENABLE_TESTS=OFF -DMONGO_USE_LLD=OFF ../
      

      This is the cmake output: cmake build log.txt

      This fix is part of PR 980.

      Open C:\MyTempDir\mongo-c-driver\cmake-VS2012-x64\mongo-c-driver.sln
      Ensure the build mode is Debug x64.
      Build the bson_static project.
      This is the build output: bson_static build 1 with errors.txt

      Notice several errors like the following one

      2>  bcon.c
      2>C:\MyTempDir\mongo-c-driver\cmake-VS2012-x64\src\libbson\src\bson\bson-config.h(34): fatal error C1017: invalid integer constant expression
      

      This is due to a broken BSON_HAVE_STDBOOL_H macro definition.
      To solve it quickly, you can
      Open C:\MyTempDir\mongo-c-driver\cmake-VS2012-x64\src\libbson\src\bson\bson-config.h
      Replace this line

      #define BSON_HAVE_STDBOOL_H 

      with this line

      #define BSON_HAVE_STDBOOL_H 0

      The missing 0 in the macro is caused to a defect in a cmake script.
      To fix it at the source, you can
      Open C:\MyTempDir\mongo-c-driver\src\libbson\CMakeLists.txt
      Under this line

      CHECK_INCLUDE_FILE (stdbool.h BSON_HAVE_STDBOOL_H)
      

      add these lines

      if (NOT BSON_HAVE_STDBOOL_H)
         set (BSON_HAVE_STDBOOL_H 0)
      endif ()
      

      Build the bson_static project again.
      Now it builds without errors.
      This is the build output: bson_static build 2 ok.txt

      This fix is also part of PR 980.

      Build the mongoc_static project.
      This is the build output: mongoc_static build 1 with errors.txt

      There are several errors. Among them, notice this one

      2>  mongoc-timeout.c
      2>c:\mytempdir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-timeout-private.h(23): fatal error C1083: Cannot open include file: 'stdbool.h': No such file or directory
      

      This is due to an include <stdbool.h> with no fallback.
      The bson-compat.h has a fallback for the missing stdbool.h.

      To fix the issue, open C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-timeout-private.h
      Replace this line

      #include <stdbool.h>

      with this one

      #include "bson-compat.h"

      By the way, a few files in C:\MyTempDir\mongo-c-driver\src\kms-message\src have the same issue and can be fixed the same way.

      The following issues are not due to missing stdbool.h.
      They are not fixed by PR 980.

      Build the mongoc_static project again.
      This is the build output: mongoc_static build 2 with errors.txt

      There are several errors like the following one

      1>  mongoc-aggregate.c
      1>c:\mytempdir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-topology-private.h(465): error C2059: syntax error : '{'
      

      This is due to the mc_tpld_take_ref function using an unsupported syntax.
      To fix this issue
      Open C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-topology-private.h
      Replace these lines

      {
         return (mc_shared_tpld){
            ._sptr_ = mongoc_atomic_shared_ptr_load (&tpl->_shared_descr_._sptr_)};
      }
      

      with these lines

      {
         mc_shared_tpld res;
         res._sptr_ = mongoc_atomic_shared_ptr_load (&tpl->_shared_descr_._sptr_);
         return res;
      }
      

      Build the mongoc_static project again.
      This is the build output: mongoc_static build 3 with errors.txt

      Notice these errors

      1>  mongoc-shared.c
      1>C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-shared.c(81): error C2059: syntax error : '{'
      1>C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-shared.c(90): error C2059: syntax error : '{'
      

      They are due to the MONGOC_SHARED_PTR_NULL macro using an unsupported syntax.
      To fix the issue
      Open C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-shared-private.h
      Replace these lines

      #define MONGOC_SHARED_PTR_NULL \
         ((mongoc_shared_ptr){       \
            .ptr = NULL,             \
            ._aux = NULL,            \
         })
      

      With this line

      #define MONGOC_SHARED_PTR_NULL(x) mongoc_shared_ptr x; x.ptr = NULL; x._aux = NULL
      

      Then, open C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-shared.c
      Replace this line

      mongoc_shared_ptr ret = MONGOC_SHARED_PTR_NULL;

      with this line

      MONGOC_SHARED_PTR_NULL ( ret );

      Replace this line

      mongoc_shared_ptr prev = MONGOC_SHARED_PTR_NULL;

      with this line

      MONGOC_SHARED_PTR_NULL ( prev );

      Build the mongoc_static project again.
      This is the build output: mongoc_static build 4 with errors.txt

      The following errors remain

      1>  mongoc-timeout.c
      1>c:\mytempdir\mongo-c-driver\src\libbson\src\bson\bson-prelude.h(18): fatal error C1004: unexpected end-of-file found
      1>  mongoc-topology.c
      1>C:\MyTempDir\mongo-c-driver\src\libmongoc\src\mongoc\mongoc-topology.c(2008): error C2059: syntax error : '{'
      

      I don't know how to fix them.

        1. bson_static build 1 with errors.txt
          7 kB
        2. bson_static build 2 ok.txt
          9 kB
        3. cmake build log.txt
          7 kB
        4. mongoc_static build 1 with errors.txt
          29 kB
        5. mongoc_static build 2 with errors.txt
          20 kB
        6. mongoc_static build 3 with errors.txt
          15 kB
        7. mongoc_static build 4 with errors.txt
          15 kB

            Assignee:
            kevin.albertson@mongodb.com Kevin Albertson
            Reporter:
            spam.dump.one@gmail.com Agostino Sturaro
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: