MongoC -DENABLE_ZLIB=SYSTEM not working

@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