# crucial env and paramters for c/c++ compiling

c/c++的程序在编译的时候就是一件很麻烦的事情，往往需要各种环境变量的设置找到对应的库。

### PATH

PATH是系统范围的变量，每个shell都有自己的path，系统会从path所指定的路径下面寻找execuatble file。修改的时候一种是临时使用export PATH=$PATH:<new path> 添加，另外一种是将export命令放在.bashrc之类的配置文件中。还有一个问题，多个同名称的excuatable在不同的路径下面，如何保证系统执行的是最新的版本。比如这个问题，根据其中的回答，系统是按照先来后到的顺序查找excutable的，这样可以使用export PATH = <new path>:$PATH 来保证将自己需要的path放在变量的最前面的部分。

### other key parameters from the system aspects

One good example is the spck env setting. Ensentally, it will modify these key variables after we loading a new spack environment (a set of variables under the spack view will be set into these key variables).

I copy this form from the spack official documents:

Variable Paths
PATH bin
MANPATH man, share/man
ACLOCAL_PATH share/aclocal
LD_LIBRARY_PATH lib, lib64
LIBRARY_PATH lib, lib64
CPATH include
PKG_CONFIG_PATH lib/pkgconfig, lib64/pkgconfig, share/pkgconfig
CMAKE_PREFIX_PATH .

This table contains typical system level env, we disucssed the PATH, LD_PATH and LD_LIBRARY_PATH. The CPATH is easy to understand, we need to find the header files. In particular, it specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the command line. This environment variable is used regardless of which language is being preprocessed.

PKG_CONFIG_PATH is a environment variable that specifies additional paths in which pkg-config will search for its .pc files. The pkg-config’s searching path can be specified by this env. The .pc file that describes the information of the package will be put into the search dir of the pkg-config search path.

for example:

### gcc -L -l 与 -I（big i）

-larchive Add archive file archive to the list of files to link. ld will search its path-list for occurrences of libarchive.a for every archive specified. 也就是说在编译的时候如果使用了某个动态链接库可以通过-l参数设置进来

-Lsearchdir Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts. 如果使用-L参数，则会在指定的目录下搜索，之后在link的时候从对应的目录下找到需要的文件

-I dir 表示include path， Add the directory dir to the list of directories to be searched for header files during preprocessing，使用了-I参数之后就会从这个路径下找头文件。

-shared Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. You must also specify -fpic or -fPIC on some systems when you specify this option.

### 参考资料

https://blog.csdn.net/wangeen/article/details/8159500

http://xahlee.info/UnixResource_dir/_/ldpath.html