Makefile variable and dynamic library static library

1. Makefile variables

1. Predefined variables of Makefile

variable name meaning
AR Function library packaging program, which can create static library .a documents. The default is ar
AS Assembler, default is as
CC C compiler, default gcc
CXX C++ compiler, default g++
CPP The preprocessor for C programs, the default is $(CC) -E
RM Delete command, the default is rm -f
ARFLAGS The command line parameters for executing the AR command, the default is rv
ASFLAGS Command-line arguments for assembler AS (when .s or .S files are specified explicitly)
CFLAGS Command line parameters for executing the CC compiler (options for compiling .c source files)
CXXFLAGS Execute the command line parameters of the g++ compiler (options for compiling .cpp source files)

2. Makefile automation variables

variable name meaning Remark
@ (file name) Represents the full name of the target of the rule it is in
% (file name) A member name of the static library file that represents the rule it is in
< (filename) Represents the first full name of the dependency list of the rule it is in
* (file name) In pattern rules and static pattern rules, the stem The part represented by "%" in the target pattern when stemming. When the filename exists in the directory, the stem can also contain the directory
? (File List) A list of dependent files with timestamps newer than the target file to be changed, separated by spaces
^ (list of files) Represents the dependency list of the rule it is in The same file cannot be repeated
+ (list of files) Represents the dependency list of the rule it is in The same file can be repeated, mainly used in the cross-reference place of the library when the program is linked

3. Variations of Makefile automation variables

variable name meaning Remark
@D Represents the directory part of the target file (remove the last slash in the directory part) If $ @ is dir/foo.o, then the value of $(@D) is dir; if $@ has a slash, its value is the current directory (.); note the difference between it and the function dir
@F The part of the object file's full filename other than the directory (the actual filename) If $@ is dir/foo.o, then $(@F) is foo.o; $(@F) is equivalent to the function $(notdir $@)
*D Represents a directory section in the target stem
*F Represents the filename part in the target stem
%D When the static library in the form of archive (member) is the target, the directory part in the member name of the table library file member Only valid for rule targets in the form of archive(member)
%F When the static library in the form of archive (member) is the target, the file name part of the member name of the table library file Only valid for rule targets in the form of archive(member)
<D Represents the directory part of the first dependent file in the rule
<F Represents the filename portion of the first dependent file in the rule
^D Directory section representing all dependent files The same file cannot be repeated
^F Represents the filename part of all dependent files The same file cannot be repeated
+D Directory section representing all dependent files The same file can be repeated
+F Represents the filename part of all dependent files The same file can be repeated
?D The part of the directory representing the updated dependent file
?F Represents the filename part of the updated dependent file

4. Run and use of Makefile

# 执行指定的Makefile  Almake是makefile的名称
$ make -f ALmake
$ make --file Almake 
$ make --makefile Almake
$ make TARGET      #指定终极目标
#强制重建所有规则中的目标
$ make -B
$ make -always-make
#指定makefile的所在路径
$ make -C dir/
$ make --directory=dir/

# GNU-autotools工具(自动化makefile)
# GNU软件安装的三部曲
$ ./configure
$ make
$ make install

2. Dynamic library and static library

1. Understanding of static libraries and dynamic libraries

  • Static library: Linux files with the suffix .a (lib file of win)
    static library are integrated into the executable program during the linking phase of gcc. Even if the library is removed, the program will still run successfully. But the volume is large.
    When the program is loaded, the static library loader is copied, so the volume is large.
  • Dynamic library: Linux files with .so suffix (win dll file)
    dynamic library is not exclusive to any program, any program can share this library, and the library is only linked when the program is running. small volume.
  • Rules for dynamic libraries in Linux naming system
    libname.so.xyz
    lib: fixed representative library name: library name so: fixed suffix x: major version number y: minor version number z: release version
    number Compatibility (difference of major version number)
    Minor version update software, retain the original program, add functions (version number downward compatible)
    release version performance change, program optimization; (mutually compatible)

2. The creation and use of static libraries (ar command)

# ar的常用参数
$ ar  -ctr   libtest.a   *.o  #将当前目录下的所有.o文件都打包成静态库libtest.a
#ar 的参数 
# -r 将文件插入库文件中,如果已存在就替换;
# -t 查看所有打包的.o文件
# -c 表示创建,create
$ gcc  main.c  libtest.a  -o main   #使用静态库

3. Creation and use of dynamic libraries

$ gcc  -fPIC  -shared   *.c  -o   libtest.so  #创建动态库
#  -fPIC    表示生成独立代码 PIC告诉编译器产生与位置无关代码,即内存不固定。
#  -shared  表示生成共享的动态库 
$ gcc  main.c  ./libtest.so  -o main  -I  ./inc  #链接动态库生成执行文件main,但main执行的时候动态库必须存在。
#动态库默认实在系统lib中找,可以将生成的动态库考培带系统lib中;也可添加$LD_LIBRARY_PATH 的环境变量到本目录下也可。
# 添加环境变量export LD_LIBRARY_PATH=/home/gec/Linux/ku:$LD_LIBRARY_PATH 
# /home/gec/lib:库文件路径(路径可根据自身需求指定); 
# 注意:封装动态库时候的函数对应头文件;动态库要存在;动态库的环境

Guess you like

Origin blog.csdn.net/weixin_44763594/article/details/125954826