Hi I have been using the c / cxx driver for a project, but am running into some issues.
Trying to build the static libs, but as I already link zlib into the project I have to use the
-DENABLE_ZLIB=OFF
option to stop linking it twice.
I have seen that there is a -DENABLE_ZLIB=SYSTEM option, but currently I believe that this does not work, as even when specifying
-DZLIB_ROOT=path/to/zlib/latest
-DZLIB_LIBRARY=path/to/zlib/lib/zlib.lib
there is a build error from mongo-compression.c stating that it cannot find zlib.h, implying the build is not respecting ZLIB_ROOT
makefile to call cmake:
configure = \
-DENABLE_ZLIB=OFF \
-DZLIB_LIBRARY=path/to/zlib/lib/zlib.lib \
-DZLIB_INCLUDE_DIR=path/to/zlib/latest \
-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$(MAKEFILE_PATH)/$(PLATFORM) \
-DCMAKE_PREFIX_PATH=$(MAKEFILE_PATH)/$(PLATFORM) \
-DBUILD_SHARED_LIBS=OFF \
-DENABLE_TESTS=OFF \
-DENABLE_EXAMPLES=OFF \
../source
install = \
--build . \
--config Release \
--target install
default: build
build:
mkdir build
mkdir $(PLATFORM)
cd build && cmake $(configure)
cd build && cmake $(install)
@Thomas_Morten it is not clear what you are trying to accomplish. It seems like you are building a project that links directly to both zlib and libmongoc. You state that you want to compile the C driver static libraries, so I am assuming that you want to statically link libmongoc, but you do not say whether you are linking zlib statically or dynamically. I am going to assume that you want to want to link both statically.
In that case, the correct way is to specify -DENABLE_ZLIB=SYSTEM
. Then, if your system zlib is not in a location that can be located by the C driver build, you can also specify -DZLIB_ROOT=path/to/zlib/latest
. Note that the ZLIB_ROOT
should be the path that contains the lib
and include
directories created by the zlib build. On a typical system installation this would be either /usr
or /usr/local
. Most likely, you want to use whatever directory was specified with the --prefix
option of the zlib build. The ZLIB_LIBRARY
variable has no effect, so you can safely remove that.
Your own project that then consumes both the C driver and zlib directly will need to build using the C driver static libraries and the same zlib static library. This will ensure that there are no conflicts between library implementation expected by the C driver static libraries and your own project’s static libraries.
Either way, specifying -DENABLE_ZLIB=OFF
is going render all the other zlib-related options useless, as the build will not include any zlib references. That approach is unlikely to be what you want, unless what you want is a C driver without zlib support.
All that said, if you want static linkage of everything and if your own project uses CMake, then the easiest thing is going to be to make the C driver sources a sub-directory of your own build, include the C driver with something like add_subdirectory(mongo-c-driver)
, then link the C driver library to whichever of your project components need it with target_link_libraries(foo mongo::mongoc_static)
.
1 Like
- Our project does not use cmake
- We are building a project that should link statically to zlib and mongoc.
- System zlib is not in a location that can be found by the mongoc build.
specifying -DENABLE_ZLIB=SYSTEM and -DZLIB_ROOT=\zlib results in the error [\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-compression.c(26): fatal error C1083: Cannot open include file: ‘zlib.h’: No such file or directory [\mongo-c-driver\build
src\libmongoc\mongoc_shared.vcxproj] during the build.
Directory listing of zlib root, and full cmake command included for reference
Directory of <Path To>\zlib
<DIR> include
<DIR> lib
Directory of <Path To>\zlib\include
<DIR> amiga
<DIR> contrib
31,009 crc32.h
13,014 deflate.h
<DIR> doc
<DIR> examples
4,809 gzguts.h
438 inffast.h
6,437 inffixed.h
6,521 inflate.h
2,990 inftrees.h
<DIR> msdos
<DIR> nintendods
<DIR> old
<DIR> qnx
8,600 trees.h
<DIR> watcom
<DIR> win32
13,826 zconf.h
81,246 zlib.h
7,427 zutil.h
Directory of <Path To>\zlib\lib
338,596 zlib.lib
cmake:
cmake \
-DENABLE_ZLIB=SYSTEM \
-DZLIB_ROOT=<Path To>\zlib \
-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=<Path To>/install/ \
-DCMAKE_PREFIX_PATH=<Path To>/install/ \
-DBUILD_SHARED_LIBS=OFF \
-DENABLE_TESTS=OFF \
-DENABLE_EXAMPLES=OFF \
-A x64 \
../source
cmake \
--build . \
--config Release \
--target install
output:
Project "<Path To>\mongo-c-driver\build\ALL_BUILD.vcxproj" (3) is building "<Path To>\mongo-c-driver\build\src\libmongoc\mongoc-stat.vcxproj" (6) on node 1 (default targets).
Project "<Path To>\mongo-c-driver\build\src\libmongoc\mongoc-stat.vcxproj" (6) is building "<Path To>\mongo-c-driver\build\src\libmongoc\mongoc_shared.vcxproj" (7) on node 1 (default targets).
PrepareForBuild:
Creating directory "mongoc_shared.dir\Release\".
Creating directory "<Path To>\mongo-c-driver\build\src\libmongoc\Release\".
Creating directory "mongoc_shared.dir\Release\mongoc_shared.tlog\".
InitializeBuildStatus:
Creating "mongoc_shared.dir\Release\mongoc_shared.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
CustomBuild:
Building Custom Rule D:/GitHub/externalapi/mongodb/mongo-c-driver/source/src/libmongoc/CMakeLists.txt
CMake does not need to re-run because D:/GitHub/externalapi/mongodb/mongo-c-driver/build/src/libmongoc/CMakeFiles/generate.stamp is up-to-date.
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\CL.exe /c /I"<Path To>\mongo-c-driver\build\src\libmongoc\src" /I"<Path To>\mongo-c-drive
r\build\src\libmongoc\src\mongoc" /I"<Path To>\mongo-c-driver\source\src\libmongoc\src" /I"<Path To>\mongo-c-driver\source\src\libmongoc\..\..\src\common" /I"<Path To>\mongo-
c-driver\source\src\libmongoc\..\kms-message\src" /I"<Path To>\mongo-c-driver\source\src\libbson\src" /I"<Path To>\mongo-c-driver\build\src\libbson\src" /I"<Path To>\mongo-c-
driver\build\src\libbson\src\bson" /nologo /W3 /WX- /diagnostics:classic /O2 /Ob2 /D WIN32 /D _WINDOWS /D NDEBUG /D MONGOC_COMPILATION /D KMS_MSG_STATIC /D KMS_MESSAGE_ENABLE_CRYPTO /D KMS_MESSAGE_ENABLE_CRYPTO_CNG /D _CRT_SECURE_NO_W
ARNINGS /D _GNU_SOURCE /D _BSD_SOURCE /D _DEFAULT_SOURCE /D COMMON_PREFIX_=_mongoc_common /D "CMAKE_INTDIR=\"Release\"" /D mongoc_shared_EXPORTS /D _WINDLL /D _MBCS /Gm- /MD /GS /fp:precise /Qspectre /Zc:wchar_t /Zc:forScope /Zc:inlin
e /Fo"mongoc_shared.dir\Release\\" /Fd"mongoc_shared.dir\Release\vc141.pdb" /Gd /TC /FC /errorReport:queue "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-aggregate.c" "D:\GitHub\externalapi\mongod
b\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-apm.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-array.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc
\mongoc-async.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-async-cmd.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-buffer.c" "D:\GitHub\externalapi\m
ongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-bulk-operation.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-change-stream.c" "<Path To>\mongo-c-driver\source\
src\libmongoc\src\mongoc\mongoc-client.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-client-pool.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-client-
side-encryption.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cluster.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cluster-aws.c" "D:\GitHub\external
api\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cluster-sasl.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-collection.c" "<Path To>\mongo-c-driver\source\
src\libmongoc\src\mongoc\mongoc-compression.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-counters.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-crypt
.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor-array.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor.c" "<Path To>\mon
go-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor-cmd.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor-change-stream.c" "<Path To>\mongo-c-driver\source\src\lib
mongoc\src\mongoc\mongoc-cursor-cmd-deprecated.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor-find.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc
-cursor-find-cmd.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor-find-opquery.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cursor-legacy.c" "D:\
GitHub\externalapi\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-database.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-error.c" "<Path To>\mongo-c-driver\s
ource\src\libmongoc\src\mongoc\mongoc-find-and-modify.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-init.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc
-gridfs.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-gridfs-bucket.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-gridfs-bucket-file.c" "D:\GitHub\ext
ernalapi\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-gridfs-file.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-gridfs-file-list.c" "<Path To>\mongo-c-driv
er\source\src\libmongoc\src\mongoc\mongoc-gridfs-file-page.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-handshake.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mon
goc\mongoc-host-list.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-http.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-index.c" "D:\GitHub\externalapi\
mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-interrupt.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-list.c" "<Path To>\mongo-c-driver\source\src\libmongoc
\src\mongoc\mongoc-linux-distro-scanner.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-log.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-matcher.c" "D:
\GitHub\externalapi\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-matcher-op.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-memcmp.c" "<Path To>\mongo-c-driv
er\source\src\libmongoc\src\mongoc\mongoc-cmd.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-opts-helpers.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc
-opts.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-queue.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-read-concern.c" "<Path To>
\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-read-prefs.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-rpc.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mo
ngoc\mongoc-server-description.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-server-stream.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-client-sessio
n.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-server-monitor.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-set.c" "<Path To>\mon
go-c-driver\source\src\libmongoc\src\mongoc\mongoc-socket.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-buffered.c" "<Path To>\mongo-c-driver\source\src\libmongoc\sr
c\mongoc\mongoc-stream.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-file.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-gridfs.c" "D:\Gi
tHub\externalapi\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-gridfs-download.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-gridfs-upload.c" "D:\GitHub\externala
pi\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-socket.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-topology.c" "<Path To>\mongo-c-driver\source\sr
c\libmongoc\src\mongoc\mongoc-topology-background-monitoring.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-topology-description.c" "<Path To>\mongo-c-driver\source\src\libm
ongoc\src\mongoc\mongoc-topology-description-apm.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-topology-scanner.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc
\mongoc-uri.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-util.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-version-functions.c" "D:\GitHub\externala
pi\mongodb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-write-command.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-write-command-legacy.c" "<Path To>\mongo-c-driv
er\source\src\libmongoc\src\mongoc\mongoc-write-concern.c" "<Path To>\mongo-c-driver\source\src\common\common-b64.c" "<Path To>\mongo-c-driver\source\src\common\common-md5.c" "D:\GitHub\external
api\mongodb\mongo-c-driver\source\src\common\common-thread.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-crypto.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc
\mongoc-scram.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-stream-tls.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-ssl.c" "D:\GitHub\externalapi\mon
godb\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-crypto-cng.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-rand-cng.c" "<Path To>\mongo-c-driver\source\src\libmong
oc\src\mongoc\mongoc-stream-tls-secure-channel.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-secure-channel.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mon
goc-sasl.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-cluster-sspi.c" "<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-sspi.c"
mongoc-aggregate.c
...
mongoc-compression.c
[<Path To>\mongo-c-driver\source\src\libmongoc\src\mongoc\mongoc-compression.c(26): fatal error C1083: Cannot open include file: 'zlib.h': No such file or directory [<Path To>\mongo-c-driver\build\
src\libmongoc\mongoc_shared.vcxproj]
@Thomas_Morten Using the additional information you provided I was able to reproduce the error. Using a Windows machine with VS2017, I built zlib and installed it to C:\zlib, then attempted to build the C driver using the same options you did in your build. I encountered the same “No such file” error.
I am writing up a ticket and will be fixing this in our Git repository shortly. In the meantime, you can get the C driver build working by applying this patch to your local copy:
diff --git a/src/libmongoc/CMakeLists.txt b/src/libmongoc/CMakeLists.txt
index 1fde3bfee..2f621971d 100644
--- a/src/libmongoc/CMakeLists.txt
+++ b/src/libmongoc/CMakeLists.txt
@@ -55,6 +55,7 @@ configure_file (
"${CMAKE_BINARY_DIR}/src/zlib-1.2.11/zconf.h"
COPYONLY
)
+set (ZLIB_INCLUDE_DIRS "")
if (ENABLE_ZLIB MATCHES "SYSTEM|AUTO")
message (STATUS "Searching for zlib CMake packages")
include (FindZLIB)
@@ -74,13 +75,12 @@ if (ENABLE_ZLIB MATCHES "SYSTEM|AUTO")
endif ()
endif ()
-set (PRIVATE_ZLIB_INCLUDES "")
if ( (ENABLE_ZLIB STREQUAL "BUNDLED")
OR (ENABLE_ZLIB STREQUAL "AUTO" AND NOT ZLIB_FOUND) )
message (STATUS "Enabling zlib compression (bundled)")
set (SOURCES ${SOURCES} ${ZLIB_SOURCES})
set (
- PRIVATE_ZLIB_INCLUDES
+ ZLIB_INCLUDE_DIRS
"${SOURCE_DIR}/src/zlib-1.2.11"
"${CMAKE_BINARY_DIR}/src/zlib-1.2.11"
)
@@ -723,7 +723,7 @@ add_library (mongoc_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
set_target_properties (mongoc_shared PROPERTIES CMAKE_CXX_VISIBILITY_PRESET hidden)
target_link_libraries (mongoc_shared PRIVATE ${LIBRARIES} PUBLIC ${BSON_LIBRARIES})
target_include_directories (mongoc_shared BEFORE PUBLIC ${MONGOC_INTERNAL_INCLUDE_DIRS})
-target_include_directories (mongoc_shared PRIVATE ${PRIVATE_ZLIB_INCLUDES})
+target_include_directories (mongoc_shared PRIVATE ${ZLIB_INCLUDE_DIRS})
target_include_directories (mongoc_shared PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
target_include_directories (mongoc_shared PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
@@ -765,7 +765,7 @@ if (MONGOC_ENABLE_STATIC_BUILD)
message ("Adding -fPIC to compilation of mongoc_static components")
endif ()
target_include_directories (mongoc_static BEFORE PUBLIC ${MONGOC_INTERNAL_INCLUDE_DIRS})
- target_include_directories (mongoc_static PRIVATE ${PRIVATE_ZLIB_INCLUDES})
+ target_include_directories (mongoc_static PRIVATE ${ZLIB_INCLUDE_DIRS})
target_include_directories (mongoc_static PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
target_include_directories (mongoc_static PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")