xmake版本:xmake v2.8.5+master.8865866a4
Qt版本:5.14.2
编译器版本:Qt安装自带的MinGW 7.3 64
平台:Windows11(注意在Windows上qt的mingw只提供release版本的库链接,所以只能够构建到release版本)
xmake.lua
Lua
set_project("softchat")
add_rules("mode.debug", "mode.release")
target("client")
add_rules("qt.widgetapp")
add_frameworks("QtWebSockets")
add_files("client/**.cpp", "client/**.h")
add_includedirs("client", "client/gui")
if is_mode("debug") then
add_defines("DEBUG")
elseif is_mode("release") then
add_defines("RELEASE")
end
target_end()
target("server")
add_rules("qt.console")
add_files("server/**.cpp", "server/**.h")
add_frameworks("QtWebSockets", "QtSql")
if is_mode("debug") then
add_defines("DEBUG")
elseif is_mode("release") then
add_defines("RELEASE")
end
target_end()
构建之后报错:
bash
error: build\.objs\server\mingw\x86_64\release\server\server.cpp.obj:server.cpp:(.text+0x2a6): undefined reference to `__imp__ZN12QHostAddressC1ENS_14SpecialAddressE'
build\.objs\server\mingw\x86_64\release\server\server.cpp.obj:server.cpp:(.text+0x2c6): undefined reference to `__imp__ZN12QHostAddressD1Ev'
build\.objs\server\mingw\x86_64\release\server\server.cpp.obj:server.cpp:(.text+0x4be): undefined reference to `__imp__ZN12QHostAddressD1Ev'
build\.objs\server\mingw\x86_64\release\server\server.cpp.obj:server.cpp:(.text+0x10ef): undefined reference to `__imp__ZNK12QHostAddress8toStringEv'
build\.objs\server\mingw\x86_64\release\server\server.cpp.obj:server.cpp:(.text+0x1248): undefined reference to `__imp__ZN12QHostAddressD1Ev'
build\.objs\server\mingw\x86_64\release\server\server.cpp.obj:server.cpp:(.text+0x1373): undefined reference to `__imp__ZN12QHostAddressD1Ev'
collect2.exe: error: ld returned 1 exit status
项目结构什么的都不用注意,关键是这个项目用到了模块QtWebSockets。
报错信息大概说的是存在未定义的符号,在链接阶段出了问题。
一开始百思不得其解,后来看到报错信息里有QHostAddress,网上一查说是提供一个IP地址的Qt封装,随即联想到这或许和QtNetwork有关,然后把QtNetwork库链接到目标之中,果然就没有报错了。
正确的xmake.lua
Lua
set_project("softchat")
add_rules("mode.debug", "mode.release")
target("client")
add_rules("qt.widgetapp")
add_frameworks("QtNetwork", "QtWebSockets")
add_files("client/**.cpp", "client/**.h")
add_includedirs("client", "client/gui")
if is_mode("debug") then
add_defines("DEBUG")
elseif is_mode("release") then
add_defines("RELEASE")
end
target_end()
target("server")
add_rules("qt.console")
add_files("server/**.cpp", "server/**.h")
add_frameworks("QtNetwork", "QtWebSockets", "QtSql")
if is_mode("debug") then
add_defines("DEBUG")
elseif is_mode("release") then
add_defines("RELEASE")
end
target_end()
回头仔细想想,确实也是如此,作为Qt基础库的QtNetwork和作为扩展库的QtWebSockets,后者也应该是在前者的基础上开发的,因此,要想在Qt中使用任何和网络有关的库,需要先添加QtNetwork模块。
但另一个方面,同样的链接模块,cmake就可以顺利构建,想来是cmake底下偷偷帮你链接QtNetwork了。