linux共享库位置配置(LD_LIBRARY_PATH环境变量 或者 更改/etc/ld.so.conf 或者 使用-R选项)
今天下午尝试使用libosip2,安装比较简单,按照自带的help文档里面的操作进行即可。
$>mkdir linux-build
$>cd linux-build
$>../libosip2-2.2.0/configure
$>make (最后2步要在管理员权限下执行)
# make install
完成之后,会在/usr/local/lib路径下生成一些lioosipXXX.so的文件,在/usr/local/include下生成关于osip的头文件。
为了测试安装是否正确,在eclipse下建了个测试项目,写了很简单的代码:
#include //不加的话,编译时会报错,可能osip依赖于time.h
#include
//之所以能找到/usr/local/include下关于osip的头文件,是因为eclipse默认把/usr/local/include放到Include directories里面了。在eclipse的项目属性-->C++ General-->Path and symbols -->Include里面可以看到。如果自己写makefile文件的话,要加上-I 选项,表示程序里include的头文件去哪里找。参见
http://blog.csdn.net/liuzhuan_1986/archive/2009/07/05/4323274.aspx 关于头文件说明
#include
using namespace std;
int main()
{
int i;
osip_t *osip;
i = osip_init(&osip);
if(i != 0)
cout << "error" << endl;
cout << "ok" << endl;
return 0;
}
并且在eclipse的项目属性-->C/C++ Build-->Settings-->Tool settings-->GCC C++ Linker-->Libraries加上-lpthread -losip2的选项。
编译链接都没有问题,运行时报错:
error while loading shared libraries: libosip2.so.4: cannot open shared object file: No such file or directory
然后查到可能是因为共享库设置的问题:
下面的几段是转载的,看了之后明白linux怎么找到共享库的
===========================================================================================
Linux 运行的时候,是如何管理共享库(*.so)的?在 Linux 下面,共享库的寻找和加载是由 /lib/ld.so 实现的。 ld.so 在标准路经(/lib, /usr/lib) 中寻找应用程序用到的共享库。
但是,如果需要用到的共享库在非标准路经,ld.so 怎么找到它呢?
目前,Linux 通用的做法是将非标准路经加入 /etc/ld.so.conf,然后运行 ldconfig 生成 /etc/ld.so.cache。 ld.so 加载共享库的时候,会从 ld.so.cache 查找。
传统上,Linux 的先辈 Unix 还有一个环境变量:LD_LIBRARY_PATH 来处理非标准路经的共享库。ld.so 加载共享库的时候,也会查找这个变量所设置的路经。
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib
export LD_LIBRARY_PATH
但是,有不少声音主张要避免使用 LD_LIBRARY_PATH 变量,尤其是作为全局变量。这些声音是:
* LD_LIBRARY_PATH is not the answer - http://prefetch.net/articles/linkers.badldlibrary.html
* Why LD_LIBRARY_PATH is bad - http://xahlee.org/UnixResource_dir/_/ldpath.html
* LD_LIBRARY_PATH - just say no - http://blogs.sun.com/rie/date/20040710
解决这一问题的另一方法是在编译的时候通过 -R 选项指定 run-time path。
1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library会找不到
2. 想往上面两个目录以外加东西的时候,一定要修改/etc/ld.so.conf,然后再调用ldconfig,不然也会找不到。
比如安装了一个mysql到/usr/local/mysql,mysql有一大堆library在/usr/local/mysql/lib下面,这时就需要在/etc/ld.so.conf下面加一行/usr/local/mysql/lib,保存过后ldconfig一下,新的library才能在程序运行时被找到。
3. 如果想在这两个目录以外放lib,但是又不想在/etc/ld.so.conf中加东西(或者是没有权限加东西)。那也可以,就是export一个全局变量LD_LIBRARY_PATH,然后运行程序的时候就会去这个目录中找library。一般来讲这只是一种临时的解决方案,在没有权限或临时需要的时候使用。
4. ldconfig做的这些东西都与运行程序时有关,跟编译时一点关系都没有。编译的时候还是该加-L就得加,不要混淆了。
5. 总之,就是不管做了什么关于library的变动后,最好都ldconfig一下,不然会出现一些意想不到的结果。不会花太多的时间,但是会省很多的事。
LD_LIBRARY_PATH 这个环境变量是大家最为熟悉的,它告诉loader:在哪些目录中可以找到共享库。可以设置多个搜索目录,这些目录之间用冒号分隔开。在linux下,还提供了另外一种方式来完成同样的功能,你可以把这些目录加到/etc/ld.so.conf中,然后调用ldconfig。当然,这是系统范围内全局有效的,而环境变量只对当前shell有效。按照惯例,除非你用上述方式指明,loader是不会在当前目录下去找共享库的,正如shell不会在当前目前找可执行文件一样。
================================================================================================
在shell下尝试设置LD_LIBRARY_PATH,以下面这种形式设置,老是报错bash: LD_LIBRARY_PATH: command not found,
LD_LIBRARY_PATH=/usr/local/lib
LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/usr/local/lib
可能是因为系统之前没有设置过LD_LIBRARY_PATH,于是改成这样:
export LD_LIBRARY_PATH=/usr/local/lib
然后用 echo $LD_LIBRARY_PATH检查一下是否真的设置成功,发现可以。
接着在该shell下运行eclipse生成的可执行文件,没有错误。
另外,如果不想每次新启一个shell都设置LD_LIBRARY_PATH,可以编辑~/.bash_profile文件:
$ vi ~/.bash_profile
添加:
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
这两行,完成之后.bash_profile如下所示:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
LD_LIBRARY_PATH=/usr/local/lib
export PATH
export LD_LIBRARY_PATH
但是这种方法只能用在shell下,想在eclipse里面运行,还是不行:
尝试了eclipse项目properties里面的各种设置都不起作用。
用“eclipse LD_LIBRARY_PATH”作为关键字(可见关键字多么重要)才搜到这么篇文章 《eclipse+cdt+gcc编译选项控制》 http://hi.baidu.com/zsffei/blog/item/7b17c043ceb51e1772f05de1.html
才知道应该在eclipse的项目属性-->C/C++ Build-->Settings-->Tool settings-->GCC C++ Linker-->Miscellaneous的Other options (-Xlinker [option])添加 -R/usr/local/lib
运行,一切正常,折腾了一下午,太感动了。
##############################
Build Targets | ||
---|---|---|
Prev | Chapter 11. Kernel build command line reference | Next |
These targets build the kernel itself in a variety of ways.
Table 11.4. Build targets
Target |
Description |
---|---|
all |
Builds all of the different targets needed for this kernel to be able to be used. This includes both the modules and the static portion of the kernel. |
vmlinux |
Builds just the static portion of the kernel, not any loadable modules. |
modules |
Builds all of the loadable kernel modules for this configuration. |
modules_install |
Installs all of the modules into the specified location. If no location is specified with the INSTALL_MODULE_PATH environment variable, they are installed in the default root directory of the machine. |
dir/ |
Builds all of the files in the specified directory and in all subdirectories below it. |
dir/file.[ o | i |s ] |
Builds only the specified file. |
dir/file.ko |
Builds all of the needed files and links them together to form the specified module. |
tags |
Builds all of the needed tags that most common text editors can use while editing the source code. |
TAGS |
Builds all of the needed tags that most common text editors can use while editing the source code. |
cscope |
Builds a cscope image, useful in source tree searches, of the source tree for the architecture specified by the configuration file (not all of the kernel source files). |
You can also pass a number of environment variables to make that will change the build. These can be specified for almost any target.
Table 11.5. Environment variables
Variable |
Value |
Description |
---|---|---|
V |
0 |
This tells the build system to run in a quiet manner, showing only the file that is currently being built, and not the entire command that is running in order to build that file. This is the default option for the build system. |
V |
1 |
This tells the build system to operate in a verbose way, showing the full command that is being used to generate each of the specific files. |
O |
dir |
This tells the build system to locate all output files in the dir directory, including the kernel configuration files. This allows the kernel to be built from a read-only filesystem and have the output placed in another location. |
C |
1 |
This checks all C files that are about to be built with the sparse tool, which detects common programming errors in the kernel source files. sparse can be downloaded using git from git://git.kernel.org/pub/scm/devel/sparse/sparse.git. Nightly snapshots can be found at http://www.codemonkey.org.uk/projects/git-snapshots/sparse/. More information on how to use sparse can be found in the Documentation/sparse.txt file in the kernel source tree. |
C |
2 |
This forces all C files to be checked with the sparse tool, even if they did not need to be built. |
#################
export CC=/your/path/to/gcc export CXX=/your/path/to/g++
###################
http://www.gentoo.org/doc/en/gcc-optimization.xml
Content: 1. Introduction 2. Optimizing 3. Optimization FAQs 4. Resources
1. Introduction
What are CFLAGS and CXXFLAGS?
CFLAGS and CXXFLAGS are environment variables that are used to tell the GNU Compiler Collection, gcc, what kinds of switches to use when compiling source code. CFLAGS are for code written in C, while CXXFLAGS are for code written in C++.
They can be used to decrease the amount of debug messages for a program, increase error warning levels, and, of course, to optimize the code produced. The GNU gcc handbook maintains a complete list of available options and their purposes.
How are they used?
CFLAGS and CXXFLAGS can be used in two ways. First, they can be used per-program with Makefiles generated by automake.
However, this should not be done when installing packages found in the Portage tree. Instead, set your CFLAGS and CXXFLAGS in /etc/make.conf. This way all packages will be compiled using the options you specify.
Code Listing 1.1: CFLAGS in /etc/make.conf |
CFLAGS="-march=athlon64 -O2 -pipe" CXXFLAGS="${CFLAGS}" |
As you can see, CXXFLAGS is set to use all the options present in CFLAGS. This is what you'll want almost without fail. You shouldn't ever need to specify additional options in CXXFLAGS.
Misconceptions
While CFLAGS and CXXFLAGS can be very effective means of getting source code to produce smaller and/or faster binaries, they can also impair the function of your code, bloat its size, slow down its execution time, or even cause compilation failures!
CFLAGS are not a magic bullet; they will not automatically make your system run any faster or your binaries to take up less space on disk. Adding more and more flags in an attempt to optimize (or "rice") your system is a sure recipe for failure. There is a point at which you will reach diminishing returns.
Despite the bragging you'll find on the internet, aggressive CFLAGS and CXXFLAGS are far more likely to harm your programs than do them any good. Keep in mind that the reason the flags exist in the first place is because they are designed to be used at specific places for specific purposes. Just because one particular CFLAG is good for one bit of code doesn't mean that it is suited to compiling everything you will ever install on your machine!
Ready?
Now that you're aware of some of the risks involved, let's take a look at some sane, safe optimizations for your computer. These will hold you in good stead and will endear you to developers the next time you report a problem on Bugzilla. (Developers will usually request that you recompile a package with minimal CFLAGS to see if the problem persists. Remember, aggressive flags can ruin code.)
2. Optimizing
The basics
The goal behind using CFLAGS and CXXFLAGS is to create code tailor-made to your system; it should function perfectly while being lean and fast, if possible. Sometimes these conditions are mutually exclusive, so we'll stick with combinations known to work well. Ideally, they are the best available for any CPU architecture. We'll mention the aggressive flags later so you know what to look out for. We won't discuss every option listed on the gcc manual (there are hundreds), but we'll cover the basic, most common flags.
Note: Whenever you're not sure what a flag actually does, refer to the relevant chapter of the gcc manual. If you're still stumped, try Google, or check out the gcc mailing lists. |
-march
The first and most important option is -march. This tells the compiler what code it should produce for your processor architecture (or arch); it says that it should produce code for a certain kind of CPU. Different CPUs have different capabilities, support different instruction sets, and have different ways of executing code. The -march flag will instruct the compiler to produce code specifically for your CPU, with all its capabilities, features, instruction sets, quirks, and so on.
Even though the CHOST variable in /etc/make.conf specifies the general architecture used, -march should still be used so that programs can be optimized for your specific processor. x86 and x86-64 CPUs (among others) should make use of the -march flag.
What kind of CPU do you have? To find out, run the following command:
Code Listing 2.1: Examining CPU information |
$ cat /proc/cpuinfo
|
Now let's see -march in action. This example is for an older Pentium III chip:
Code Listing 2.2: /etc/make.conf: Pentium III |
CFLAGS="-march=pentium3" CXXFLAGS="${CFLAGS}" |
Here's another one for a 64-bit AMD CPU:
Code Listing 2.3: /etc/make.conf: AMD64 |
CFLAGS="-march=athlon64" CXXFLAGS="${CFLAGS}" |
If you still aren't sure what kind of CPU you have, you may just want to use -march=native. When this flag is used, GCC will detect your processor and automatically set appropriate flags for it.However, this should not be used if you intend to compile packages for a different CPU!
So if you're compiling packages on one computer, but intend to run them on a different computer (such as when using a fast computer to build for an older, slower machine), then do not use -march=native. "Native" means that the code produced will run only on that type of CPU. The applications built with -march=native on an AMD Athlon 64 CPU will not be able to run on an old VIA C3 CPU.
Also available are the -mtune and -mcpu flags. These flags are normally only used when there is no available -march option; certain processor architectures may require -mtune or even -mcpu. Unfortunately, gcc's behavior isn't very consistent with how each flag behaves from one architecture to the next.
On x86 and x86-64 CPUs, -march will generate code specifically for that CPU using all its available instruction sets and the correct ABI; it will have no backwards compatibility for older/different CPUs. If you don't need to execute code on anything other than the system you're running Gentoo on, continue to use -march. You should only consider using -mtune when you need to generate code for older CPUs such as i386 and i486. -mtune produces more generic code than -march; though it will tune code for a certain CPU, it doesn't take into account available instruction sets and ABI. Don't use -mcpu on x86 or x86-64 systems, as it is deprecated for those arches.
Only non-x86/x86-64 CPUs (such as Sparc, Alpha, and PowerPC) may require -mtune or -mcpu instead of -march. On these architectures, -mtune/-mcpu will sometimes behave just like -march (on x86/x86-64) . . . but with a different flag name. Again, gcc's behavior and flag naming just isn't consistent across architectures, so be sure to check the gcc manual to determine which one you should use for your system.
Note: For more suggested -march/-mtune/-mcpu settings, please read chapter 5 of the appropriate Gentoo Installation Handbook for your arch. Also, read the gcc manual's list of architecture-specific options, as well as more detailed explanations about the differences between -march, -mcpu, and -mtune. |
-O
Next up is the -O variable. This controls the overall level of optimization. This makes the code compilation take somewhat more time, and can take up much more memory, especially as you increase the level of optimization.
There are five -O settings: -O0, -O1, -O2, -O3, and -Os. You should use only one of them in /etc/make.conf.
With the exception of -O0, the -O settings each activate several additional flags, so be sure to read the gcc manual's chapter on optimization options to learn which flags are activated at each -Olevel, as well as some explanations as to what they do.
Let's examine each optimization level:
As previously mentioned, -O2 is the recommended optimization level. If package compilations error out, check to make sure that you aren't using -O3. As a fallback option, try setting your CFLAGS and CXXFLAGS to a lower optimization level, such as -O1 or even -O0 -g2 -ggdb (for error reporting and checking for possible problems) and recompile the package.
-pipe
A common flag is -pipe. This flag actually has no effect on the generated code, but it makes the compilation process faster. It tells the compiler to use pipes instead of temporary files during the different stages of compilation, which uses more memory. On systems with low memory, gcc might get killed. In that case, do not use this flag.
-fomit-frame-pointer
This is a very common flag designed to reduce generated code size. It is turned on at all levels of -O (except -O0) on architectures where doing so does not interfere with debugging (such as x86-64), but you may need to activate it yourself by adding it to your flags. Though the GNU gcc manual does not specify all architectures it is turned on by using -O, you will need to explicitly activate it on x86. However, using this flag will make debugging hard to impossible.
In particular, it makes troubleshooting applications written in Java much harder, though Java is not the only code affected by using this flag. So while the flag can help, it also makes debugging harder; backtraces in particular will be useless. However, if you don't plan to do much software debugging and haven't added any other debugging-related CFLAGS such as -ggdb, then you can try using -fomit-frame-pointer.
Important: Do not combine -fomit-frame-pointer with the similar flag -momit-leaf-frame-pointer. Using the latter flag is discouraged, as -fomit-frame-pointer already does the job properly. Furthermore, -momit-leaf-frame-pointer has been shown to negatively impact code performance. |
-msse, -msse2, -msse3, -mmmx, -m3dnow
These flags enable the SSE, SSE2, SSE3, MMX, and 3DNow! instruction sets for x86 and x86-64 architectures. These are useful primarily in multimedia, gaming, and other floating point-intensive computing tasks, though they also contain several other mathematical enhancements. These instruction sets are found in more modern CPUs.
Important: Be sure to check if your CPU supports these by running cat /proc/cpuinfo. The output will include any supported additional instruction sets. Note that pni is just a different name for SSE3. |
You normally don't need to add any of these flags to /etc/make.conf as long as you are using the correct -march (for example, -march=nocona implies -msse3). Some notable exceptions are newer VIA and AMD64 CPUs that support instructions not implied by -march (such as SSE3). For CPUs like these you'll need to enable additional flags where appropriate after checking the output of cat /proc/cpuinfo.
Note: You should check the list of x86 and x86-64-specific flags to see which of these instruction sets are activated by the proper CPU type flag. If an instruction is listed, then you don't need to specify it; it will be turned on by using the proper -march setting. |
3. Optimization FAQs
But I get better performance with -funroll-loops -fomg-optimize!
No, you only think you do because someone has convinced you that more flags are better. Aggressive flags will only hurt your applications when used system-wide. Even the gcc manual says that using -funroll-loops and -funroll-all-loops makes code larger and run more slowly. Yet for some reason, these two flags, along with -ffast-math, -fforce-mem, -fforce-addr, and similar flags, continue to be very popular among ricers who want the biggest bragging rights.
The truth of the matter is that they are dangerously aggressive flags. Take a good look around the Gentoo Forums and Bugzilla to see what those flags do: nothing good!
You don't need to use those flags globally in CFLAGS or CXXFLAGS. They will only hurt performance. They may make you sound like you have a high-performance system running on the bleeding edge, but they don't do anything but bloat your code and get your bugs marked INVALID or WONTFIX.
You don't need dangerous flags like these. Don't use them. Stick to the basics: -march, -O, and -pipe.
What about -O levels higher than 3?
Some users boast about even better performance obtained by using -O4, -O9, and so on, but the reality is that -O levels higher than 3 have no effect. The compiler may accept CFLAGS like -O4, but it actually doesn't do anything with them. It only performs the optimizations for -O3, nothing more.
Need more proof? Examine the gcc source code:
Code Listing 3.1: -O source code |
if (optimize >= 3) { flag_inline_functions = 1; flag_unswitch_loops = 1; flag_gcse_after_reload = 1; /* Allow even more virtual operators. */ set_param_value ("max-aliased-vops", 1000); set_param_value ("avg-aliased-vops", 3); } |
As you can see, any value higher than 3 is treated as just -O3.
What about redundant flags?
Oftentimes CFLAGS and CXXFLAGS that are turned on at various -O levels are specified redundantly in /etc/make.conf. Sometimes this is done out of ignorance, but it is also done to avoid flag filtering or flag replacing.
Flag filtering/replacing is done in many of the ebuilds in the Portage tree. It is usually done because packages fail to compile at certain -O levels, or when the source code is too sensitive for any additional flags to be used. The ebuild will either filter out some or all CFLAGS and CXXFLAGS, or it may replace -O with a different level.
The Gentoo Developer Manual outlines where and how flag filtering/replacing works.
It's possible to circumvent -O filtering by redundantly listing the flags for a certain level, such as -O3, by doing things like:
Code Listing 3.2: Specifying redundant CFLAGS |
CFLAGS="-O3 -finline-functions -funswitch-loops" |
However, this is not a smart thing to do. CFLAGS are filtered for a reason! When flags are filtered, it means that it is unsafe to build a package with those flags. Clearly, it is not safe to compile your whole system with -O3 if some of the flags turned on by that level will cause problems with certain packages. Therefore, you shouldn't try to "outsmart" the developers who maintain those packages. Trust the developers. Flag filtering and replacing is done for your benefit! If an ebuild specifies alternative flags, then don't try to get around it.
You will most likely continue to run into problems when you build a package with unacceptable flags. When you report your troubles on Bugzilla, the flags you use in /etc/make.conf will be readily visible and you will be told to recompile without those flags. Save yourself the trouble of recompiling by not using redundant flags in the first place! Don't just automatically assume that you know better than the developers.
What about LDFLAGS?
The Gentoo developers have already set basic, safe LDFLAGS in the base profiles, so you don't need to change them.
Can I use per-package flags?
There is no supported method for using CFLAGS or other variables on a per-package basis, though there are a few rather abusive ways of trying force Portage to do so.
Warning: You should not try to force Portage to use per-package flags, as it is not in any way supported and will greatly complicate bug reports. Just set your flags in /etc/make.conf to be used on a system-wide basis. |
4. Resources
The following resources are of some help in further understanding optimization:
#########################转
http://www.yolinux.com/TUTORIALS/LinuxTutorialOptimization.html
Optimize hard drive access performance: |
The following optimizations techniques can be applied to (E