007_wz_bbk_ar cr t 链接外部静态库

Search Order

When environment variables and command-line options are used together the compiler searches the directories in the following order:

  • Command-line options '-I' and '-L',from left to right.
  • Directories specified by environment variables,such as C_INCLUDE_PATH and LIBRARY_PATH.
  • Default system directories.
    Note:In day-to-day usage,directories are usually added to the search paths with the options '-I' and '-L'.

Creating a Library with ar

The GNU archiver ar combines a collection of objects files into a single archive file,also know as a library.An archive file is simply a convenient way of distributing a large number of related object files together.

  • You can use the following command to create a static library:
$ ar cr libNAME.a file1.o file2.o ... filen.o
  • The archiver ar also provides a "table of contents" option 't' to list the object files in an existing library.
$ ar t libNAME.a

EX:

main.c

mylib.h

func1.c

func2.c

使用 gcc -Wall * -c生成目标文件
目录

使用ar cr创建一个自己的库文件:
libadd.a

第三种方法,使用默认库:

使用库文件编译程序,生成可执行文件:


add

注意文件的编译顺序:


第一种方法:

-L

第二种方法(通过环境变量):

LIBRARY_PATH

注:添加:$LIBRARY_PATH是防止变量原有的的内容被冲掉,表示最佳环境变量
2020.11.18

你可能感兴趣的:(007_wz_bbk_ar cr t 链接外部静态库)