使用CMake在Windows上静态链接Qt

时间:2017-01-20 14:15:00

标签: windows qt cmake qt5 qmake

我正在开发一个在Linux上开发的Qt项目,但也有一个静态链接的Windows版本。我可以使用相同的CMakeLists.txt文件在Linux和Windows上构建它。它剥离到:

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})

但我还没有设法通过CMake在Windows上进行静态链接构建。当我手动设置相应的include dirs时,所有构建都很好,但最后我得到了一个链接器错误:

[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

这就是为什么我创建了一个qmake .pro文件来进行静态构建。这个删除到:

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

使用qmake(来自静态Qt),我可以毫无问题地进行静态构建。所以我的静态Qt构建很好,这是一个CMake问题。

这里有什么问题?谢谢大家的帮助!

1 个答案:

答案 0 :(得分:1)

我认为你是this bug的受害者(尚未解决)。我不是100%肯定会出现什么问题,但你可以在评论中找到一些想法。