简体   繁体   English

如何使用 CMake 和 FindLibXml2 链接不需要 DLL 的静态版本的 LibXml2

[英]How can I use CMake and FindLibXml2 to link against a static version of LibXml2 that requires no DLL

I am modernizing our CMake build system and switching to static compilation of all dependencies so I can deploy the application as single binary.我正在现代化我们的 CMake 构建系统并切换到所有依赖项的静态编译,以便我可以将应用程序部署为单个二进制文件。 One of the dependencies is LibXml2 which is statically compiled (Environment MSVC 2019 x64 Native):依赖项之一是静态编译的 LibXml2(Environment MSVC 2019 x64 Native):

cscript configure.js iconv=no compiler=msvc cruntime=/MT debug=yes static=yes prefix=libxml
nmake Makefile.msvc libxml install

This generates the DLL win32\\libxml\\bin\\libxml2.dll and the LIB files win32\\libxml\\lib\\libxml2.lib and win32\\libxml\\lib\\libxml2_a.lib .这将生成 DLL win32\\libxml\\bin\\libxml2.dll和 LIB 文件win32\\libxml\\lib\\libxml2.libwin32\\libxml\\lib\\libxml2_a.lib

My CMake file looks like this:我的 CMake 文件如下所示:

find_package(LibXml2 REQUIRED)

add_executable(testapp WIN32)

target_sources(testapp
    PRIVATE
        Main.cpp
)

target_include_directories(testapp PUBLIC ${LIBXML_LIBRARIES})

target_link_libraries(testapp PRIVATE LibXml2::LibXml2)

Problem : It looks like the find module FindLibXml2 picks up the relocatable shared DLL, but not the static LIB archive.问题:看起来查找模块FindLibXml2提取可重定位的共享 DLL,而不是静态 LIB 存档。 Thus the application is linked against the dynamic library.因此,应用程序链接到动态库。

Question : How can I use the find module script, but link against the static version of LibXml2?问题:如何使用 find 模块脚本,但链接到 LibXml2 的静态版本? Is this even possible or do I have to write an own find script?这甚至可能还是我必须编写自己的查找脚本?

User @alex-reinking gave the important tip: Set the cached variable before calling the find module - and not afterwards.用户@alex-reinking 给出了重要提示:调用 find 模块之前设置缓存变量 - 而不是之后。

Because I don't want to hardcode the path (and can't because I have a debug and release build of LibXml2), I use find_library to find the static library (Note: I added the libxml directory via CMAKE_PREFIX_PATH ):因为我不想硬编码路径(并且不能因为我有 LibXml2 的调试和发布版本),我使用find_library来查找静态库(注意:我通过CMAKE_PREFIX_PATH添加了 libxml 目录):

find_library(STATIC_LIBXML2_LIBRARY NAMES libxml2_a)
message(STATUS "Found library: ${STATIC_LIBXML2_LIBRARY}")
set(LIBXML2_LIBRARY ${STATIC_LIBXML2_LIBRARY})
find_package(LibXml2 REQUIRED)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM