Static linking against Qt on Windows using CMake

烈酒焚心 提交于 2021-01-29 03:17:29

问题


I'm working on a Qt project that is developed on Linux, but also has a statically linked Windows build. I can build it on Linux and Windows using the same CMakeLists.txt file. It strips down to:

project(muckturnier)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_REQUIRED TRUE)
find_package(Qt5 COMPONENTS Widgets Sql)
include_directories(${Qt5Widgets_INCLUDES} ${Qt5Sql_INCLUDES})
set(CMAKE_AUTOMOC ON)
set(muckturnier_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/SomeCode.cpp)
add_executable(muckturnier ${muckturnier_SRCS})
target_link_libraries(muckturnier ${Qt5Widgets_LIBRARIES} ${Qt5Sql_LIBRARIES})

But I didn't manage to do a statically linked build on Windows via CMake yet. When I manually set the respective include dirs, all builds fine, but I get a linker error in the end:

[100%] Linking CXX executable muckturnier.exe
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x228d):
undefined reference to `hb_buffer_create'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x22a4):
undefined reference to `hb_buffer_set_unicode_funcs'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x22b7):
undefined reference to `hb_buffer_pre_allocate'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x22bf):
undefined reference to `hb_buffer_allocation_successful'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x23ef):
undefined reference to `hb_buffer_clear_contents'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x241f):
undefined reference to `hb_buffer_add_utf16'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x2432):
undefined reference to `hb_buffer_set_segment_properties'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x243a):
undefined reference to `hb_buffer_guess_segment_properties'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x2455):
undefined reference to `hb_buffer_set_flags'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x2555):
undefined reference to `hb_shape_full'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x2578):
undefined reference to `hb_buffer_get_length'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x258c):
undefined reference to `hb_buffer_destroy'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x264a):
undefined reference to `hb_buffer_get_glyph_infos'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x2662):
undefined reference to `hb_buffer_get_glyph_positions'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x28f8):
undefined reference to `hb_buffer_reverse'
C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o):qtextengine.cpp:(.text+0x2941):
undefined reference to `hb_buffer_destroy'
C:/Qt/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w
64-mingw32/bin/ld.exe: C:/Qt/5.6_static/lib/libQt5Gui.a(qtextengine.o): bad relo
c address 0x7a in section `.text$_ZN7QVectorIN11QTextLayout11FormatRangeEEaSERKS
2_[__ZN7QVectorIN11QTextLayout11FormatRangeEEaSERKS2_]'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\muckturnier.dir\build.make:424: recipe for target 'muckturnier.exe' f
ailed
mingw32-make[2]: *** [muckturnier.exe] Error 1
CMakeFiles\Makefile2:141: recipe for target 'CMakeFiles/muckturnier.dir/all' fai
led
mingw32-make[1]: *** [CMakeFiles/muckturnier.dir/all] Error 2
makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

This is why I created a qmake .pro file to do the static build. This one strips down to:

QMAKE_CXXFLAGS += -std=c++11
CONFIG += qt
CONFIG += static
QT += widgets
QT += sql
HEADERS += SomeCode.h
SOURCES += SomeCode.cpp
TARGET = muckturnier

Using qmake (from the static Qt), I can do the static build without a problem. So my static Qt build is just fine, and it's a CMake problem.

What's wrong here? Thanks for all help!


回答1:


I think you're a victim of this bug (still unsolved). I'm not 100% sure of what's going wrong, but you can find some ideas in the comments.



来源:https://stackoverflow.com/questions/41765673/static-linking-against-qt-on-windows-using-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!