英文:
How do I add zlib via CMake?
问题
You can tell CMake to use that folder as a library by adding the following line to your CMakeLists.txt file:
target_include_directories(your_target_name PRIVATE path/to/zlib/include)
Replace "your_target_name" with the name of your project target and "path/to/zlib/include" with the actual path to the zlib library's header files.
英文:
I have a project in c++ where I need to use zlib. Because I want it to be a self contained project I have downloaded the zlib source from their GitHub, and added it in a library folder.
How can I tell cmake to use that folder as a library?
答案1
得分: 0
以下是您要翻译的内容:
我认为你应该能够使用你选择的方法构建和安装 zlib(zlib 包括一个 Makefile 和一个 CMakeLists.txt)。例如:
cd <无论您将 zlib 克隆到何处>
cmake -S . -B build <其他选项> -DCMAKE_INSTALL_PREFIX=<要安装到的绝对路径>
cmake --build build <其他选项>
cmake --install build
请注意,通常可以使用 --prefix
参数 来执行 cmake --install
,但 zlib 使用了一些不太好的 CMake 实践,阻止了这种可能性,因此您必须设置 CMAKE_INSTALL_PREFIX
。
如果您使用的是多配置生成器,请确保您正在构建和安装的配置相匹配。不能安装尚未构建的内容。还请参阅构建 --config
参数和安装 --config
参数。例如:
cd <无论您将 zlib 克隆到何处>
cmake -S . -B build <其他选项> -DCMAKE_INSTALL_PREFIX=<要安装到的绝对路径>
cmake --build build --config Release <其他选项>
cmake --install build --config Release
根据您和您的构建用户的需求(基本上,如果您希望始终使您和您的构建用户克隆、构建和安装特定版本的 zlib,您可能会发现 ExternalProject 模块 在自动化上述过程中很有帮助)。
然后在您的 CMakeLists.txt 中使用 find_package
。CMake 提供了一个查找模块来查找 zlib 安装:https://cmake.org/cmake/help/latest/module/FindZLIB.html。例如:
set(ZLIB_ROOT <您安装 zlib 的位置>)
# set(ZLIB_USE_STATIC_LIBS YES) # 如果 cmake_minimum_required >= 3.24 并且您想找到 zlibstatic 而不是 zlib,请取消注释
find_package(zlib <其他参数>)
target_link_libraries(my_executable PRIVATE ZLIB::ZLIB)
如果您将 zlib 安装到项目目录内的目录中,请执行 set(ZLIB_ROOT ...)
。否则,建议使用配置命令中的 -D
设置此提示变量。
另请参阅
- https://cmake.org/cmake/help/latest/manual/cmake.1.html
- https://cmake.org/cmake/help/latest/command/find_package.html
英文:
I think you should be able to build and install zlib using your approach of choice (zlib includes a Makefile and a CMakeLists.txt). Ex.
cd <wherever you cloned zlib to>
cmake -S . -B build <other options you want> -DCMAKE_INSTALL_PREFIX=<absolute path to where you want to install to>
cmake --build build <other options you want>
cmake --install build
Note that usually you can use the --prefix
argument to cmake --install
, but zlib uses some not-so-great CMake practices that prevent that from being possible, so instead, you have to set CMAKE_INSTALL_PREFIX
.
If you are using a multi-config generator, make sure that the configurations you're building and installing match. You can't install something you haven't built yet. See also the build --config
argument, and the install --config
argument. Ex.
cd <wherever you cloned zlib to>
cmake -S . -B build <other options you want> -DCMAKE_INSTALL_PREFIX=<absolute path to where you want to install to>
cmake --build build --config Release <other options you want>
cmake --install build --config Release
Depending on your and your build-users' needs, (basically, if you're going to want to always have you and your build-users clone, build, and install a specific version of zlib, you may find the ExternalProject
module helpful in automating the above).
And then use find_package
in your CMakeLists.txt. CMake provides a find module to find zlib installations: https://cmake.org/cmake/help/latest/module/FindZLIB.html. Ex.
set(ZLIB_ROOT <where you installed zlib to>)
# set(ZLIB_USE_STATIC_LIBS YES) # uncomment if cmake_minimum_required >= 3.24 and you want to find zlibstatic instead of zlib
find_package(zlib <other arguments you want>)
target_link_libraries(my_executable PRIVATE ZLIB::ZLIB)
do set(ZLIB_ROOT ...)
if you install zlib to a directory inside your project directory. Otherwise, I advise to set this hint variable using -D
in the configuration command.
See also
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论