How to fix undefined references after libtorch custom build? - 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?

Related

Building Qt project with Network using CMake causes undefined references

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

Build errors in C++ project with OpenCV in Eclipse: undefined reference to `SelectObject#8' + many others

I'll start by saying that I don't have much experience at all with compiled languages, this may be obvious but I've done a few solid hours searching and have come up empty-handed.
I am trying to build this project in Eclipse. I have built and imported OpenCV 3.1, and everything seems okay until I go to build the project. At that point I get a wide array of error messages saying that I have 'undefined references'.
For example:
C:\dev\opencv\build\x64\mingw_s\lib/libopencv_highgui310.a(window_w32.cpp.obj):window_w32.cpp:(.text$_ZL15icvRemoveWindowP8CvWindow+0x11b): undefined reference to `SelectObject#8'
C:\dev\opencv\build\x64\mingw_s\lib/libopencv_highgui310.a(window_w32.cpp.obj):window_w32.cpp:(.text$_ZL15icvRemoveWindowP8CvWindow+0x126): undefined reference to `DeleteObject#4'
C:\dev\opencv\build\x64\mingw_s\lib/libopencv_highgui310.a(window_w32.cpp.obj):window_w32.cpp:(.text$_ZL15icvRemoveWindowP8CvWindow+0x138): undefined reference to `DeleteDC#4'
C:\dev\opencv\build\x64\mingw_s\lib/libopencv_highgui310.a(window_w32.cpp.obj):window_w32.cpp:(.text$_ZL16icvGetBitmapDataP8CvWindowP7tagSIZEPiPPv.isra.0+0x1e): undefined reference to `GdiFlush#0'
C:\dev\opencv\build\x64\mingw_s\lib/libopencv_highgui310.a(window_w32.cpp.obj):window_w32.cpp:(.text$_ZL16icvGetBitmapDataP8CvWindowP7tagSIZEPiPPv.isra.0+0x32): undefined reference to `GetCurrentObject#8'
C:\dev\opencv\build\x64\mingw_s\lib/libopencv_highgui310.a(window_w32.cpp.obj):window_w32.cpp:(.text$_ZL16icvGetBitmapDataP8CvWindowP7tagSIZEPiPPv.isra.0+0x6d): undefined reference to `GetObjectA#12'
Full console output is here.
From what I've seen, the causes of this error in general can be varied, perhaps the specifics of my case would allow a more precise answer.
I get the feeling that I'm not importing something correctly, or not setting an appropriate flag, but I have no idea what it is, or how to accomplish it. Any help would be greatly appreciated.
Matt
All your missing functions are in Gdi32.lib Search "msdn SelectObject" (for example) - scroll down to requirements add missing libraries to project
-- comment by Richard Critten

Using ArrayFire libraries

I downloaded the free version of ArrayFire library. I tried to test matrix multiplication with it. However I got a bunch of undefined references such as
matrixdata.cpp:(.text+0x19e5): undefined reference to af::array::array<unsigned char>(int, int, unsigned char const*, af::af_source_t, int)
matrixdata.cpp:(.text+0x1a22): undefined reference to af::array::operator=(af::array const&)'
matrixdata.cpp:(.text+0x1a31): undefined reference to af::array::~array()
Naturally I noticed that I haven't linked arrayfire libraries in my makefile.
However, all my attempts to link the libraries have failed. Can someone please tell what command should I add to link the proper libraries
The files in the lib folder are:
libafcpu.so
libafcuda.so
libafopencl.so
libclBLAS.so
libclBLAS.so.2
libclBLAS.so.2.3.0
libclFFT.so
libclFFT.so.2
libclFFT.so.2.3.0
libStatTimer.so
libStatTimer.so.2
libStatTimer.so.2.3.0
After installation, add the path to the shared libraries of ArrayFire to your LD_LIBRARY_PATH variable. Once you do that, it is merely indicating the correct backend library you need with -l option. Say for cpu, it would be -lafcpu.
What command are you trying ?

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.

Why will I have C++ unknown reference link error on the application program but not the library in linux

Now I am building a C++ dynamic library libabc.so and an application test based on this library in linux. libabc.so will invoke boost dynamic library libboost.so. I can compile libabc.so very well, and no errors can be found. If I use ldd command on libabc.so, I can easily observe that this library has a dependency on libboost.so. However, when I compile the application program test, I have the following link error:
abc.so: undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
collect2: ld returned 1 exit status
I do not know where the problem comes from. When I compile test program, I am sure that I link it with both libabc.so and libboost.so. I also changed the sequence of libabc.so and libboost.so when linking just to make sure that the right library sequence is given. Any ideas? Thanks.
The function in question is defined to take different arguments, depending on whether the programis compiled as C++03 or C++11.
void boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option::enum_type, boost::system::error_code*) // pre-C++11
void boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*) // C++11
In my opinion this is a Boost bug.
Consequently, if this function is used in a program, then both Boost and the program must be compiled with C++11 support, or both without. Otherwise the program will fail at the linking stage.
Shared libraries in Linux are normally allowed to have unresolved references, this doesn't cause their linking to fail.