如何使用所需的第 3 方库的自定义版本来编译 wxWidgets

问题描述 投票:0回答:2

假设我有 libTifflibpngzliblibjpeg。我如何告诉 wxWidgets 使用这些而不是系统上内置的或安装的?

我正在尝试将 wxWidgets 作为依赖项添加到我的 CMake 项目中,作为回报,该项目已经依赖并构建(使用

ExternalProject
)上述库。我不想混合版本(如果我使用内置版本或系统上已安装的版本,这必然会发生)。我还希望将所有依赖项与系统上已有的依赖项分开,以便于分发。

我已经检查了

setup.h.in
定义了预处理器宏的位置(此处:
wxUSE_ZLIB
wxUSE_LIBJPEG
wxUSE_LIBTIFF
wxUSE_LIBPNG
)。我还看到,在
<root of wxWidgets git repo>/build/cmake/lib/
内部,我有各种
*.cmake
包含处理不同第三方依赖项的文件。

然而,我很困惑我需要传递给我的

cmake
命令才能将 wxWidgets 配置为从特定位置使用上述库的版本。我已经定义了
PNG_LIBRARIES
JPEG_LIBRARIES
等(指向
*.lib
(在 Windows 上)或
*.so
(在 Linux 上)文件所在的位置。此外,我还相应地定义了
..._INCLUDE_DIRS

目前这是我的外部项目描述的状态(

...
是有效路径的占位符)

ExternalProject_Add(wxWidgets
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/deps/wxwidgets
    
    INSTALL_DIR ...
    DOWNLOAD_DIR ""
    TMP_DIR ...
    STAMP_DIR ...
    LOG_DIR ...
    BINARY_DIR ...
    SOURCE_DIR ...

    INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "Skipping install step for dependency libzmq"
    INSTALL_PREFIX ...

    BUILD_ALWAYS OFF

    # For build configurations see https://wiki.wxwidgets.org/WxWidgets_Build_Configurations
    # Also setup.h.in and <wxWidgets_root>/build/cmake/libs
    CMAKE_ARGS
        "-DCMAKE_BUILD_TYPE=Release"
        "-DBUILD=release"
        "-DSHARED=1"
        "-DMONOLITHIC=1"
        "-DwxBUILD_TESTS=OFF"
        "-DwxBUILD_DEMOS=OFF"
        "-DwxBUILD_BENCHMARKS=OFF"
        "-DwxUSE_GUI=1"                               # <---- forces dependency requirement for PNG, ZLIB, JPEG, TIFF
        "-DwxUSE_OPENGL=1"
        "-DwxUSE_ZLIB=sys"
        "-DZLIB_LIBRARY=${ZLIB_LIBRARIES}"            # <---- from another ExternalProject
        "-DZLIB_INCLUDE_DIRS=${ZLIB_INCLUDE_DIRS}"    # <---- from another ExternalProject
        "-DwxUSE_LIBJPEG=sys"
        "-DJPEG_LIBRARY=${JPEG_LIBRARIES}"            # <---- from another ExternalProject
        "-DJPEG_INCLUDE_DIR=${JPEG_INCLUDE_DIRS}"     # <---- from another ExternalProject
        "-DwxUSE_LIBPNG=sys"
        "-DPNG_LIBRARY=${PNG_LIBRARIES}"              # <---- from another ExternalProject
        "-DPNG_PNG_INCLUDE_DIR=${PNG_INCLUDE_DIRS}"   # <---- from another ExternalProject
        "-DwxUSE_LIBTIFF=builtin" # TODO Add libTiff to external project dependencies and use here
)

我基本上所做的就是最初把东西放在

OFF
上。 wxWidgets 的配置足够智能,可以警告我(因为我包含了
wxUSE_GUI
,它需要这些库)相应的库丢失了,它将使用内置库代替。从那里我发现了它实际上正在寻找哪些变量,并通过将相应库的
wxUSE_...
设置为
sys
。现在我已经接近配置构建了:

1>------ Rebuild All started: Project: ZERO_CHECK, Configuration: Debug x64 ------
1>Checking Build System
2>------ Rebuild All started: Project: wxWidgets, Configuration: Debug x64 ------
2>Creating directories for 'wxWidgets'
2>Building Custom Rule C:/Users/.../Documents/.../CMakeLists.txt
2>No download step for 'wxWidgets'
2>No update step for 'wxWidgets'
2>No patch step for 'wxWidgets'
2>Performing configure step for 'wxWidgets'
2>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19042.
2>-- cotire 1.8.0 loaded.
2>-- Found ZLIB: C:/Users/.../CMakeBuilds/ef5b5ada-ee42-7735-988a-ae37c735ccff/build/deps/build/zlib/Debug/zlibd.lib (found version "1.2.11")
2>-- Found JPEG: C:/Users/rcr-aa/CMakeBuilds/ef5b5ada-ee42-7735-988a-ae37c735ccff/build/deps/build/jpeg/Debug/jpeg.lib (found version "90")
2>-- Found PNG: C:/Users/.../CMakeBuilds/ef5b5ada-ee42-7735-988a-ae37c735ccff/build/deps/build/png/Debug/libpng16d.lib (found version "1.6.38.git")
2>-- Which libraries should wxWidgets use?
2>    wxUSE_STL:      OFF      (use C++ STL classes)
2>    wxUSE_REGEX:    builtin  (enable support for wxRegEx class)
2>    wxUSE_ZLIB:     sys      (use zlib for LZW compression)
2>    wxUSE_EXPAT:    builtin  (use expat for XML parsing)
2>    wxUSE_LIBJPEG:  sys      (use libjpeg (JPEG file format))
2>    wxUSE_LIBPNG:   sys      (use libpng (PNG image format))
2>    wxUSE_LIBTIFF:  builtin  (use libtiff (TIFF file format))
2>    wxUSE_LIBLZMA:  OFF      (use liblzma for LZMA compression)
2>
2>-- Configured wxWidgets 3.1.6 for Windows-10.0.19042
2>    Min OS Version required at runtime:                Windows Vista / Windows Server 2008 (x64 Edition)
2>    Which GUI toolkit should wxWidgets use?            msw
2>    Should wxWidgets be compiled into single library?  OFF
2>    Should wxWidgets be linked as a shared library?    ON
2>    Should wxWidgets support Unicode?                  ON
2>    What wxWidgets compatibility level should be used? 3.0
2>-- Configuring done

环顾四周时,我还检查了我想要使用自定义版本的库的

*.cmake
文件,以确认需要设置的内容。 zlib 的示例如下所示:

#############################################################################
# Name:        build/cmake/lib/zlib.cmake
# Purpose:     Use external or internal zlib
# Author:      Tobias Taschner
# Created:     2016-09-21
# Copyright:   (c) 2016 wxWidgets development team
# Licence:     wxWindows licence
#############################################################################

if(wxUSE_ZLIB STREQUAL "builtin")
    # TODO: implement building zlib via its CMake file, using
    # add_subdirectory or ExternalProject_Add
    wx_add_builtin_library(wxzlib
        src/zlib/adler32.c
        src/zlib/compress.c
        src/zlib/crc32.c
        src/zlib/deflate.c
        src/zlib/gzclose.c
        src/zlib/gzlib.c
        src/zlib/gzread.c
        src/zlib/gzwrite.c
        src/zlib/infback.c
        src/zlib/inffast.c
        src/zlib/inflate.c
        src/zlib/inftrees.c
        src/zlib/trees.c
        src/zlib/uncompr.c
        src/zlib/zutil.c
    )
    if(WIN32)
        # Define this to get rid of many warnings about using open(),
        # read() and other POSIX functions in zlib code. This is much
        # more convenient than having to modify it to avoid them.
        target_compile_definitions(wxzlib PRIVATE _CRT_NONSTDC_NO_WARNINGS)
    endif()
    set(ZLIB_LIBRARIES wxzlib)                           # <----------- set via -DZLIB_LIBRARIES in my ExternalProject 
    set(ZLIB_INCLUDE_DIRS ${wxSOURCE_DIR}/src/zlib)      # <----------- set via -DZLIB_INCLUDE_DIRS in my ExternalProject
elseif(wxUSE_ZLIB)
    find_package(ZLIB REQUIRED)
endif()

我最大的问题是,我显然必须为每个第 3 方依赖项设置配置(在我的例子中,将所有其他依赖项设置为

builtin

2>CMake Error at build/cmake/functions.cmake:591 (add_library):
2>  Cannot find source file:
2>
2>    C:/Users/.../Documents/.../deps/wxwidgets/3rdparty/pcre/src/pcre2_auto_possess.c
2>
2>  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
2>  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
2>Call Stack (most recent call first):
2>  build/cmake/lib/regex.cmake:13 (wx_add_builtin_library)
2>  build/cmake/lib/CMakeLists.txt:28 (include)
2>
2>
2>CMake Error at build/cmake/functions.cmake:591 (add_library):
2>  Cannot find source file:
2>
2>    C:/Users/.../Documents/.../deps/wxwidgets/src/expat/expat/lib/xmlparse.c
2>
2>  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
2>  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
2>Call Stack (most recent call first):
2>  build/cmake/lib/expat.cmake:13 (wx_add_builtin_library)
2>  build/cmake/lib/CMakeLists.txt:28 (include)
2>
2>
2>CMake Error at build/cmake/functions.cmake:591 (add_library):
2>  Cannot find source file:
2>
2>    C:/Users/.../Documents/.../deps/wxwidgets/src/tiff/libtiff/tif_win32.c
2>
2>  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
2>  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
2>Call Stack (most recent call first):
2>  build/cmake/lib/tiff.cmake:19 (wx_add_builtin_library)
2>  build/cmake/lib/CMakeLists.txt:28 (include)
2>
2>
2>CMake Error at build/cmake/functions.cmake:591 (add_library):
2>  No SOURCES given to target: wxregex
2>Call Stack (most recent call first):
2>  build/cmake/lib/regex.cmake:13 (wx_add_builtin_library)
2>  build/cmake/lib/CMakeLists.txt:28 (include)
2>
2>
2>CMake Error at build/cmake/functions.cmake:591 (add_library):
2>  No SOURCES given to target: wxexpat
2>Call Stack (most recent call first):
2>  build/cmake/lib/expat.cmake:13 (wx_add_builtin_library)
2>  build/cmake/lib/CMakeLists.txt:28 (include)
2>
2>
2>CMake Error at build/cmake/functions.cmake:591 (add_library):
2>  No SOURCES given to target: wxtiff
2>Call Stack (most recent call first):
2>  build/cmake/lib/tiff.cmake:19 (wx_add_builtin_library)
2>  build/cmake/lib/CMakeLists.txt:28 (include)
2>
2>
2>CMake Generate step failed.  Build files cannot be regenerated correctly.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(209,5): error MSB6006: "cmd.exe" exited with code 1.
2>Done building project "wxWidgets.vcxproj" -- FAILED.

我可以将所有其余依赖项设置为

builtin
吗?

c++ cmake wxwidgets
2个回答
1
投票

首先,您必须进行设置,以便可以通过 CMake(使用其

find_package()
)或配置(使用
pkg-config
)找到现有库。

其次,使用 CMake,我认为您需要显式设置选项,例如

wxUSE_LIBPNG
sys
。使用configure构建时,这已经是默认设置,因为configure将首先查找系统库,并且只有在找不到系统库时才使用内置库,但您也可以使用其命令行选项,例如
 --with-libpng=sys
强制使用系统库(如果未找到,则配置失败)。


0
投票

我最近需要使用自定义 zlib 和 libpng 构建 wxWidgets 的交叉编译(在针对 Windows 主机的 Linux 上构建)副本。 我还想让它保持独立,避免任何可能影响其他交叉编译项目构建的“系统”更改。

我没有使用 CMake。 我要分享的只是从 Linux 命令行运行的命令配方,这些命令使用自定义 zlib 和 libpng 构建 wxWidgets。 不确定这是否真的满足您的 CMake 请求,但也许有关构建 wxWidgets 的一些细节可能会有所帮助?

首先是构建 zlib。 它的配置脚本没有用于交叉编译的

--host
选项,但在命令行上设置CC是可行的。
--prefix
选项是安装自定义构建 zlib 和 libpng 的“本地系统”文件夹。

cd ~/wxwidgets/
tar -xf zlib-1.3.1.tar.xz
cd zlib-1.3.1
CC=i686-w64-mingw32-gcc ./configure --prefix=/home/paul/wxwidgets/3.2.6.mingw-sys --static
make -j24
make install

构建完成后,您将在“本地系统”位置中看到一个包含 zlib.h 的 include 文件夹,以及一个包含 libz.a 的 lib 文件夹。

然后要构建libpng,你可以使用

--host=i686-w64-mingw32
来告诉它交叉编译。 但它还需要在配置之前在命令行上设置变量
CPPFLAGS
LDFLAGS
,告诉它在哪里可以找到你刚刚构建的“本地系统”libz。

cd ~/wxwidgets/
tar -xf libpng-1.6.44.tar.xz
cd libpng-1.6.44
CPPFLAGS=-I/home/paul/wxwidgets/3.2.6.mingw-sys/include LDFLAGS=-L/home/paul/wxwidgets/3.2.6.mingw-sys/lib ./configure --prefix=/home/paul/wxwidgets/3.2.6.mingw-sys --host=i686-w64-mingw32 --disable-shared
make -j24
make install

完成后,您将看到 libpng 标头和库存档文件已添加到本地系统文件夹中。

然后就可以编译wxWidgets了。 在配置之前,它需要在命令行上设置相同的变量

CPPFLAGS
LDFLAGS
,以便它可以找到“本地系统”zlib和libpng。 然后使用
--with-zlib=sys
--with-libpng=sys
告诉它使用“系统”提供的库。 但是因为你给了它这些
CPPFLAGS
LDFLAGS
变量,wxWidgets 会发现你的自定义构建副本作为“系统”库。

wxWidgets 也使用

--host
进行交叉编译,所以不要试图通过设置 CC 来强制它。 这是命令行配方。

cd ~/wxwidgets/
tar -xf wxWidgets-3.2.6.tar.bz2
cd wxWidgets-3.2.6
CPPFLAGS=-I/home/paul/wxwidgets/3.2.6.mingw-sys/include LDFLAGS=-L/home/paul/wxwidgets/3.2.6.mingw-sys/lib ./configure --prefix=/home/paul/wxwidgets/3.2.6.mingw --host=i686-w64-mingw32 --disable-shared --with-cxx=11 --disable-debug --enable-optimise --with-zlib=sys --with-libpng=sys --without-libtiff -with-libjpeg=builtin --disable-compat28 --disable-compat30
make -j 24
make install

完成后,您将拥有一个 wxWidgets 库,它使用您的自定义构建 zlib 和 libpng。 您可以通过运行 wx-config 进行确认,例如

/home/paul/wxwidgets/3.2.6.mingw/bin/wx-config --libs
,您会看到它给出了一个 -L 库文件夹,其中包含您构建 zlib 和 libpng 的“本地系统”位置,当然还有
-lz
-lpng
实际使用它们。

© www.soinside.com 2019 - 2024. All rights reserved.