3

I'm working on a Qt project using the Network package (in particular QTcpSocket). I can easily compile it using qmake, but I fail to do so using cmake (which I need for cross compilation), due to some undefined references regarding the Network package.

My CmakeLists contains, amongst others, the following lines (with ... being placeholders for other things):

find_package(Qt5 COMPONENTS Network REQUIRED)
qt5_use_modules(... Network ...)
target_link_libraries(... Qt5::Network ...)

I thought those are all that is required, but apparently not so.

The undefined reference errors I get when linking are a huge lists, so I will only show a small exempt, unless somebody requires more:

/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qsslsocket_openssl.o):qsslsocket_openssl.cpp:(.text+0x139c): undefined reference to `__imp_CertCreateCertificateContext'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qsslsocket_openssl.o):qsslsocket_openssl.cpp:(.text+0x1441): undefined reference to `__imp_CertGetCertificateChain'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qsslsocket_openssl.o):qsslsocket_openssl.cpp:(.text+0x14c2): undefined reference to `__imp_CertFreeCertificateChain'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qsslsocket_openssl.o):qsslsocket_openssl.cpp:(.text+0x14cb): undefined reference to `__imp_CertFreeCertificateContext'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x15): undefined reference to `ConvertInterfaceNameToLuidW'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x2e): undefined reference to `ConvertInterfaceLuidToIndex'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x5b): undefined reference to `ConvertInterfaceIndexToLuid'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x75): undefined reference to `ConvertInterfaceLuidToNameW'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0xec): undefined reference to `GetNetworkParams'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x1cd): undefined reference to `GetNetworkParams'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x243): undefined reference to `GetAdaptersAddresses'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x3c4): undefined reference to `ConvertInterfaceLuidToNameW'
/opt/mxe/usr/x86_64-w64-mingw32.static/qt5/lib/libQt5Network.a(qnetworkinterface_win.o):qnetworkinterface_win.cpp:(.text+0x91e): undefined reference to `GetAdaptersAddresses'

What did I miss? Are there some other modules I have to add? Is this problem related to me cross compiling using MXE (I didn't tag it as such for now)?

So far I found MXE - Undefined reference to Qt when cross compiling with cmake and mingw, which makes it seem like MXE might be the problem, but that one is not answered.

Aziuth
  • 3,652
  • 3
  • 18
  • 36
  • Are you sure `Qt5::Network` is correct? Not `Qt5Network`? – G.M. Aug 07 '20 at 10:45
  • 1
    @G.M. https://doc.qt.io/qt-5/qtnetwork-index.html at least uses `Qt5::Network` in the cmake example. – Aziuth Aug 07 '20 at 10:59
  • One important note, unless your CMake version is *older than* 2.8.11, you [shouldn't need to use](https://doc.qt.io/qt-5.9/cmake-manual.html#using-qt-5-with-cmake-older-than-2-8-11) the `qt5_use_modules` command. Consider removing it. – Kevin Aug 07 '20 at 12:20
  • 1
    @Aziuth Did you include directories of Network module into your project? Like that include_directories(Qt5Network_INCLUDE_DIRS). You can do it after find_package(...) – Maxim Skvortsov Aug 14 '20 at 11:41
  • 1
    @MaximSkvortsov Haven't done that yet, will try it out, thanks for the suggestion. – Aziuth Aug 14 '20 at 13:12
  • Was the problem solved? I've got the same issue – Mykola Tetiuk Jun 26 '22 at 18:46
  • 1
    @MykolaTetiuk Sorry, don't remember after that time. Since I haven't accepted anything here, I might have done the old solution of reinstalling everything. Might be that MXE has to be build with the right flag? But I'm guessing here. – Aziuth Jun 27 '22 at 07:04

1 Answers1

0

Qt's cmake module's dependencies must be messed up. You can manually link with Iphlpapi. Add target_link_libraries(... Iphlpapi) to the CMakeFile.txt and try linking again. You can easily find the necessary libraries by googling the name of the symbol. That will bring up the relevant MSDN page. When you scroll to the bottom, you'll see what library file needs to be linked to make that API available to your program.

The explicit link directive will be a no-op when the library is properly referenced by Qt's cmake module(s). You can also make this linking conditional on the target being Windows:

if (WIN32)
    target_link_libraries (mytarget Iphlpapi)
endif ()
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313