Skycoder42 / QHotkey

A global shortcut/hotkey for Desktop Qt-Applications
BSD 3-Clause "New" or "Revised" License
548 stars 161 forks source link

Windows: linking client to shared library fails when QHotkey is compiled with CMake and MSVC #58

Closed dbermond closed 2 years ago

dbermond commented 2 years ago

When building QHotkey as a shared library on Windows with CMake and MSVC 2019 (-DBUILD_SHARED_LIBS=ON), compiling a client application leads to the following error:

main.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const QHotkey::staticMetaObject" (?staticMetaObject@QHotkey@@2UQMetaObject@@B) referenced in function main

First of all, QHotkeys's CMakeLists.txt needs a fix to correctly build the shared library on Windows under MSVC. It lacks the QHOTKEY_LIB and QHOTKEY_LIB_BUILD definitions (referenced here), and in this way it will not produce the qhotkey.lib file, but only the qhotkey.dll one. Without this, a client application will not even pass the CMake step, telling that the imported qhotkey target references the file qhotkey.lib, and that it was not found.

So, we need to add something like add_definitions(-DQHOTKEY_LIB=1 -DQHOTKEY_LIB_BUILD=1) to QHotkeys's CMakeLists.txt in order to MSVC to correctly generate the qhotkey.lib. I've not sent any pull request because it's not sufficient to fix the issue.

After this, compiling a client application fails with the link error above. Like the simple application example from the README file.

On Windows, I'm using the classic CMake build steps like on the README, with cmake <args>, cmake --build and cmake --install. No fancy compiler flags.

This is the CMakeLists.txt that I'm using for the client application:

cmake_minimum_required(VERSION 3.1)
project(qhotkey_example VERSION 1.0.0 LANGUAGES CXX)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
find_package(QHotkey REQUIRED)
add_executable(qhotkey_example main.cpp)
target_link_libraries(qhotkey_example PRIVATE Qt5::Core Qt5::Widgets qhotkey)

Linking from a static library compiled with CMake and MSVC 2019 works fine.

Not a Windows guy here, and maybe I'm doing something wrong, but that's what I could gather about this.

dbermond commented 2 years ago

By following the Qt documentation, I created a minimal CMake based Qt library whose clients successfully links to the shared library under Windows also with CMake and MSVC 2019.

By implementing the changes here, it still does not work. I could not yet figure out what is causing the problem. It looks like that some export is missing.

Shatur commented 2 years ago

Interesting. Could you to set WINDOWS_EXPORT_ALL_SYMBOLS in QHotkey's CMakeLists.txt?

dbermond commented 2 years ago

Setting the WINDOWS_EXPORT_ALL_SYMBOLS property on QHotkey's CMakeLists.txt does not work for me. At the same way, using the CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS variable does not work either. I have tried it before. I also think that this would not be the optimal way to solve this issue.

Anyway, I found what's causing this. I will send a pull request with the fix.

Shatur commented 2 years ago

Anyway, I found what's causing this. I will send a pull request with the fix.

Okay, great, looking forward to it!