"Things embedded LINUX and C language software development kernel depth analysis" study 5-- preprocessing, the library

C compiler connection process

Here Insert Picture Description

GCC in some remote extension

extension name meaning
.C C ++ source code file
.i Pretreated C source code files
.ii Pretreated C ++ source code files
.m Objective-C source code files
.out Linker generates executable file
.s Assembly language source code file, the latter no longer preprocessing operations
.S Assembly language source code files, the latter will carry out pre-processing operations can run preprocessing directives

Pretreatment of the content and meaning

意义:使编译器实现功能变得更为专一。
内容:文件包含、宏定义、条件编译、一些特殊的预处理关键字、去掉程序中的注释

The difference between #ifdef and #if defined

#if defined可以组成复杂的预编译条件,例:
#if defined(A) && defined (B)
<code>
#endif

Comparison of static and dynamic libraries

使用静态库,在被链接形成可执行程序时,把库中的代码复制进可执行程序中。这样如果多个应用程序都用到相同的库函数,就会复制多分,运行时在内存中也存在多分相同的库函数副本,浪费内存。
使用动态库,在编译的时候不会复制库函数代码,只是做一个链接标记,标记这个函数需要到动态库中找,然后运行时环境发现它调用了一个动态库中的库函数,就加载这个动态库到内存,以后不管多少应用程序使用该库函数,该库函数在内存中只有一份。

Make use of static libraries

gcc XX.c -o XX.o -c
ar -rc libXX.a XX.o
使用gcc -c只编译不连接,生成.o ,然后用ar工具打包成.a。库文件名不能随便起,要lib+库名称.a。
要使用的时候,把库文件拷贝到系统库目录下或者编译参数加-L指定库文件查找目录,然后编译参数加上:-lXX指定编译的时候需要用的库。
(不要忘了头文件)

Make use of dynamic libraries

gcc XX.c -o XX.o -c -fPIC
gcc -o libXX.so XX.o -shared
-fPIC是位置无关码,-shared是按照共享库的方式来链接。
使用方法和静态库一样。
Published 38 original articles · won praise 17 · views 4315

Guess you like

Origin blog.csdn.net/qq_14877637/article/details/87115741