Using the latest version of libmongoc, 1.29.1, my Visual Studio 2022 Project compiles just fine with MT and even MD setting, but only with the dynamic libs bson-1.0.lib and mongoc-1.0.lib.
This causes my resulting output (mydll.dll) to still require the bson-1.0.dll and mongoc-1.0.dll to be externally available. But i would like them statically compiled into mydll.dll so i have no hassles with local deployments.
I followed the guide here: Using libmongoc in a Microsoft Visual Studio project — libmongoc 1.23.1
The procedure is to include bson-static-1.0.lib and mongoc-static-1.0.lib (and a few other libs), as well as before include mongoc.h, write these lines #define BSON_STATIC #define MONGOC_STATIC
When i do that in my application, i get these > 400 warnings and 47 errors like this:
1>mongoc-static-1.0.lib(kms_request_str.obj) : error LNK2019: unresolved external symbol __imp_strdup referenced in function kms_request_str_path_normalized
1>OLDNAMES.lib(strdup.obi) : error LNK2001: unresolved external symbol __imp_strdup
1>OLDNAMES.lib(strdup.obi) : error LNK2001: unresolved external symbol __imp__strdup
1>C:\dev\MongoCBridge\x64\Debug\MongoCBridge.dll : fatal error LNK1120: 17 unresolved externals
When i try the same procedure with the hello_mongoc project from the examples of libmongoc, i also get errors but a little different. Examples:
2>hello_mongoc.obj : error LNK2019: unresolved external symbol __imp_bson_free referenced in function main
2>hello_mongoc.obj : error LNK2019: unresolved external symbol __imp_bcon_new referenced in function main
Small correction, using the hello_mongoc project, when following the link above, i do of course not get missing __img_bson_free (that would mean the bson lib as such is missing), but i get these:
1>bson-static-1.0.lib(bson.obj) : error LNK2019: unresolved external symbol __imp_strspn referenced in function should_ignore
1>bson-static-1.0.lib(bson.obj) : error LNK2019: unresolved external symbol __imp__time64 referenced in function time
1>bson-static-1.0.lib(bson-oid.obj) : error LNK2001: unresolved external symbol __imp__time64
1>bson-static-1.0.lib(bson-decimal128.obj) : error LNK2019: unresolved external symbol __imp_isupper referenced in function _dec128_tolower
1>bson-static-1.0.lib(bson-string.obj) : error LNK2001: unresolved external symbol __imp_isupper
1>bson-static-1.0.lib(bson-decimal128.obj) : error LNK2019: unresolved external symbol __imp_isdigit referenced in function bson_decimal128_from_string_w_len
1>bson-static-1.0.lib(bson-string.obj) : error LNK2001: unresolved external symbol __imp_isdigit
1>bson-static-1.0.lib(bson-iso8601.obj) : error LNK2001: unresolved external symbol __imp_isdigit
1>bson-static-1.0.lib(bson-decimal128.obj) : error LNK2019: unresolved external symbol __imp___stdio_common_vsscanf referenced in function _vsscanf_s_l
1>bson-static-1.0.lib(bson-error.obj) : error LNK2019: unresolved external symbol __imp_strerror_s referenced in function bson_strerror_r
1>bson-static-1.0.lib(bson-string.obj) : error LNK2019: unresolved external symbol __imp_isalpha referenced in function bson_ascii_strtoll
1>bson-static-1.0.lib(bson-string.obj) : error LNK2019: unresolved external symbol __imp_isspace referenced in function bson_isspace
1>bson-static-1.0.lib(bson-string.obj) : error LNK2019: unresolved external symbol __imp__stricmp referenced in function bson_strcasecmp
1>bson-static-1.0.lib(bson-iso8601.obj) : error LNK2019: unresolved external symbol __imp__gmtime64_s referenced in function gmtime_s
1>bson-static-1.0.lib(bson-iso8601.obj) : error LNK2019: unresolved external symbol __imp_strftime referenced in function _bson_iso8601_date_format
1>C:\repos\mongo-cxx-driver-r4.0.0\build\_deps\mongo-c-driver-build\src\libmongoc\Debug\hello_mongoc.exe : fatal error LNK1120: 11 unresolved externals
OK i believe i found out what the issue was. It looks like by default, when compiling mongoc visual studio project “as is”, the output libs named static are not anyhow statically built.
To compile mongoc statically (MT) and embed the dependencies in my own dll, i had to compile libbson and libmongoc as well as their dependencies with MT too.
So here is my guide:
First, compile MT (C++ Code Generation) Versions of the sub projects from the visual studio project that contains all the mongoc examples and libs, namely: mpongoc_static and bson_static, you might need to compile their dependencies also as MT, e.g. zlib and i believe utf8proc_obj.
My successful linker dependencies look like this:
This way, my project compiled successfully without dependent dll’s But there were lots of warnings. To get rid of these warnings, i had to add to Linker/Commandline:
/NODEFAULTLIB:MSVCRTD (or /NODEFAULTLIB:MSVCRT for release)
If you still get warnings like LNK4286, ensure that you include mongoc.h only once in the whole project and use the define BSON_STATIC, which changes decldir to cdecl.