Search code examples
qtcmakevcpkg

Qt5 build as shared library on macos using cmake and vcpkg


Building the Qt library as shared library using cmake and vcpkg on the macOS Monterey(version 12.2.1) fails. Using the default VCPKG_LIBRARY_LINKAGE though (i.e. static), builds the library and makes it usable successfully. The issue only happens when I try to build it as a shared library.

The full toolset that I'm using:

  • macOS Monterey - 12.2.1 (21D62).

  • Xcode 13.3(Build version 13E113).

  • cmake version 3.22.3.

  • vcpkg tag - 2022.03.10(af2287382b1991dbdcb7e5112d236f3323b9dd7a).

  • Qt version set implicitly by this vcpkg tag - 5.15.3.

On my PreLoad.cmake file - the following code is included:

...
set(VCPKG_TARGET_TRIPLET "x64-osx")
set(VCPKG_HOST_TRIPLET "x64-osx")
set(CMAKE_OSX_ARCHITECTURES x86_64)
...

When running cmake -DCMAKE_BUILD_TYPE=Debug .. the Qt library (eventually) builds successfully. However, when I'm trying to build it as a shared library to be later dynamically linked to my code by adding this snippet to my PreLoad.cmake:

if((${PORT} MATCHES "qt5-base") OR (${PORT} MATCHES "qt5-tools"))
    set(VCPKG_LIBRARY_LINKAGE dynamic)
else()
    set(VCPKG_LIBRARY_LINKAGE static)
endif()

the vcpkg install process fails with this error:

-- Running vcpkg install - failed
CMake Error at vcpkg/scripts/buildsystems/vcpkg.cmake:834 (message):
  vcpkg install failed.  See logs for more information:
  /Users/avibiton/Dev/main/build/vcpkg-manifest-install.log
Call Stack (most recent call first):
  /opt/homebrew/Cellar/cmake/3.22.3/share/cmake/Modules/CMakeDetermineSystem.cmake:124 (include)
  CMakeLists.txt:30 (project)

-- Configuring incomplete, errors occurred!

Inspecting the vcpkg-manifest-install.log file and from there into vcpkg/buildtrees/qt5-base/config-x64-osx-dbg-err.log file, I came across this error message:

ERROR: debug-only framework builds are not supported. Configure with -no-framework if you want a pure debug build.

So I went to this file vcpkg/ports/qt5-base/portfile.cmake and tried to append the -no-framework flag to the CORE_OPTIONS variable like this:

...
## 3rd Party Libs
list(APPEND CORE_OPTIONS
    -system-zlib
    -system-libjpeg
    -system-libpng
    -system-freetype
    -system-pcre
    -system-doubleconversion
    -system-sqlite
    -system-harfbuzz
    -icu
    -no-vulkan
    -no-angle # Qt does not need to build angle. VCPKG will build angle!
    -no-glib
    -no-framework # This is my change
    )
...

But this change turn out to be a rabbit-hole of it self :(

If anyone has succeeded building Qt as a shared library on macos using cmake and vcpkg, I'll be grateful for any help !


Solution

  • If you don't need the Qt and other vcpkg dependencies built in debug configuration you could also set the VCPKG_BUILD_TYPE in your PreLoad.cmake

    ...    
    set(VCPKG_BUILD_TYPE release)
    ...
    

    If your other ports need to be built in debug configuration, you could probably also set this on a per port basis as you did with VCPKG_LIBRARY_LINKAGE

    ...
    set(VCPKG_LIBRARY_LINKAGE static)
    if(PORT MATCHES "qt5-")
        set(VCPKG_LIBRARY_LINKAGE dynamic)
        set(VCPKG_BUILD_TYPE release)
    endif()
    ...
    

    As this setting will most likely break Windows builds, you should rather use a custom triplet that will only be used for macOS builds instead of a PreLoad.cmake that is potentially used for all platforms, including Windows. You can find an example for how to do this in the vcpkg docs https://vcpkg.io/en/docs/examples/overlay-triplets-linux-dynamic.html.