Three possibilities and solutions for undefined reference to error in Qt (take libgdal as an example)

There is a piece of code that has not been touched for a long time. Today I want to run it and compile it, but suddenly I find a bunch of errors, as follows:

Well, first of all, I can guarantee that these errors must not have occurred during the debugging of the code before. There has been no movement recently, why did it suddenly appear.

Anyway, solving the problem is the key.

Just looking at the content of this error report, it is a typical undefined reference to error report. This error report is a common occurrence in the programming of calling a third-party library, and the meaning of the representative is also very clear, that is, the definition of the function cannot be found.

Anyone who writes code in C++ knows that in C++, the code is divided into declaration and definition. The declaration tells the compiler that there is this function. In other words, it is a name, and the definition is the implementation content of this function.

Similar to this error report, there will also be an error report that is undeclared, which is probably the error report of the function declaration.

Closer to home, how to deal with the error of undefined reference to

I should first think of whether there is no corresponding library file , so I went to the /usr/local/lib directory to find libgdal.so.26, and I could find it.
If the library file is there, it may be that Qt did not find the library, then try to import the library manually, but it is useless .

When it is found that the above two points are not the reason, it means that the error information obtained from here is not enough to solve this problem, so at this time, turn your attention to the tab page of the compiled output to find clues. Then found the crux of the problem. In the content of the compiled output, there is such an error

/usr/bin/ld: warning: libxerces-c-3.2.so, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libnetcdf.so.15, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libhdf5_serial.so.103, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libogdi.so.4.1, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libCharLS.so.2, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libgeotiff.so.5, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libcfitsio.so.8, needed by /usr/local/lib/libgdal.so.26, not found (try using -rpath or -rpath-link)

The problem is obvious. There are a lot of libgdal dependency libraries missing, which makes me very confused. Obviously there were no such errors before, which means that the dependencies are all right, so why suddenly there is a problem. I recalled it carefully, and suspected that some dependencies might have been deleted by mistake when using sudo apt autoremove when uninstalling other software.

The problem is easy to solve, just install the missing dependencies for him. A special reminder, other dependencies are easy to find. Just sudo apt install XXXXX and try a few more times, and you can basically find it. Among them There are two libraries that are not easy to find, libmfhdfalt and libdfalt, these two libraries need to install libhdf4-alt-dev to solve.

So to sum up, there are three possible reasons for the undefined reference to error:

There is no such library file.

There are library files, but Qt can't find his location.

Dependencies for this library file are missing.

Reposted from: https://blog.csdn.net/baidu_31788709/article/details/123094382

Guess you like

Origin blog.csdn.net/fuhanghang/article/details/130366452