Building Qt project with Network using CMake causes undefined references - c++

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.

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

Related

How to fix undefined references after libtorch custom build?

I've built libtorch from source (as shared objects) with the purpose of slimming it down, but now when I try to link it into an application, I get this:
error: undefined reference to 'std::invalid_argument::invalid_argument(char const*)'
...
error: undefined reference to 'torch::serialize::OutputArchive::write(std::string const&, at::Tensor const&, bool)'
This doesn't happen when I link with the corresponding pre-built libtorch instead. My application links with -lstdc++ -ltorch -ltorch_cpu -lc10. I've noticed that
Some of these undefined references are from std, others from torch. This surprises me - how is it possible I'm missing stuff from std when I'm linking in stdc++?
The pre-built libtorch includes several .so's I don't have, such as glom. None of these seem necessary to my application though, and I don't need to link them in when using pre-built libtorch.
So what might be the reason I'm getting these undefined references, or how can I go about debugging it?

C++ build error using librealsense in ubuntu

I installed librealsense from the source. It was installed and build successfully.
When I tried to run the sample project (rs-hello-realsense) I have found the build error.
/usr/local/include/librealsense2/hpp/rs_types.hpp:90: undefined reference to `rs2_get_error_message'
/usr/local/include/librealsense2/hpp/rs_types.hpp:92: undefined reference to `rs2_get_failed_function'
/usr/local/include/librealsense2/hpp/rs_types.hpp:92: undefined reference to `rs2_get_failed_function'
Same kind of error from a different type of build.
CMakeFiles/rs-hello-realsense.dir/rs-hello-realsense.cpp.o: In function `rs2::error::error(rs2_error*)':
rs-hello-realsense.cpp:(.text._ZN3rs25errorC2EP9rs2_error[_ZN3rs25errorC5EP9rs2_error]+0x2e): undefined reference to `rs2_get_error_message'
rs-hello-realsense.cpp:(.text._ZN3rs25errorC2EP9rs2_error[_ZN3rs25errorC5EP9rs2_error]+0x73): undefined reference to `rs2_get_failed_function'
Finally, get it. Need to add a couple of lines in CMakeList.txt.
Which linked the libraries in the project.
find_package(realsense2 2.29.0)
target_link_libraries(rs-hello-realsense ${DEPENDENCIES} ${realsense2_LIBRARY})
OR
target_link_libraries(${PROJECT_NAME} ${realsense2_LIBRARY})
Other than all other properties of CMake File will be same

OpenCV Qt undefined reference errors while compiling

