你可以通过CMake如何添加zlib?

huangapple go评论68阅读模式
英文:

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 设置此提示变量。

另请参阅

英文:

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 &lt;wherever you cloned zlib to&gt;
cmake -S . -B build &lt;other options you want&gt; -DCMAKE_INSTALL_PREFIX=&lt;absolute path to where you want to install to&gt;
cmake --build build &lt;other options you want&gt;
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 &lt;wherever you cloned zlib to&gt;
cmake -S . -B build &lt;other options you want&gt; -DCMAKE_INSTALL_PREFIX=&lt;absolute path to where you want to install to&gt;
cmake --build build --config Release &lt;other options you want&gt;
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 &lt;where you installed zlib to&gt;)
# set(ZLIB_USE_STATIC_LIBS YES) # uncomment if cmake_minimum_required &gt;= 3.24 and you want to find zlibstatic instead of zlib
find_package(zlib &lt;other arguments you want&gt;)
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

huangapple
  • 本文由 发表于 2023年5月17日 18:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定