Why does the undefined reference to ' xxxxx ' error occur?
First of all, this is a link error, not a compilation error, that is, if only this error, the source code of your program
No problem, you compile with the compiler when the parameters are not used, do not specify the linker to use the library, such as your program used some mathematical functions, then you will be compiled
parameter specifies the program to link the math library, by adding-LM to the compile command line.
-L parameter and-l parameter
The-l parameter is used to specify the library to which the program is linked, and the-l parameter is followed by the library name, so what does the library name have to do with the real library file name?
Take the math library, his library name is M, his library filename is libm.so, it is easy to see, the library file name of the head Lib and tail. So remove the name of the library.
Okay now we know how to get the name of the library, for example, we have to use a third party to provide the library name is libtest.so, then we just copy libtest.so to/usr/lib, compile with-ltest parameters, We will be able to use the Libtest.so library (of course, with the function in the Libtest.so library, we also need to match the libtest.so header file).
Placed in/libAnd/usr/libAnd/usr/local/libIn the library directly with the-l parameter can be linked, but if the library file is not placed in these three directories, but in other directories, then we only use the-l parameter, the link will be error, the error message is probably: "/usr/bin/ld:cannot find-lxxx", That is, the linker LD couldn't find it in those 3 directories.
Libxxx.so, then another parameter-L will come in handy, such as the Common X11 library, which is placed in the/usr/x11r6/lib directory, we compile with the-l/usr/x11r6/lib-lx11 parameter, the-l parameter followed by the library file is located in the directory name. For example, we put libtest.so in the/AAA/BBB/CCC directory, the link parameter is-l/aaa/bbb/ccc-ltest In addition, most libxxxx.so is just a link to RH9, for example, libm.so it links to/lib/ Libm.so.x,/lib/libm.so.6 also linked to/lib/libm-2.3.2.so,
If there is no such link, or error, because LD will only find libxxxx.so, so if you want to use the XXXX library, and only libxxxx.so.x or libxxxx-x.x.x.so, do a link can be ln-s libxxxx-x.x.x.so libxxxx.so Manual to write link parameters is always troublesome, fortunately many library development packages provide a program to generate link parameters, the name
Generally called xxxx-config, generally placed in the/usr/bin directory, such as gtk1.2 link parameter generator is gtk-config, execute gtk-config--libs can get the following output "-
L/usr/lib-l/usr/x11r6/lib-lgtk-lgdk-rdynamic-lgmodule-lglib-ldl-lxi-lxext-lx11-lm ", this is the G required to compile a gtk1.2 program
TK link parameter, xxx-config In addition to the--libs parameter is a parameter is--cflags used to generate the header
Contains the directory, which is the-I parameter, which we'll cover below. You can try out the Gtk-config--libs--cflags and see the output.
Now the problem is how to use these output results, the most stupid way is to copy and paste or copied, smart
The method is to add the ' Xxxx-config--libs in the compiler command line--
Cflags ', such as compiling a GTK program: gcc gtktest.c ' gtk-config--libs--cflags ' so
is poor
It's not much. Note ' is not a single quote, but the key to the left of the 1 key.
In addition to Xxx-config, now the new development package is generally used pkg-config to generate link parameters, using the method
Similar to Xxx-config, but Xxx-config is for a specific development package
, but Pkg-config contains a lot of development package link parameter generation, with Pkg-config--list-all command can
All supported development packages are listed, and the use of Pkg-config is pkg
-config pagname--libs--cflags, where Pagname is the package name, is Pkg-config--list-all
One of the lists, like gtk1.2 's name is gtk+,pkg-.
The role of config GTK +--libs--cflags is the same as Gtk-config--libs--cflags. Like what:
GCC gtktest.c ' pkg-config GTK +--libs--cflags '
。
5. -include and-I parameters
-include is used to include header files, but usually contains header files in the source code with # include XXXXXX implementation
, the-include parameter is seldom used. The-i parameter is used to specify the header file directory
, the/usr/include directory is usually not specified, GCC knows where to look, but if the header file is not/usr/i
In the nclude we are going to specify with the-I parameter, such as the header file
In the/myinclude directory, the compiler command line will add the-i/myinclude parameter, if not added you will get
An "Xxxx.h:no such file or directory" error. -I.
Parameters can be used relative to the path, such as the header file in the current directory, can be specified with-I. The--cf we mentioned above
The lags parameter is used to generate the-I parameter.
6. -O parameter
This is a program optimization parameters, generally used-o2 is used to optimize the program, such as GCC Test.c-o2, excellent
The resulting program is smaller than it is not optimized, and the speed of execution may be
High (I haven't tested it).
7. -shared parameters
Use when compiling a dynamic library, such as gcc-shared Test.c-o libtest.so
8. A few related environment variables
Pkg_config_path: Used to specify the path of the PC file used by Pkg-config, default is/usr/lib/pkgconf
The ig,pc file is a text file with the extension. PC, which defines the development
The installation path of the package, libs parameters and Cflags parameters, and so on.
CC: Used to specify the C compiler.
CXX: Used to specify the CXX compiler.
LIBS: Similar to the--libs above.
CFLAGS: Similar to the--cflags above.
Cc,cxx,libs,cflags Manual compilation is generally not used, when doing configure sometimes used, the general situation
Don't care.
Environment variable Setting method: Export Env_name=xxxxxxxxxxxxxxxxx
9. About Cross-compiling
Cross-compilation in layman's words is to compile on one platform that can run on a different architecture, than
such as compiling on our PC platform (X86 CPU) can run on SPARC
Programs on the CPU platform, compiled programs are not operational on X86 CPU platforms and must be placed on SPARC
Running on the CPU platform.
Of course, all two platforms are Linux.
This method is widely used in heterogeneous platform porting and embedded development.
Relative and cross-compiling, our usual compilation is called local compilation, that is, in the current platform compiled, compiled to get
The program is also executed locally.
The compiler used to compile such a program is called a cross-compiler, which, in contrast, is called a local compilation.
The general use of GCC, but this gcc with the local gcc compiler
is different, you need to compile GCC with a specific configure parameter in order to get GCC that supports cross-compilation.
In order to not be confused with the local compiler, the name of the cross compiler usually has a prefix, such as Sparc-xxxx-linux-gn
u-gcc,sparc-xxxx-linux-gnu-g++, wait.
10. How to use the cross compiler
The use is similar to the local GCC, but one thing is special: the-L and-I parameters must be used to specify the compiler spar
C system Library and header files, cannot be used locally (X86)
Library (header files can sometimes be used locally).
Example:
SPARC-XXXX-LINUX-GNU-GCC Test.c-l/path/to/sparclib-i/path/to/sparcinclude
GCC command Line Details-L Specify the path to the library-l specifies the name of the library to connect to