I am trying to work with a depth sensor (PMD Camboard) and trying to show the captured depth image as a live stream. I can get the data from the sensor just fine. The problem starts as soon as I try to put in the OpenCV parts. Individually they work perfectly, but the problem starts as soon as I try to integrate them. I think the error is with the CMakeLists.txt, since even if I try to compile a simple program that just displays the webcam feed, without any of the depthsensor stuff, I get the same error. The said program works when I try to compile it with just the CMake for openCV.
EDIT: I tried systematically disabling parts of the CMake to see which parts exactly trigger the error and found that the line:
link_directories(${royale_LIB_DIR})
is the one that is causing the error.
I get the following errors when I try to compile:
/usr/local/lib/libopencv_highgui.so.3.1.0: undefined reference to `QWidget::isFullScreen() const#Qt_5'
/usr/local/lib/libopencv_cvv.so.3.1.0: undefined reference to `QAbstractSlider::setMinimum(int)#Qt_5'
/usr/local/lib/libopencv_highgui.so.3.1.0: undefined reference to `non-virtual thunk to QBoxLayout::minimumSize() const#Qt_5'
/usr/lib64/libQt5OpenGL.so.5: undefined reference to `QTransform::type() const#Qt_5'
/usr/local/lib/libopencv_cvv.so.3.1.0: undefined reference to `QWidget::hasHeightForWidth() const#Qt_5'
/usr/lib64/libQt5OpenGL.so.5: undefined reference to `QPen::color() const#Qt_5'
/usr/local/lib/libopencv_cvv.so.3.1.0: undefined reference to `QFrame::changeEvent(QEvent*)#Qt_5'
/usr/lib64/libQt5OpenGL.so.5: undefined reference to `QOpenGLMultiGroupSharedResource::insert(QOpenGLContext*, QOpenGLSharedResource*)#Qt_5_PRIVATE_API'
/usr/lib64/libQt5OpenGL.so.5: undefined reference to `QPaintDevice::~QPaintDevice()#Qt_5'
/usr/local/lib/libopencv_cvv.so.3.1.0: undefined reference to `QObject::QObject(QObject*)#Qt_5'
/usr/local/lib/libopencv_cvv.so.3.1.0: undefined reference to `QTabWidget::metaObject() const#Qt_5'
/usr/local/lib/libopencv_cvv.so.3.1.0: undefined reference to `QColorDialog::QColorDialog(QWidget*)#Qt_5'
/usr/local/lib/libopencv_highgui.so.3.1.0: undefined reference to `QFutureInterfaceBase::setThreadPool(QThreadPool*)#Qt_5'
/usr/lib64/libQt5OpenGL.so.5: undefined reference to
Full error at http://pastebin.com/KLKtzzSn
And my CMakeLists.txt is as follows:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_PREFIX_PATH "share")
#project (depthDataCallBack)
project (webStream)
find_package(OpenCV REQUIRED)
find_package(royale REQUIRED)
link_directories(${royale_LIB_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
add_executable(webStream webStream.cpp)
target_link_libraries(webStream ${OpenCV_LIBS})
target_link_libraries(webStream "${royale_LIBS}")
I'm new to CMake, having only been recently corrected. Any help would be appreciated.
The list of errors shows that there is not Qt5 Widgets in linkage. Add the below lines to your CMakeLists.txt:
target_link_libraries(webStream Qt5::Widgets)
target_link_libraries(webStream Qt5::Core)
The other question is whether or not Qt 5 is installed in that system.
Also read here: cmake doesn't link libGLU using QtOpenGL as long as there Qt OpenGL dependencies missing as well.

Boost filesystem3 calls undefined

Encountering "boost::filesystem3" undefined reference errors while trying to link:
RawStreamReader.cpp:(.text._ZNK5boost11filesystem34path4leafEv[_ZNK5boost11filesystem34path4leafEv]+0x1f): undefined reference to `boost::filesystem3::path::filename() const'
This is on Linux 64.
I rebuilt the boost 1.55 with same c++11 options that I am using for my build.
The output from this symbol dump seems to indicate that none of the symbols contain "filesystem3" in the name (the 3 part is missing).
Note, that I have read these questions / answers (to no avail):
Failed at linking C++ [undefined reference boost::filesystem3 ... ]
c++ boost::filesystem undefined reference to `boost::filesystem3::path::root_name() const'
The code is compiling on other systems against same boost version. This is a fresh install with a new cmake build setup that I am trying to debug. All other boost libraries seem to be linking fine.

How to Install/Use libcurl with C++ on Windows/Eclipse CDT

Can someone please explain how to use libcurl with C++ on Windows with Eclipse CDT/Code::Blocks or a similar IDE?
I'm very new to C++ but I know my way around Java very well.
I'm using MinGW but I keep getting this error:
C:\Core\src>g++ -I"C:\curl\include\curl" -L"C:\curl\lib64" -lcurldll core.cpp -o
core.exe
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xc81): undefined
reference to `_imp__curl_easy_init'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xca7): undefined
reference to `_imp__curl_easy_setopt'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xcc4): undefined
reference to `_imp__curl_easy_setopt'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xcd1): undefined
reference to `_imp__curl_easy_perform'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xce1): undefined
reference to `_imp__curl_easy_cleanup
What I've Tried:
If I give the wrong library path/name it will tell me that it can not find the library. So clearly it FOUND the libcurldll.a/libcurl.a files but it isn't linking with them properly.
I've tried putting the actual libcurl.dll file from the bin into every source folder possible in my project.
I've tried going to C/C++ General > Paths and Symbols then added "curl" and "curldll" to libraries and "C:\curl\lib64" to the library search path.
I've tried manually adding the -lcurl, -lcurldll, -DCURL_STATICLIB, -L"C:\curl\lib64" options to the MinGW Linker tool.
This has been stumping me for days. Please help.