Figure C.1 shows how the linker accepts binary files, which were generated by the compiler, and creates executable binaries as its output. The linker executable, on *nix machines is simply called ld, and it is run by g++ after all source files are compiled successfully.
All of these steps are performed when you run make, which prints out every command before it executes. By reading the output of make, you can see what arguments are being passed to the compiler and linker. When an error occurs, it immediately follows the command line that produced the error.
Example C.3 shows the command line options passed to g++, and attempts to show how g++ runs the linker, known as ld, and also passes some arguments to it.
g++ -Wl,-rpath,/usr/local/qt-x11-free-3.2.3/lib <-- 1 -o hw7 <-- 2 .obj/address.o .obj/ca_address.o .obj/constraintgroup.o .obj/customer.o .obj/dataobject.o .obj/dataobjectfactory.o .obj/hw07-demo.o .obj/us_address.o .obj/moc_address.o <-- 3 .obj/moc_ca_address.o .obj/moc_customer.o .obj/moc_dataobject.o .obj/moc_us_address.o -L/usr/local/qt-x11-free-3.2.3/lib -L/usr/X11R6/lib -L/usr/local/utils/lib <-- 4 -lutils -lqt -lXext -lX11 -lm <-- 5
|
Linking entails the following:
This is the general idea. The linker resolves references to names by finding their real addresses in files and checking the addresses to determine whether they e valid for the type id. Its like a directory look-up service for C++ compilers.
C++ programmers sometimes spend lots of time trying to understand and repair compiler and linker errors. If you can understand the message, you e stuck. With a compiler error, the problem is easier to diagnose because it is related to the compilation of one source code module and the header files it includes. The compiler generally tells you the exact location of any error that it detects. With a linker error, the problem is related to how your source code modules link together. When the linker stage is reached, all the individual modules have compiled without errors. Linker errors can be caused by bugs in C++ code, but they can also be a result of mistakes in the project file.
Installing a library means making it available for more than a single user on a system. It is also possible to reuse a library without installing it. All libraries that you reuse must either be installed or placed in a directory listed in your LD_LIBRARY_PATH.
When you are reusing a library for the first time, you will probably see this error message. It means that the linker cannot find the library. When the gnu linker looks for a shared object, it checks at least two places:
The directories specified in LD_LIBRARY_PATH
Installed libraries referenced from a cache file called /etc/ld.so.cache
This is the most common and probably the most annoying linker error of all. It means that the linker cannot find the definition of some named entity in your code. Here is some output from make.
.obj/ca_address.o(.gnu.linkonce.t._ZN10DataObject16getConstraint- MgrEv+0x4): In function DataObject::getConstraintMgr(): /usr/local/qt-x11-free-3.2.3/include/qshared.h:50: undefined reference to DataObject::sm_Cm collect2: ld returned 1 exit status make: *** [hw7] Error 1
The compiler found the declaration, but the linker can find the corresponding definition. In some part of your code, you are referencing a symbol, but there is no definition found. The useful bits of information are
The first step is to determine whether we, as humans, can find the missing definition. If we can , how can the linker? If we find it in a .cpp file, we must make sure that
Because we are using good naming conventions (see Section 3.4), we can immediately tell that sm_Cm is a static data member of class DataObject. The compiler found the declaration, but the linker can find the definition.
Because it is static (Section 2.10), the definition for sm_Cm belongs in dataobject.cpp. The compiler expects to find a definition statement of the form:
ConstraintMgr DataObject::sm_Cm;
If its there and the linker still can find it, the most likely causes for this error are
This is one of the most confusing errors. It generally means that a virtual function definition is missing. Literally, the vtable for that class (which has addresses of each the virtual functions) is unable to be fully constructed.
This error can arise from missing function definitions in your code, but it can also be caused by a missing HEADERS or SOURCES enTRy in your make/project file. The resolution is to double-check that all files are listed properly in the project file before you delve too deeply into your C++ code.
[3] classes with at least one virtual method