Original address: http://www.oschina.net/question/4873_19441
If our program calls the dynamic-link library, you can set the path to find the dynamic-link library file by run-->enviroment--> Adding the LD_LIBRARY_PATH environment variable when running in the CDT. But when running, where does the program go to find the dynamic link library?
There are two main methods of this method. One is to set the LD_LIBRARY_PATH environment variable for the system (it is not valid to set up a program that is officially running in Eclipse). The method of setting is shown in appendix three
The other is to compile the path of the dynamic link library file into a binary executable file. So how does the eclipse compile? Such as
OK, so our program can run away from Eclipse (, for a long time). Note, do not set the error, is G++linker's miscellaneous.
The disadvantage of the second approach is that the library's address cannot be changed (this method is taught in applications such as games). The advantage of the first approach is that you can change the address of the library file by changing the value of the Ld_library_path. The disadvantage of course is to change the value of the environment variable.
---------------------------------------------------------Cute Split-line----------------------------------------------------------
Linux Summary of dynamic library usage
. Basic concepts of static and dynamic libraries
A static library, which is added to the execution code when the executable is connected, is physically part of the execution program; programs compiled with static libraries do not need to be supported by the library file, but can be used everywhere, but generate
Executable file is larger. Dynamic libraries, which are loaded into the executor when the executable program is started, can be shared by multiple executable programs. The program generated using dynamic library compilation is relatively small, but the runtime requires library file support, which cannot be run if the library files are not available in the machine.
2.
How to use dynamic libraries
When a program uses a shared library when connecting, it must
The location of the shared library must be found at run time. The executable program of Linux is executed by default to search both directories/lib and/usr/lib, and then follow the configuration in/etc/ld.so.conf to search for absolute paths. At the same time, Linux also provides the environment variable Ld_library_path for the user to choose to use, the user can set it to find the path other than the default path, such as Find/work/lib path, you can/etc/rc.d/ Rc.local or other scripts that can be executed when the system is booted
Add the following statement: Ld_library_path =/work/lib:$ (Ld_library_path). And the Ld_library_path path takes precedence over the system default path before the lookup (detailed reference to the use
Ld_library_path ").
However, the role of Ld_library_path is global, and excessive use may affect the operation of other applications, so it is more used in debugging. (Ld_library_path defects and usage guidelines, refer to the Why
Ld_library_path is bad "). It is generally recommended to use the-R or-rpath option of GCC to specify the lookup path of the library at compile time, and the path information for that library is stored in the executable file, which is directly to that path at run time.
To avoid using LD_LIBRARY_PATH environment variable lookups.
3. Link-time path and run-time path for the library
Modern connectors separate the link-time path (link-time path) from the run-time path (run-time path) while processing the dynamic library, and the user can specify the path to the library through-l (or-rpath), specifying the path to the Program runtime Library, Greatly improves the flexibility of the library application. For example, we do embedded
#arm-linux-gcc $ (CFLAGS) –o target–l/work/lib/zlib/when porting
-llibz-1.2.3 (Work/lib/zlib is a cross-compiled zlib library), after the target is compiled, we simply copy the Zlib library to the system default path of the development Board. Or, the find path is specified by-rpath (or-R), Ld_library_path.
How to make Linux boot after loading ld_library_path path! Landlord Bekars (Turbo: No problem can not solve, because there is no problem) 2004-10-20 10:06:43 in Linux/unix Community/program Development Zone ask is to modify the. bash_profile file, I modified it but after starting Linux ld The path of _library_path is still not, some library files are not loaded when running the program! Problem points: 50, reply number: 8top1 Lou winux0 (unchanged) reply at 2004-10-20 11:20:09 score 50 Remember the last to export LD_LIBRARY_PATHTOP2 building Bekars (Turbo: There is no solution to the problem, Because there's no problem at all) reply at 2004-10-20 11:35:34 score 0.bash_profile# Get the aliases and functions
If [-f ~/.BASHRC]; Then
. ~/.bashrc
fi# User specific environment and startup Programspath= $PATH:/sbin:/usr/sbin:/usr/local/sbin
Ld_library_path= $LD _library_path:/usr/local/libexport PATH
Export Ld_library_path
unset username or not. TOP3 building Cheeralen (embedded) reply at 2004-10-20 11:46:58 score 0bash does not read the environment variable from the profile
CSH will not read the variables from the profile file.
So you can modify your bash for csh!!! TOP4 Building Bekars (Turbo: There is no problem that can't be solved, because there is no problem) reply at 2004-10-20 11:57:37 score 0 upstairs of not quite understand, can say more some Top5 Lou Winux0 (fate unchanged) back in 2004-10-20 11:58:16 score 0 No, otherwise your export path is useless. TOP6 Building Bekars (Turbo: There is no problem to solve, because there is no problem) reply to 2004-10-20 12:35:29 score 0 do not know, Can you give me a try? I just can't do it here. bash_profile# Get the aliases and functions
If [-f ~/.BASHRC]; Then
. ~/.bashrc
fi# User specific environment and startup Programspath= $PATH:/sbin:/usr/sbin:/usr/local/sbin
Ld_library_path= $LD _library_path:/usr/local/libexport PATH
Export Ld_library_path
Unset USERNAME
As we all know, the default search path for Linux dynamic libraries is/lib and/usr/lib. When a dynamic library is created, it is generally copied to both directories. When the program executes requires a dynamic library, and the dynamic library is not loaded into memory, the system will automatically go to the two default search paths to find the corresponding dynamic library file, and then load the file into memory, so that the program can use the function in the dynamic library, as well as other resources of the dynamic library. In Linux, the search path for a dynamic library can be specified in the following three ways, in addition to the default search path.
Method One: Specify the dynamic library search path in the configuration file/etc/ld.so.conf.
You can specify the search path for a dynamic library by editing the configuration file/etc/ld.so.conf, where each behavior is a dynamic library search path. Each time you finish editing the file, you must run the command ldconfig for the modified configuration to take effect. We illustrate the method by example.
Example 1:
We use the following command to create the dynamic library libpos.so with the source program POS_CONF.C (see Program 1), and the detailed creation process is described in [1].
# gcc-c POS_CONF.C
# Gcc-shared-fpci-o libpos.so POS_CONF.O
#
#include <stdio.h>
void Pos ()
{
printf ("/root/test/conf/lib\n");
}
Program 1:POS_CONF.C
Then compile the MAIN.C (see Program 2) to generate the target program POS with the following command.
# gcc-o POS main.c-l.-lpos
#
void Pos ();
int main ()
{
POS ();
return 0;
}
Program 2:MAIN.C
Then move the library file to the directory/root/test/conf/lib.
# mkdir-p/root/test/conf/lib
# MV Libpos.so/root/test/conf/lib
#
Finally, edit the configuration file/etc/ld.so.conf and append a line "/root/test/conf/lib" to the file.
Try running the program POS.
#./pos
./pos:error while loading shared libraries:libpos.so:cannot open Shared object file:no such file or directory
#
Error, the system did not find the dynamic library libpos.so. Look for reasons, the original after editing the configuration file/etc/ld.so.conf, did not run the command ldconfig, so just the changes have not yet taken effect. We run Ldconfig and try again.
# Ldconfig
#./pos
/root/test/conf/lib
#
The program POS runs successfully and prints the correct results.
Method Two: Specify the dynamic library search path through the environment variable Ld_library_path.
You can also specify a dynamic library search path by setting the environment variable Ld_library_path. When multiple dynamic library search paths are specified through the environment variable, the paths are separated by a colon ":". This method is illustrated by the following example.
Example 2:
We use the following command to create a dynamic library libpos.so with the source program POS_ENV.C (see program 3).
# gcc-c POS_ENV.C
# Gcc-shared-fpci-o libpos.so POS_ENV.O
#
#include <stdio.h>
void Pos ()
{
printf ("/root/test/env/lib\n");
}
Program 3:POS_ENV.C
The test executable POS can be used in example 1 to get the target program POS, which does not need to be compiled again. Because the function pos in POS_CONF.C and the function pos function in POS_ENV.C are identical, and the dynamic library name is the same, it is like recreating the library after modifying the dynamic library pos. This is also one of the advantages of using dynamic libraries.
Then move the dynamic library libpos.so to the directory/root/test/conf/lib.
# mkdir-p/root/test/env/lib
# MV Libpos.so/root/test/env/lib
#
We can use export to set the environment variable, which is valid for all commands after setting the environment variable.
For example:
# Export Ld_library_path=/root/test/env/lib
#
But for the sake of convenience, this article uses another method of setting the environment variable, which is only valid for the command before the command, and the environment variable is not effective until the command executes. As the following command:
# Ld_library_path=/root/test/env/lib./pos
/root/test/env/lib
#
The program POS runs successfully, and the result of printing is "/root/test/env/lib", which is the result of the function POS running in program POS_ENV.C. So the dynamic library that the program POS searches for is/root/test/env/lib/libpos.so.
Method Three: Specify the dynamic library search path for the program when compiling the target code.
You can also specify the dynamic library search path for the program when compiling the target code. This is specified through the GCC parameter "-wl,-rpath," as shown in Example 3. When you specify multiple dynamic library search paths, the paths are separated by a colon ":".
Example 3:
We use the following command to create a dynamic library libpos.so with the source program POS.C (see program 4).
# gcc-c POS.C
# Gcc-shared-fpci-o libpos.so POS.O
#
#include <stdio.h>
void Pos ()
{
printf ("./\n");
}
Program 4:POS.C
Because we need to specify the dynamic library search path of the executable when compiling the target code, we need to recompile the source program MAIN.C (see Program 2) with the GCC command to generate the executable pos.
# gcc-o POS main.c-l.-lpos-wl,-rpath,./
#
Try running the program POS again.
#./pos
./
#
The program POS runs successfully and the output is the result of the function pos running in POS.C. So the dynamic library that the program POS searches for IS./libpos.so.
The above describes three ways to specify the dynamic library search path, plus the default dynamic library search path/lib and/usr/lib, a total of five dynamic library search paths, then what is the order of their search?
In the introduction of the three methods described above, a dynamic library was created./libpos.so,/root/test/env/lib/libpos.so and/root/test/conf/lib/libpos.so. We then use the source program POS_LIB.C (see program 5) to create the dynamic library/lib/libpos.so, using the source program POS_USRLIB.C (see program 6) to create a dynamic library/usr/lib/libpos.so.
#include <stdio.h>
void Pos ()
{
printf ("/lib\n");
}
Program 5:POS_LIB.C
#include <stdio.h>
void Pos ()
{
printf ("/usr/lib\n");
}
Program 6:POS_USRLIB.C
So we get five dynamic library libpos.so, these dynamic libraries have the same name, and all contain common function pos of the same function prototype. But the location of the store differs from the result of the common function pos printing. The common function pos in each dynamic library outputs the location where the dynamic library is stored. In this way, we can find out which dynamic library it searched for by executing the results from the executable pos in Example 3, get the 1th Dynamic Library search order, then delete the dynamic library, execute the program POS, get the 2nd Dynamic Library search path, and delete the 2nd search dynamic library, so reciprocating, The sequencing of the Linux search dynamic Library will be available. The corresponding relationship between the output of the program POS execution and the search for the dynamic library is shown in table 1:
program POS Output Results |
The dynamic library used |
corresponding dynamic Library search path designation method |
./ |
./libpos.so |
The dynamic library search path specified when compiling the target code |
/root/test/env/lib |
/root/test/env/lib/libpos.so |
environment variable Ld_library_path the specified dynamic library search path |
/root/test/conf/lib |
/root/test/conf/lib/libpos.so |
Dynamic Library search path specified in configuration file/etc/ld.so.conf |
/lib |
/lib/libpos.so |
Default dynamic Library search path/lib |
/usr/lib |
/usr/lib/libpos.so |
Default dynamic Library search path/usr/lib |
Table 1: Correspondence between program POS output and dynamic libraries
Create individual dynamic libraries and place them in the appropriate directory. The test environment is ready. Execute the program POS and set the environment variable Ld_library_path in the command line.
# Ld_library_path=/root/test/env/lib./pos
./
#
According to the output results of the program POS, the first search is the dynamic library search path specified when compiling the target code. Then we remove the dynamic library./libpos.so, and then run the above command to try again.
# RM libpos.so
Rm:remove regular file ' libpos.so '? Y
# Ld_library_path=/root/test/env/lib./pos
/root/test/env/lib
#
According to the output results of the program POS, the 2nd Dynamic Library search path is specified by the environment variable Ld_library_path. We'll remove the/root/test/env/lib/libpos.so and run the above command.
# rm/root/test/env/lib/libpos.so
Rm:remove regular file '/root/test/env/lib/libpos.so '? Y
# Ld_library_path=/root/test/env/lib./pos
/root/test/conf/lib
#
The search path for the 3rd dynamic library is the path specified by the configuration file/etc/ld.so.conf. Remove the dynamic library/root/test/conf/lib/libpos.so before running the above command.
# rm/root/test/conf/lib/libpos.so
Rm:remove regular file '/root/test/conf/lib/libpos.so '? Y
# Ld_library_path=/root/test/env/lib./pos
/lib
#
The search path for the 4th dynamic Library is the default search path,/lib. We then delete the dynamic library/lib/libpos.so and run the above command.
# rm/lib/libpos.so
Rm:remove regular file '/lib/libpos.so '? Y
# Ld_library_path=/root/test/env/lib./pos
/usr/lib
#
The last dynamic library search path is the default search path,/usr/lib.
Combined with the above results, the order of search path search for dynamic library is as follows:
1. The dynamic library search path specified when compiling the target code;
2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path;
3. The dynamic library search path specified in the configuration file/etc/ld.so.conf;
4. The default dynamic library search path/lib;
5. The default dynamic library search path/usr/lib.
When you specify a dynamic library search path in the above 1, 2, 3, you can specify multiple dynamic library search paths, which are searched in the order of the specified paths. For this article no longer an example, interested readers can refer to the method of this article to verify.
"Go" eclipse+cdt+gcc compile option control