Can CMake require static libraries (e.g., ZLIB)?

浪尽此生 提交于 2020-01-05 07:24:29

问题


It has been years since I worked in C++, and I've never used CMake before. I'm trying to compile a program called ngmlr, which uses CMake. It worked seamlessly on other systems I tried to build it on. This time around, CMake finds ZLIB (Found ZLIB: /usr/lib64/libz.so (found version "1.2.3")), as required by ngmlr, but the subsequent make fails with ld: cannot find -lz.

I think I know what's happening: CMake found the dynamic ZLIB library (libz.so), but the CMakeLists.txt file requires static (I found the following option in the file: option(STATIC "Build static binary" ON)). As far as I can tell, the static library (libz.a) is missing on this machine. It's not in the same /usr/lib64 directory as libz.so. locate is not available.

Questions:

  1. Does that seem correct?
  2. For education, assuming this is the problem, can you force CMake to look specifically for static ZLIB? e.g., since the developer required static, it would have been nice to immediately know the missing static library was the problem, rather than the embarrassingly long amount of time it took me to figure it out.

I've looked extensively for a clear answer to both, but didn't find anything conclusive (e.g., Force cmake to use static libraries).

UPDATE I did confirm that the problem is that ld could not find the static library. Now I'm particularly interested to know if the developer can tell CMake to throw an error if the static libraries are not present, and save someone else.

cmake version 2.8.8


回答1:


  1. Yes
  2. Generally speaking it is up to Find-module authors. Some modules have special "static" option, others do not. Particularly Zlib module has not. That's why cmake global variable is set in subdirectory src/CMakeLists.txt: SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a"). But it is invoked after find_package( ZLIB REQUIRED ) command. Looks like a bug.

Now I'm particularly interested to know if the developer can tell CMake to throw an error if the static libraries are not present, and save someone else.

REQUIRED means that error will be thrown if package was not found. In your case it should be thrown if you move SET(CMAKE_FIND_LIBRARY_SUFFIXES before find_package

Perhaps you can build your project if disable STATIC option

cmake -G"Unix Makefiles" _PATH_ -DSTATIC=OFF



回答2:


Your CMakeLists.txt probably has this somewhere:

find_library(ZLIB z)

You can replace it with:

find_library(ZLIB libz.a)


来源:https://stackoverflow.com/questions/44731248/can-cmake-require-static-libraries-e-g-zlib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!