QT + meson 编译方式

预备工作

cross.ini 样例

以下用的mingw64编辑器版本为x86_64-12.2.0-release-posix-seh-rt_v10-rev0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[constants]
cross_root = 'E:/cross-tools/mingw64/bin/'
qt_root = 'E:/Qt/5.15.6/qtbase/'
target = ''

[binaries]
c = cross_root + target + 'gcc'
cpp = cross_root + target + 'g++'
ar = cross_root + target + 'ar'
strip = 'strip'
qmake = qt_root + '/bin/qmake'

[host_machine]
system = 'windows'
cpu_family = 'x86'
cpu = 'i386'
endian = 'little'

[properties]
qt_root = qt_root

meson.build 编译

动态编译

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
project('qtdemo', 'cpp',
  version : '0.1',
  default_options : ['warning_level=3', 'cpp_std=c++17'])

qt5_mod = import('qt5')
qt5_dep = dependency('qt5', modules: ['Core', 'Widgets'])
qt5_path = 'library/qt/'
# qt library
subdir(qt5_path)

processed = qt5_mod.preprocess(
  moc_headers : qt5_path + 'include/mainWindow.h',   # Only headers that need moc should be put here
  moc_sources : qt5_path + 'source/mainWindow.cpp', # must have #include"moc_helperFile.cpp"
  ui_files    : qt5_path + 'mainWindow.ui',
)

INCLUDE_HDR = []
INCLUDE_HDR += include_directories('include')
INCLUDE_HDR += QT_HDR

MHF_SRC = []
MHF_SRC += 'main.cpp'
MHF_SRC += processed
MHF_SRC += QT_SRC

if build_machine.system() == 'windows'
    CFLAGS = ['-DWINDOWS']
else
    CFLAGS = []
endif

CFLAGS += '-w'

# generate qtdemo
executable('qtdemo',
           sources: MHF_SRC,
           include_directories: INCLUDE_HDR,
           c_args : '',
           cpp_args : CFLAGS,
           link_args: '',
           dependencies: [qt5_dep]
           )

静态编译

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
project('qtdemo', 'cpp',
  version : '0.1',
  default_options : ['warning_level=3',
                     'buildtype=release',
                     'cpp_std=c++17'])

cxx = meson.get_compiler('cpp')
qt5_mod = import('qt5')
qt5_dep = dependency('qt5', modules: ['Core', 'Widgets', 'Gui'], static: true)
qt5_path = '3rdparty/qt/'

# qt library
subdir(qt5_path)

MHF_HDR = []
MHF_HDR += include_directories('include')
MHF_HDR += QT_HDR

MHF_SRC = []
MHF_SRC += 'main.cpp'
MHF_SRC += QT_SRC
MHF_SRC += qt5_mod.compile_ui(sources : [qt5_path + 'mainWindow.ui'])
MHF_SRC += qt5_mod.compile_moc(sources: [qt5_path + 'source/mainWindow.cpp'],
                               headers: [qt5_path + 'include/mainWindow.h'])


LIBS = []
LIBS += qt5_dep
LIBS += cxx.find_library('Qt5Core', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('qtpcre2', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('QT5Widgets', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('Qt5Gui', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('qtlibpng', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('qtharfbuzz', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
# plugins
LIBS += cxx.find_library('qwindows', dirs: meson.get_external_property('qt_root') + 'plugins/platforms', static: true)
LIBS += cxx.find_library('Qt5FontDatabaseSupport', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('qtfreetype', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('Qt5WindowsUIAutomationSupport', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('Qt5EventDispatcherSupport', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)
LIBS += cxx.find_library('Qt5ThemeSupport', dirs: meson.get_external_property('qt_root') + 'lib/', static: true)



if build_machine.system() == 'windows'
    CXXFLAGS = ['-DWINDOWS']
else
    CXXFLAGS = []
endif

CXXFLAGS += '-w'
CXXFLAGS += '-static'

LD_FLAGS = []
LD_FLAGS += '-lz'
LD_FLAGS += '-lwsock32'
LD_FLAGS += '-lopengl32'
# GetFileVersionInfoSizeW
LD_FLAGS += '-lversion'
# __imp_GetUserProfileDirectoryW
LD_FLAGS += '-luserenv'
# NetShareEnum
LD_FLAGS += '-lnetapi32'
# __imp_timeSetEvent
LD_FLAGS += '-lwinmm'
# DwmGetWindowAttribute
LD_FLAGS += '-ldwmapi'
# ImmGetOpenStatus
LD_FLAGS += '-limm32'
# WTSFreeMemory
LD_FLAGS += '-lwtsapi32'



# generate qtdemo
executable('qtdemo',
           sources: MHF_SRC,
           include_directories: MHF_HDR,
           c_args : '',
           cpp_args : CXXFLAGS,
           link_args : LD_FLAGS,
           dependencies: LIBS,
           gui_app : true
           )

一些笔记

去掉 meson 最后指定的gui_app : true可以在控制台看到 QT 程序不能启动的错误信息

参考链接