﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Prayer-随笔分类-LINUX/UNIX/AIX</title><link>http://www.cppblog.com/prayer/category/7968.html</link><description>在一般中寻求卓越</description><language>zh-cn</language><lastBuildDate>Wed, 10 Apr 2019 13:21:33 GMT</lastBuildDate><pubDate>Wed, 10 Apr 2019 13:21:33 GMT</pubDate><ttl>60</ttl><item><title>nm命令中符号类型详解</title><link>http://www.cppblog.com/prayer/archive/2019/04/10/216346.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 10 Apr 2019 06:28:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/04/10/216346.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216346.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/04/10/216346.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216346.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216346.html</trackback:ping><description><![CDATA[<p style="font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; color: #2f2f2f; font-size: 16px; line-height: 24px; background-color: #ffffff;">nm命令介绍的很多，但大多不介绍其函数符号标志的含义。<br />最近在调试动态库时常用到，其中用的最多的用法:<br />nm -A * |grep &#8220;aaa&#8221; | c++filt&nbsp; // -A 为了显示文件， c++filt转换为可读风格，好像有个参数也能实现类似功能</p><p style="font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; color: #2f2f2f; font-size: 16px; line-height: 24px; background-color: #ffffff;"><br />其他内容整理如下（原作者未知):</p><p style="font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; color: #2f2f2f; font-size: 16px; line-height: 24px; background-color: #ffffff;"><span style="font-family: 'Times New Roman'; font-size: 12pt;"><span style="font-family: inherit; background-color: #fffe65;">nm</span></span><span style="font-family: 宋体; font-size: 12pt;">用于列出目标文件的符号清单，如果没有指定目标文件，则默认为&#8220;</span><span style="font-family: 'Times New Roman'; font-size: 12pt;">a.out</span><span style="font-family: 宋体; font-size: 12pt;">&#8221;。</span><span style="font-family: 'Times New Roman'; font-size: 12pt;"><span style="font-family: inherit; background-color: #fffe65;">nm</span></span><span style="font-family: 宋体; font-size: 12pt;">的格式如下：</span></p><p style="font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; color: #2f2f2f; font-size: 16px; line-height: 24px; background-color: #ffffff;"><span style="font-family: 宋体; font-size: 12pt;"><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: Arial; font-size: small;"><span style="font-family: inherit; background-color: #fffe65;">nm</span>&nbsp;[&#8216;-a&#8217;|&#8216;--debug-syms&#8217;] [&#8216;-g&#8217;|&#8216;--extern-only&#8217;]</span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;-B&#8217;] [&#8216;-C&#8217;|&#8216;--demangle&#8217;[=style]] [&#8216;-D&#8217;|&#8216;--dynamic&#8217;]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;-S&#8217;|&#8216;--print-size&#8217;] [&#8216;-s&#8217;|&#8216;--print-armap&#8217;]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;-A&#8217;|&#8216;-o&#8217;|&#8216;--print-file-name&#8217;][&#8216;--special-syms&#8217;]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;-n&#8217;|&#8216;-v&#8217;|&#8216;--numeric-sort&#8217;] [&#8216;-p&#8217;|&#8216;--no-sort&#8217;]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;-r&#8217;|&#8216;--reverse-sort&#8217;] [&#8216;--size-sort&#8217;] [&#8216;-u&#8217;|&#8216;--undefined-only&#8217;]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;-t&#8217; radix|&#8216;--radix=&#8217;radix] [&#8216;-P&#8217;|&#8216;--portability&#8217;]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;--target=&#8217;bfdname] [&#8216;-f&#8217;format|&#8216;--format=&#8217;format]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&#8216;--defined-only&#8217;] [&#8216;-l&#8217;|&#8216;--line-numbers&#8217;] [&#8216;--no-demangle&#8217;]</span></span></span><br /><span style="font-family: inherit; font-size: small;"><span style="font-family: Arial;"><span style="font-family: inherit; font-size: 9pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-family: Arial;">&nbsp;<span style="font-family: inherit; font-size: 9pt;">[&#8216;-V&#8217;|&#8216;--version&#8217;] [&#8216;-X 32_64&#8217;] [&#8216;--help&#8217;] [objfile...]</span></span></span><br /><span style="font-family: Arial;"><span style="font-family: inherit; font-size: small;"><span style="font-family: inherit; font-size: 9pt;">&nbsp;&nbsp;</span></span></span><br /><span style="font-family: inherit; font-size: small;">对于每一个符号，<span style="font-family: inherit; background-color: #fffe65;">nm</span>列出其值(the symbol value)，类型（the symbol type）和其名字(the symbol name)。</span><br /><span style="font-family: inherit; font-size: 12pt;">如下例：</span><br /><span style="font-family: inherit; font-size: 12pt;">&nbsp;&nbsp;</span><br />&nbsp;&nbsp;&nbsp;&nbsp; 00000024 T cleanup_before_linux<br />&nbsp;&nbsp;&nbsp;&nbsp; 00000018 T cpu_init<br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;00000060 T dcache_disable</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;00000054 T dcache_enable</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp; 0000006c T dcache_status</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp; 00000000 T do_reset</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp; 0000003c T icache_disable</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp; 00000030 T icache_enable</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp; 00000048 T icache_status</span></span><br /><span style="font-family: inherit; font-size: 9pt;"><span style="font-family: inherit; font-size: small;">&nbsp;<br />上面的显示是使用<span style="font-family: inherit; background-color: #fffe65;">nm</span>&nbsp;cpu.o的输出，对于cleanup_before_linux这个符号来说，00000024是以16进制显示的其值，T为其类型，而cleanup_before_linux是其名字。可以看出，上面显示的<span style="font-family: inherit; color: blue;">cleanup_before_linux</span><span style="font-family: inherit; color: blue;">这个</span><span style="font-family: inherit; color: blue;">symbol</span><span style="font-family: inherit; color: blue;">的值实际上是该函数在</span><span style="font-family: inherit; color: blue;">text section</span><span style="font-family: inherit; color: blue;">中的偏移。但是，每个符号的值的具体含义依其类型而异</span>。当然，对于每个符号的值，其类型、其值以及它们所属的section是密切相关的。<br />下面说明符号类型：<span style="font-family: inherit; font-size: 12pt;">对于每一个符号来说，其类型如果是小写的，则表明该符号是</span><span style="font-family: inherit; font-size: 12pt;">local</span><span style="font-family: inherit; font-size: 12pt;">的；大写则表明该符号是</span><span style="font-family: inherit; font-size: 12pt;">global(external)</span><span style="font-family: inherit; font-size: 12pt;">的。</span></span></span></span></p><table border="1" cellpadding="0" cellspacing="0" style="color: #2f2f2f; font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 16px; line-height: 24px; background-color: #ffffff; border-collapse: collapse; margin-left: 14.4pt;"><tbody><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; width: 36pt; border: 1.5pt double silver;"><strong><span style="font-family: inherit; font-size: 9pt;">符号</span></strong><br /><strong><span style="font-family: inherit; font-size: 9pt;">类型</span></strong></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: 1.5pt 1.5pt 1.5pt medium; border-style: double double double none; width: 378pt; border-top-color: silver; border-right-color: silver;"><strong><span style="font-family: inherit; font-size: 9pt;">说明</span></strong></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">A</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号的值是绝对的，在以后的链接过程中，不允许进行改变。这样的<span style="font-family: inherit; color: blue;">符号值，常常出现在中断向量表中，例如用符号来表示各个中断向量函数在中断向量表中的位置。</span></span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">B</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号的值出现在非初始化数据段</span><span style="font-family: inherit; font-size: 9pt;">(bss)</span><span style="font-family: inherit; font-size: 9pt;">中。例如，<span style="font-family: inherit; color: blue;">在一个文件中定义全局</span></span><span style="font-family: inherit; color: blue; font-size: 9pt;">static int test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。则该符号</span><span style="font-family: inherit; color: blue; font-size: 9pt;">test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">的类型为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">b</span><span style="font-family: inherit; color: blue; font-size: 9pt;">，位于</span><span style="font-family: inherit; color: blue; font-size: 9pt;">bss section</span><span style="font-family: inherit; color: blue; font-size: 9pt;">中。其值表示该符号在</span><span style="font-family: inherit; color: blue; font-size: 9pt;">bss</span><span style="font-family: inherit; color: blue; font-size: 9pt;">段中的偏移。一般而言，</span><span style="font-family: inherit; color: blue; font-size: 9pt;">bss</span><span style="font-family: inherit; color: blue; font-size: 9pt;">段分配于</span><span style="font-family: inherit; color: blue; font-size: 9pt;">RAM</span><span style="font-family: inherit; color: blue; font-size: 9pt;">中</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">C</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号为</span><span style="font-family: inherit; font-size: 9pt;">common</span><span style="font-family: inherit; font-size: 9pt;">。</span><span style="font-family: inherit; font-size: 9pt;">common symbol</span><span style="font-family: inherit; font-size: 9pt;">是未初始话数据段。<span style="font-family: inherit; color: blue;">该符号没有包含于一个普通</span></span><span style="font-family: inherit; color: blue; font-size: 9pt;">section</span><span style="font-family: inherit; color: blue; font-size: 9pt;">中。只有在链接过程中才进行分配。符号的值表示该符号需要的字节数。例如在一个</span><span style="font-family: inherit; color: blue; font-size: 9pt;">c</span><span style="font-family: inherit; color: blue; font-size: 9pt;">文件中，定义</span><span style="font-family: inherit; color: blue; font-size: 9pt;">int test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">，并且该符号在别的地方会被引用，则该符号类型即为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">C</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。否则其类型为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">B</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">D</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号位于初始话数据段中。<span style="font-family: inherit; color: blue;">一般来说，分配到</span></span><span style="font-family: inherit; color: blue; font-size: 9pt;">data section</span><span style="font-family: inherit; color: blue; font-size: 9pt;">中。例如定义全局</span><span style="font-family: inherit; color: blue; font-size: 9pt;">int baud_table[5] = {9600, 19200, 38400, 57600, 115200}</span><span style="font-family: inherit; color: blue; font-size: 9pt;">，则会分配于初始化数据段中</span><span style="font-family: inherit; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">G</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号也位于初始化数据段中。主要用于</span><span style="font-family: inherit; font-size: 9pt;">small object</span><span style="font-family: inherit; font-size: 9pt;">提高访问</span><span style="font-family: inherit; font-size: 9pt;">small data object</span><span style="font-family: inherit; font-size: 9pt;">的一种方式。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">I</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号是对另一个符号的间接引用。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">N</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号是一个</span><span style="font-family: inherit; font-size: 9pt;">debugging</span><span style="font-family: inherit; font-size: 9pt;">符号。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">R</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号位于只读数据区。<span style="font-family: inherit; color: blue;">例如定义全局</span></span><span style="font-family: inherit; color: blue; font-size: 9pt;">const int test[] = {123, 123};</span><span style="font-family: inherit; color: blue; font-size: 9pt;">则</span><span style="font-family: inherit; color: blue; font-size: 9pt;">test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">就是一个只读数据区的符号。注意在</span><span style="font-family: inherit; color: blue; font-size: 9pt;">cygwin</span><span style="font-family: inherit; color: blue; font-size: 9pt;">下如果使用</span><span style="font-family: inherit; color: blue; font-size: 9pt;">gcc</span><span style="font-family: inherit; color: blue; font-size: 9pt;">直接编译成</span><span style="font-family: inherit; color: blue; font-size: 9pt;">MZ</span><span style="font-family: inherit; color: blue; font-size: 9pt;">格式时，源文件中的</span><span style="font-family: inherit; color: blue; font-size: 9pt;">test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">对应</span><span style="font-family: inherit; color: blue; font-size: 9pt;">_test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">，并且其符号类型为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">D</span><span style="font-family: inherit; color: blue; font-size: 9pt;">，即初始化数据段中。但是如果使用</span><span style="font-family: inherit; color: blue; font-size: 9pt;">m6812-elf-gcc</span><span style="font-family: inherit; color: blue; font-size: 9pt;">这样的交叉编译工具，源文件中的</span><span style="font-family: inherit; color: blue; font-size: 9pt;">test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">对应目标文件的</span><span style="font-family: inherit; color: blue; font-size: 9pt;">test,</span><span style="font-family: inherit; color: blue; font-size: 9pt;">即没有添加下划线，并且其符号类型为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">R</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。一般而言，位于</span><span style="font-family: inherit; color: blue; font-size: 9pt;">rodata section</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。值得注意的是，如果在一个函数中定义</span><span style="font-family: inherit; color: blue; font-size: 9pt;">const char *test = &#8220;abc&#8221;, const char test_int = 3</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。使用</span><span style="font-family: inherit; color: blue; font-size: 9pt;"><span style="font-family: inherit; background-color: #fffe65;">nm</span></span><span style="font-family: inherit; color: blue; font-size: 9pt;">都不会得到符号信息，但是字符串&#8220;</span><span style="font-family: inherit; color: blue; font-size: 9pt;">abc</span><span style="font-family: inherit; color: blue; font-size: 9pt;">&#8221;分配于只读存储器中，</span><span style="font-family: inherit; color: blue; font-size: 9pt;">test</span><span style="font-family: inherit; color: blue; font-size: 9pt;">在</span><span style="font-family: inherit; color: blue; font-size: 9pt;">rodata section</span><span style="font-family: inherit; color: blue; font-size: 9pt;">中，大小为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">4</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">S</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">符号位于非初始化数据区，用于</span><span style="font-family: inherit; font-size: 9pt;">small object</span><span style="font-family: inherit; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">T</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号位于代码区</span><span style="font-family: inherit; font-size: 9pt;">text section</span><span style="font-family: inherit; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">U</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号在当前文件中是未定义的，即该符号的定义在别的文件中。<span style="font-family: inherit; color: blue;">例如，当前文件调用另一个文件中定义的函数，在这个被调用的函数在当前就是未定义的；但是在定义它的文件中类型是</span></span><span style="font-family: inherit; color: blue; font-size: 9pt;">T</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。但是对于全局变量来说，在定义它的文件中，其符号类型为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">C</span><span style="font-family: inherit; color: blue; font-size: 9pt;">，在使用它的文件中，其类型为</span><span style="font-family: inherit; color: blue; font-size: 9pt;">U</span><span style="font-family: inherit; color: blue; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">V</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号是一个</span><span style="font-family: inherit; font-size: 9pt;">weak object</span><span style="font-family: inherit; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">W</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">-</span></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号是</span><span style="font-family: inherit; font-size: 9pt;">a.out</span><span style="font-family: inherit; font-size: 9pt;">格式文件中的</span><span style="font-family: inherit; font-size: 9pt;">stabs symbol</span><span style="font-family: inherit; font-size: 9pt;">。</span></td></tr><tr><td valign="top" width="48" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt; border-style: none double double; border-left-color: silver; width: 36pt; border-right-color: silver;"></td><td valign="top" width="504" style="margin: 0px; padding: 0cm 5.4pt; border-bottom-color: silver; border-width: medium 1.5pt 1.5pt medium; border-style: none double double none; width: 378pt; border-right-color: silver;"><span style="font-family: inherit; font-size: 9pt;">该符号类型没有定义</span></td></tr></tbody></table><img src ="http://www.cppblog.com/prayer/aggbug/216346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-04-10 14:28 <a href="http://www.cppblog.com/prayer/archive/2019/04/10/216346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux 比较两个文件夹不同 (diff命令, md5列表)</title><link>http://www.cppblog.com/prayer/archive/2019/04/04/216335.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 04 Apr 2019 09:46:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/04/04/216335.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216335.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/04/04/216335.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216335.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216335.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong><span style="color: #ff0000;">比较文件夹diff，可以直接使用diff命令</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">[root@~]# diff -urNa dir1 dir2<br />　　-a Treat all files as text and compare them line-by-line, even if they do not seem to be text.<br />　　-N, --new-file<br />　　　　In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.<br />　　-r When comparing directories, recursively compare any subdirectories found.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">　　-u Use the unified output format.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="color: #ff0000;"><strong>比较文件夹diff，也可以比较文件MD5列表。下面命令可以获取文件夹中文件md5列表</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">find /home/ -type f -not \( -name '.*' \) -exec md5sum {} \;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">说明：(1)&nbsp;/home/文件目录</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">　　 &nbsp; (2)&nbsp;-type f 文件类型为普通文件</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">　　 &nbsp; (3)&nbsp;-not \( -name '.*' \) &nbsp;过滤掉隐藏文件。可以过滤掉不需要考虑的文件</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(4)&nbsp;-exec md5sum {} \; &nbsp;对每个文件执行md5sum命令&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="color: #ff0000;"><strong>&nbsp;用tar命令压缩后，比较文件的MD5是不行的。tar压缩会带上文件的时间</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><img src="https://images2015.cnblogs.com/blog/466768/201703/466768-20170303081158188-1578572513.png" alt="" style="border: 0px; max-width: 900px; height: auto;" /></p><img src ="http://www.cppblog.com/prayer/aggbug/216335.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-04-04 17:46 <a href="http://www.cppblog.com/prayer/archive/2019/04/04/216335.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux下提示/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found 解决办法</title><link>http://www.cppblog.com/prayer/archive/2019/04/04/216333.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 04 Apr 2019 02:15:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/04/04/216333.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216333.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/04/04/216333.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216333.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216333.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">在rhel6.6上安装qt后，运行qtcreator提示 &#8220;核心载入失败: /opt/Qt5.3.2/Tools/QtCreator/lib/qtcreator/plugins/libCore.so:&nbsp;无法加载库/opt/Qt5.3.2/Tools/QtCreator/lib/qtcreator/plugins/libCore.so：(/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found&nbsp;<em id="__mceDel" style="margin: 0px; padding: 0px; line-height: 1.5;">(required by /opt/Qt5.3.2/Tools/QtCreator/lib/qtcreator/plugins/libCore.so))</em></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;"><em id="__mceDel" style="margin: 0px; padding: 0px; line-height: 1.5;"><em id="__mceDel" style="margin: 0px; padding: 0px;"></em></em>在网上查找资料后，解决方法如下<em id="__mceDel" style="margin: 0px; padding: 0px; line-height: 1.5;">。</em></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">1.查看gcc版本中包含哪些库。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">strings /usr/lib64/libstdc++.so.6 | grep GLIBC</p><table border="0" align="left" style="margin: 0px; padding: 0px; border-collapse: collapse; border-spacing: 0px; border-style: solid; border-color: silver; word-break: break-word; color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff; height: 22px; width: 222px;"><tbody style="margin: 0px; padding: 0px;"><tr style="margin: 0px; padding: 0px;"><td style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">GLIBCXX_3.4<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.1<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.2<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.3<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.4<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.5<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.6<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.7<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.8<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.9<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.10<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.11<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.12<br style="margin: 0px; padding: 0px;" />GLIBCXX_3.4.13<br style="margin: 0px; padding: 0px;" />GLIBC_2.2.5<br style="margin: 0px; padding: 0px;" />GLIBC_2.3<br style="margin: 0px; padding: 0px;" />GLIBC_2.4<br style="margin: 0px; padding: 0px;" />GLIBC_2.3.2<br style="margin: 0px; padding: 0px;" />GLIBCXX_FORCE_NEW<br style="margin: 0px; padding: 0px;" />GLIBCXX_DEBUG_MESSAGE_LENGTH</p></td></tr></tbody></table><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">　　可以看出不包含GLIBCXX_3.4.14</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">2.从网上下载高版本的libstdc++.so.6.0.13以上的版本，比如libstdc++.so.6.0.17。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">3.把libstdc++.so.6.0.17拷贝到/usr/lib64目录下。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">　　cp&nbsp;libstdc++.so.6.0.17 /usr/lib64/</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">4.删除libstdc++.so.6符号连接。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">　　rm&nbsp;libstdc++.so.6</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">5.新建里符号连接。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">　　ln -s libstdc++.so.6.0.17 libstdc++.so.6</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">6.再次查看就没有问题了。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; line-height: 27px; background-color: #ffffff;">libstdc++.so.6.0.17下载地址</p><img src ="http://www.cppblog.com/prayer/aggbug/216333.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-04-04 10:15 <a href="http://www.cppblog.com/prayer/archive/2019/04/04/216333.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lib lib32 lib64等lib文件夹的说明</title><link>http://www.cppblog.com/prayer/archive/2019/04/04/216332.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 04 Apr 2019 02:14:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/04/04/216332.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216332.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/04/04/216332.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216332.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216332.html</trackback:ping><description><![CDATA[<ul style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: rgba(0, 0, 0, 0.74902); background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;">/lib&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />最基本的共享库和内核模块。&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />目的 -&gt; 存放用于启动系统和执行root文件系统的命令的如/bin /sbin的二进制文件的共享库，或者存放32位，或者64位(file命令查看)。</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;">/lib、 /usr/lib、/usr/local/lib&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />目的 -&gt; 就是为了区分32位和64位而设置的目录。这种情况下/lib有可能是其符号链接。</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;"><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">/usr/lib、/usr/local/lib&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />程序和安装包的库路径。&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />说明 -&gt; 不依赖特定架构的库应该放到/usr/share中而不是这里。应用程序可以自己创建一个单独的子目录并把自己的库放在其中。</p></li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;"><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">总结&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />一般情况下：&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />系统自己会匹配应用程序与其应该调用的位数的库。&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />如果是用于/bin /sbin的库，放置到/lib*之中。&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />如果是用于用户程序或安装包的，放置到/usr/lib*或/usr/local/lib*之中。&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />如果某个库只是特别的用了某个专用程序，而这个程序但并不打算直接被用户或者root调用，应放置到/usr/libexec中。</p></li></ul><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word; background-color: #ffffff;"><a href="http://unix.stackexchange.com/questions/74646/difference-between-lib-lib32-lib64-libx32-and-libexec" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; background-color: transparent; word-wrap: break-word;">http://unix.stackexchange.com/questions/74646/difference-between-lib-lib32-lib64-libx32-and-libexec</a></p><img src ="http://www.cppblog.com/prayer/aggbug/216332.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-04-04 10:14 <a href="http://www.cppblog.com/prayer/archive/2019/04/04/216332.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux程序分析工具介绍—ldd,nm</title><link>http://www.cppblog.com/prayer/archive/2019/04/03/216328.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 03 Apr 2019 07:56:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/04/03/216328.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216328.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/04/03/216328.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216328.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216328.html</trackback:ping><description><![CDATA[<section style="background-color: #ffffff; width: 1616px; margin: auto; overflow: hidden; font-family: 宋体, arial; font-size: 12px; line-height: normal;"><div style="width: 1486.71875px; margin: 0px auto auto; line-height: 32px; font-size: 18px; text-align: justify; overflow: hidden;"><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">本文要介绍的ldd和nm是linux下，两个用来分析程序很实用的工具。ldd是用来分析程序运行时需要依赖的动态库的工具；nm是用来查看指定程序中的符号表相关内容的工具。下面通过例子，分别来介绍一下这两个工具：</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">1. ldd, 先看下面的例子, 用ldd查看cs程序所依赖的动态库：</p><ol style="font-size: 0.95em;"><li style="font-size: 0.95em;">$&nbsp;ldd&nbsp;cs&nbsp;&nbsp;</li><li style="font-size: 0.95em;">linux-gate.so.1&nbsp;=&gt;&nbsp;&nbsp;(0xffffe000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libz.so.1&nbsp;=&gt;&nbsp;/lib/libz.so.1&nbsp;(0xb7f8c000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libpthread.so.0&nbsp;=&gt;&nbsp;/lib/libpthread.so.0&nbsp;(0xb7f75000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libcrypto.so.0.9.8&nbsp;=&gt;&nbsp;/usr/lib/libcrypto.so.0.9.8&nbsp;(0xb7e4d000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libpcre.so.0&nbsp;=&gt;&nbsp;/usr/lib/libpcre.so.0&nbsp;(0xb7e21000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libstdc++.so.6&nbsp;=&gt;&nbsp;/usr/local/gcc4.5.1/lib/libstdc++.so.6&nbsp;(0xb7d40000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libm.so.6&nbsp;=&gt;&nbsp;/lib/libm.so.6&nbsp;(0xb7d18000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libgcc_s.so.1&nbsp;=&gt;&nbsp;/usr/local/gcc4.5.1/lib/libgcc_s.so.1&nbsp;(0xb7cfd000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libc.so.6&nbsp;=&gt;&nbsp;/lib/libc.so.6&nbsp;(0xb7bbc000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">/lib/ld-linux.so.2&nbsp;(0xb7fab000)&nbsp;&nbsp;</li><li style="font-size: 0.95em;">libdl.so.2&nbsp;=&gt;&nbsp;/lib/libdl.so.2&nbsp;(0xb7bb7000)&nbsp;&nbsp;</li></ol><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">在上面的例子中，ldd的结果可以分为三列来看：</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">&#8226;第一列：程序需要依赖什么库<br style="font-size: 0.95em;" />&#8226;第二列: 系统提供的与程序需要的库所对应的库<br style="font-size: 0.95em;" />&#8226;第三列：库加载的开始地址<br style="font-size: 0.95em;" />通过上面的信息，我们可以得到以下几个信息：</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">&#8226;（1） 通过对比第一列和第二列，我们可以分析程序需要依赖的库和系统实际提供的，是否相匹配<br style="font-size: 0.95em;" />&#8226;（2） 通过观察第三列，我们可以知道在当前的库中的符号在对应的进程的地址空间中的开始位置<br style="font-size: 0.95em;" />2. nm, 通过下面的例子，我们来介绍nm工具：</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">先看一下这个简单的程序：</p><ol style="font-size: 0.95em;"><li style="font-size: 0.95em;">#include&nbsp;"iostream"&nbsp;&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;</li><li style="font-size: 0.95em;"><strong style="font-size: 0.95em;">using</strong>&nbsp;<strong style="font-size: 0.95em;">namespace</strong>&nbsp;std;&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;</li><li style="font-size: 0.95em;"><strong style="font-size: 0.95em;">class</strong>&nbsp;Test&nbsp;&nbsp;</li><li style="font-size: 0.95em;">{&nbsp;&nbsp;</li><li style="font-size: 0.95em;"><strong style="font-size: 0.95em;">public</strong>:&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;<strong style="font-size: 0.95em;">void</strong>&nbsp;Hello()&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;&lt;&nbsp;&lt;&nbsp;"Hello&nbsp;world!"&nbsp;&lt;&lt;&nbsp;endl;&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li style="font-size: 0.95em;">};&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;</li><li style="font-size: 0.95em;"><strong style="font-size: 0.95em;">int</strong>&nbsp;main()&nbsp;&nbsp;</li><li style="font-size: 0.95em;">{&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;test;&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;test.Hello();&nbsp;&nbsp;</li><li style="font-size: 0.95em;">}&nbsp;&nbsp;</li></ol><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">接下来，我们编译该程序，然后看nm的结果：</p><ol style="font-size: 0.95em;"><li style="font-size: 0.95em;">$&nbsp;g++&nbsp;test.cc&nbsp;-o&nbsp;test&nbsp;&nbsp;</li><li style="font-size: 0.95em;">c$&nbsp;nm&nbsp;test&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049f10&nbsp;d&nbsp;_DYNAMIC&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049ff4&nbsp;d&nbsp;_GLOBAL_OFFSET_TABLE_&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080486f0&nbsp;t&nbsp;_GLOBAL__I_main&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080487fc&nbsp;R&nbsp;_IO_stdin_used&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w&nbsp;_Jv_RegisterClasses&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080486b0&nbsp;t&nbsp;_Z41__static_initialization_and_destruction_0ii&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804870c&nbsp;W&nbsp;_ZN4Test5HelloEv&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;_ZNSolsEPFRSoS_E@@GLIBCXX_3.4&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;_ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a040&nbsp;B&nbsp;_ZSt4cout@@GLIBCXX_3.4&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a0d4&nbsp;b&nbsp;_ZStL8__ioinit&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049f00&nbsp;d&nbsp;__CTOR_END__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049ef8&nbsp;d&nbsp;__CTOR_LIST__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049f08&nbsp;D&nbsp;__DTOR_END__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049f04&nbsp;d&nbsp;__DTOR_LIST__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080488c8&nbsp;r&nbsp;__FRAME_END__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049f0c&nbsp;d&nbsp;__JCR_END__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049f0c&nbsp;d&nbsp;__JCR_LIST__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a02c&nbsp;A&nbsp;__bss_start&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;__cxa_atexit@@GLIBC_2.1.3&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a024&nbsp;D&nbsp;__data_start&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080487b0&nbsp;t&nbsp;__do_global_ctors_aux&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08048610&nbsp;t&nbsp;__do_global_dtors_aux&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a028&nbsp;D&nbsp;__dso_handle&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w&nbsp;__gmon_start__&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;__gxx_personality_v0@@CXXABI_1.3&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080487aa&nbsp;T&nbsp;__i686.get_pc_thunk.bx&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049ef8&nbsp;d&nbsp;__init_array_end&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08049ef8&nbsp;d&nbsp;__init_array_start&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08048740&nbsp;T&nbsp;__libc_csu_fini&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08048750&nbsp;T&nbsp;__libc_csu_init&nbsp;&nbsp;</li><li style="font-size: 0.95em;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;__libc_start_main@@GLIBC_2.0&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a02c&nbsp;A&nbsp;_edata&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a0d8&nbsp;A&nbsp;_end&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080487dc&nbsp;T&nbsp;_fini&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080487f8&nbsp;R&nbsp;_fp_hw&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08048508&nbsp;T&nbsp;_init&nbsp;&nbsp;</li><li style="font-size: 0.95em;">080485e0&nbsp;T&nbsp;_start&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a0cc&nbsp;b&nbsp;completed.7065&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a024&nbsp;W&nbsp;data_start&nbsp;&nbsp;</li><li style="font-size: 0.95em;">0804a0d0&nbsp;b&nbsp;dtor_idx.7067&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08048670&nbsp;t&nbsp;frame_dummy&nbsp;&nbsp;</li><li style="font-size: 0.95em;">08048694&nbsp;T&nbsp;main&nbsp;&nbsp;</li></ol><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">上面便是test这个程序中所有的符号，首先需要介绍一下上面的内容的格式：</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">&#8226;第一列：当前符号的地址<br style="font-size: 0.95em;" />&#8226;第二列：当前符号的类型<br style="font-size: 0.95em;" />&#8226;第三列：当前符号的名称<br style="font-size: 0.95em;" />在上面的结果中，像_ZN4Test5HelloEv这样的符号，很多读者朋友可能会被它搞晕，这里介绍个小技巧，在nm的时候，加上-C选项，就可以把这些难以识别的符号，转换成便于我们阅读的符号TestHello()。这个主要是c++中的mangle机制所导致的，加上-C就是指定列出的符号是demangle了的。说了这么多，到底nm对我们程序有啥具体的帮助呢，我觉得主要有以下几个方面：<br style="font-size: 0.95em;" />（1）判断指定程序中有没有定义指定的符号 (比较常用的方式：nm -C proc | grep symbol)<br style="font-size: 0.95em;" />（2）解决程序编译时undefined reference的错误，以及mutiple definition的错误</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;">（3）查看某个符号的地址，以及在进程空间的大概位置（bss, data, text区，具体可以通过第二列的类型来判断）</p><p style="font-size: 0.95em; margin: 0px; line-height: 1.6em; padding: 0px; text-indent: 2em;"></p><blockquote style="background-color: #f3f7f0; padding-right: 5px; padding-left: 5px; margin-right: 0px; margin-left: 0px; font-size: 0.95em;"><p style="font-size: 14px; margin: 0px; line-height: 1.6; padding: 0px; text-indent: 2em;">本文要介绍的ldd和nm是linux下，两个用来分析程序很实用的工具。ldd是用来分析程序运行时需要依赖的动态库的工具；nm是用来查看指定程序中的符号表相关内容的工具。下面通过例子，分别来介绍一</p></blockquote></div></section><div style="width: 1519.03125px; margin: auto; overflow: hidden; font-family: 宋体, arial; font-size: 12px; line-height: normal; text-align: center; background-color: #ffffff;"><div style="width: 1427.875px; float: none; margin: 10px auto 0px;"><h1>相关推荐</h1></div></div><img src ="http://www.cppblog.com/prayer/aggbug/216328.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-04-03 15:56 <a href="http://www.cppblog.com/prayer/archive/2019/04/03/216328.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态库(.so)链接静态库(.a)的情况总结</title><link>http://www.cppblog.com/prayer/archive/2019/03/20/216304.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 20 Mar 2019 07:28:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/20/216304.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216304.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/20/216304.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216304.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216304.html</trackback:ping><description><![CDATA[<p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">来自：http://www.cnblogs.com/nobugtodebug/archive/2012/11/07/e6cd72c67b3dd843f40d7ce919f7336a.html</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"></p><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">动态库(.so)链接静态库(.a)的情况总结&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">一般来说在链接时想要使用静态库有三种方法：</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">1、link时加上 -static 选项；当加上 -static选项后，gcc会把所有用到的库都做静态连接。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">2、link时直接指定想要静态连接的.a文件的绝对路径。优点是除非.a文件不存在，否则肯定有效；缺点也是很明显，拿到其他机器上编译时，.a文件也必须在相同的路径下存放。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #c0c0c0;">3、在要静态连接的库前指定-Bstatic ，在要动态连接的库前指定-Bdynamic选项。连接器在看到-Bstatic时会优于去找静态库，如果找不到再去找动态库。 -Bdynamic也是同样的情况。</span>（<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ff0000;">可是我实验的结果是这两个选项根本不起作用，我的环境是centos 6.2 gcc4.7.2, 仔细看了gcc 文档，这两个选项是针对VxWorks平台的，所以不起作用。</span>）</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">当我们要编译一个so提供给外部使用，这个so本身依赖一些第三方库。但是我们却希望so的使用者不用关心该so对其他库的依赖。很自然的是会想到在编译so的时候把依赖到的第三方库静态链接进来。</div></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">我在这样做的时候碰到了问题：指定-static选项时，link失败，错误提示说要用到的object文件应该用-fPIC选项重新编译才行（也就是说，只有用-fPIC选项编译的object文件能被link到.so里）；当直接给出.a的绝对路径的时候link成功，但是.so里却并没有直正包含所用到的符合连接。针对碰到的问题，我做了一些实验。实验如下：</div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">static.c</span></div><blockquote dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 16px 16px 0px; border-left-width: 8px; border-left-color: #dddfe4; background-color: #eef0f4; overflow: auto; word-wrap: break-word; word-break: normal; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;"><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />#include &lt;stdio.h&gt;</div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">&nbsp;</div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">const char* sz_static = "i'm a static str.";</div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">&nbsp;</div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">void print_niuzai_said()<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />{<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />&nbsp;&nbsp;&nbsp; printf("in static lib, niu zai said, i'm happy!\n");<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />}</div></blockquote><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><hr style="box-sizing: border-box; outline: 0px; margin: 24px 0px; padding: 0px; border-style: none none solid; border-bottom-color: #cccccc; word-wrap: break-word;" /></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">dynamic.c</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</div><blockquote dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 16px 16px 0px; border-left-width: 8px; border-left-color: #dddfe4; background-color: #eef0f4; overflow: auto; word-wrap: break-word; word-break: normal; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;"><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">#include &lt;stdio.h&gt;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />#include "static.h"</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">void print_papa_said()<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />{<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />&nbsp;&nbsp;&nbsp; print_niuzai_said();<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />&nbsp;&nbsp;&nbsp; printf("in dynamic lib, papa said, niu zai is wonderful!\n");<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />}</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">&nbsp;</div></blockquote><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><hr style="box-sizing: border-box; outline: 0px; margin: 24px 0px; padding: 0px; border-style: none none solid; border-bottom-color: #cccccc; word-wrap: break-word;" /></div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">main.c</span></div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</div><blockquote dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 16px 16px 0px; border-left-width: 8px; border-left-color: #dddfe4; background-color: #eef0f4; overflow: auto; word-wrap: break-word; word-break: normal; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;"><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">#include &lt;unistd.h&gt;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />#include "dynamic.h"</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">&nbsp;</div><div dir="ltr" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;">int main(int argc, char** argv)<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />{<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />&nbsp;&nbsp;&nbsp; print_papa_said();<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />&nbsp;&nbsp;&nbsp; return 0;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />}</div></blockquote><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">分别用两组命令编译出了两个.a 文件</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">1、gcc -o static.o -c static.c</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ar -r libstatic.a static.o</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">2、gcc -o static_shared.o -shared -fPIC -c static.c</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ar -r libstatic_shared.a static_shared.o</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">然后用此命令&nbsp; "gcc -o dynamic.o -c dynamic.c"&nbsp; 编译出dynamic.o文件</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">接下来就是本文的主题了，链接生成libdynamic.so.此时有这么几种情况：</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">1、使用 "gcc -o libdynamic.so -shared -fPIC -L. -lstatic dynamic.o "，连接成功，但.so里实际上没有static.o里的内容。<img src="http://images.cnblogs.com/cnblogs_com/nobugtodebug/201211/20121107132126610.png" border="0" alt="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; max-width: 100%; word-wrap: break-word; cursor: -webkit-zoom-in; border: 0px;" /></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">2、使用"gcc -o libdynamic.so dynamic.o -shared -fPIC -L. -lstatic",连接失败<img src="http://images.cnblogs.com/cnblogs_com/nobugtodebug/201211/201211071321265593.png" border="0" alt="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; max-width: 100%; word-wrap: break-word; cursor: -webkit-zoom-in; border: 0px;" />&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">3、使用 "gcc -o libdynamic.so -shared -fPIC -L. -lstatic_shared dynamic.o"，连接成功，但.so里实际上没有static.o里还是没有的内容。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><img src="http://images.cnblogs.com/cnblogs_com/nobugtodebug/201211/201211071321266673.png" border="0" alt="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; max-width: 100%; word-wrap: break-word; cursor: -webkit-zoom-in; border: 0px;" />&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">4、使用 "gcc -o libdynamic.so dynamic.o -shared -fPIC -L. -lstatic_shared",连接成功，.a的内容被连接到了.so里面。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><img src="http://images.cnblogs.com/cnblogs_com/nobugtodebug/201211/20121107132126577.png" border="0" alt="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; max-width: 100%; word-wrap: break-word; cursor: -webkit-zoom-in; border: 0px;" />&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">另外，链接静态生成可执行程序时，静态库是不是用 "-shared -fPIC" 选项编译产生的没有影响。都能正常生成可执行程序。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">综合以上情况，总结如下：</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">1、动态连接库中用到的object文件必须是用 "-shared -fPIC"选项编译产生的，否则连接时要么报错，要么被忽略。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">2、静态库中的object文件最好也用"-shared -fPIC"选项编译，这样静态库就可以同时被连接到.so&nbsp;或者可执行性文件中。</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">3、动态库只能连接用"-shared -fPIC"选项编译出来的静态库（和第1点是同一件事）</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">4、连接选项的顺序对连接器的行为有重要影响！</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; word-wrap: break-word; overflow-x: auto;"><table border="1" style="box-sizing: border-box; outline: 0px; margin: 0px auto; padding: 0px; border-collapse: collapse; border-spacing: 0px; width: 852px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0;"><tbody style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border: 0px; word-wrap: break-word;"><tr bgcolor="#008000" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-top-style: solid; border-top-color: #dddddd; word-wrap: break-word;"><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 137.429px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ffff00;">静态库使用-shared -fPIC</span></td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ffff00;">连接命令</span></td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ffff00;">连接执行结果</span></td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ffff00;">动态库结果</span></td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ffff00;">最终结果</span></td></tr><tr style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-top-style: solid; border-top-color: #dddddd; background-color: #f7f7f7; word-wrap: break-word;"><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 137.429px;">否</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">gcc -o libdynamic.so -shared -fPIC -L. -lstatic dynamic.o</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">成功</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">静态库内容没有被连接</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">失败</td></tr><tr style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-top-style: solid; border-top-color: #dddddd; word-wrap: break-word;"><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 137.429px;">否</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">gcc -o libdynamic.so dynamic.o -shared -fPIC -L. -lstatic</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">失败</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">&nbsp;</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">失败</td></tr><tr style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-top-style: solid; border-top-color: #dddddd; background-color: #f7f7f7; word-wrap: break-word;"><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 137.429px;">是</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">gcc -o libdynamic.so -shared -fPIC -L. -lstatic_shared dynamic.o</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">成功</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">静态库内容没有被连接</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">失败</td></tr><tr style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-top-style: solid; border-top-color: #dddddd; word-wrap: break-word;"><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 137.429px;">是</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">gcc -o libdynamic.so dynamic.o -shared -fPIC -L. -lstatic_shared</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">成功</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">静态库内容被连接</td><td style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; word-wrap: break-word; border-style: solid; border-color: #c0c0c0; font-size: 14px; color: #4f4f4f; line-height: 22px; border-collapse: collapse; width: 136.857px;">成功</td></tr></tbody></table></div></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</div><div></div><img src ="http://www.cppblog.com/prayer/aggbug/216304.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-20 15:28 <a href="http://www.cppblog.com/prayer/archive/2019/03/20/216304.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>理清gcc、libc、libstdc++的关系</title><link>http://www.cppblog.com/prayer/archive/2019/03/18/216301.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 18 Mar 2019 06:27:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/18/216301.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216301.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/18/216301.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216301.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216301.html</trackback:ping><description><![CDATA[<div><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">当你在linux下写C/C++代码的时候，是不是会遇到许多编译链接的问题？ 时不时报个glibc,gcc，g++等相关的错误？ 很多时候都无从下手，而且比较混乱。 这也是编译链接过程中经常出现的问题。</p><a id="more" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #555555; cursor: pointer; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; word-wrap: break-word; background-color: #ffffff; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; line-height: 28px;"></a><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">这篇文章不是去介绍如何编译链接，而是理清编译链接过程中碰到的一些概念和出现的问题。尤其是，libc,glib,glibc,eglibc,libc++,libstdc++,gcc,g++。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">从libc说起。<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />libc是Linux下原来的标准C库，也就是当初写hello world时包含的头文件#include &lt; stdio.h&gt; 定义的地方。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">后来逐渐被glibc取代，也就是传说中的GNU C Library,在此之前除了有libc，还有klibc,uclibc。现在只要知道用的最多的是glibc就行了，主流的一些linux操作系统如 Debian, Ubuntu，Redhat等用的都是glibc（或者其变种，下面会说到).</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">那glibc都做了些什么呢？ glibc是<a href="https://www.baidu.com/s?wd=Linux%E7%B3%BB%E7%BB%9F&amp;tn=24004469_oem_dg&amp;rsv_dl=gh_pl_sl_csd" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">Linux系统</a>中最底层的API，几乎其它任何的运行库都要依赖glibc。 glibc最主要的功能就是对系统调用的封装，你想想看，你怎么能在C代码中直接用fopen函数就能打开文件？ 打开文件最终还是要触发系统中的sys_open系统调用，而这中间的处理过程都是glibc来完成的。<a href="http://www.chongh.wiki/blog/2016/04/08/linux-syscalls/" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #555555; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc;">这篇文章</a>详细介绍了glibc是如何与上层应用程序和系统调用交互的。除了封装系统调用，glibc自身也提供了一些上层应用函数必要的功能,如string,malloc,stdlib,linuxthreads,locale,signal等等。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">好了，那eglibc又是什么？ 这里的e是Embedded的意思，也就是前面说到的变种glibc。eglibc的主要特性是为了更好的支持嵌入式架构，可以支持不同的shell(包括嵌入式)，但它是二进制兼容glibc的，就是说如果你的代码之前依赖eglibc库，那么换成glibc后也不需要重新编译。<a href="https://www.baidu.com/s?wd=ubuntu%E7%B3%BB%E7%BB%9F&amp;tn=24004469_oem_dg&amp;rsv_dl=gh_pl_sl_csd" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">ubuntu系统</a>用的就是eglibc（而不是glibc）,不信，你执行 ldd &#8211;version 或者 /lib/i386-linux-gnu/libc.so.6<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />(64位系统运行/lib/x86_64-linux-gnu）看看，便会显示你系统中eglibc/glibc的版本信息。 这里提到了libc.so.6,这个文件就是eglibc/glibc编译后的生成库文件。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">还有一个glib看起来也很相似，那它又是什么呢？glib也是个c程序库，不过比较轻量级，glib将C语言中的数据类型统一封装成自己的数据类型，提供了C语言常用的数据结构的定义以及处理函数，有趣的宏以及可移植的封装等(注：glib是可移植的，说明你可以在linux下，也可以在windows下使用它）。那它跟glibc有什么关系吗？其实并没有，除非你的程序代码会用到glib库中的数据结构或者函数，glib库在ubuntu系统中并不会默认安装(可以通过apt-get install libglib2.0-dev手动安装)，著名的GTK+和Gnome底层用的都是glib库。想更详细了解glib？ 可以参考<a href="https://developer.gnome.org/glib/" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #555555; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc;">这里</a></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">看到这里，你应该知道这些库有多重要了吧？ 你写的C代码在编译的过程中有可能出现明明是这些库里面定义的变,却量还会出现&#8217;Undefined&#8217;, &#8216;Unreference&#8217;等错误，这时候你可能会怀疑是不是这些库出问题了？ 是不是该动手换个gilbc/eglibc了？ 这里强调一点，在你准备更换/升级这些库之前，你应该好好思考一下，你真的要更换/升级吗？你要知道你自己在做什么！你要时刻知道glibc/eglibc的影响有多大，不管你之前部署的什么程序，linux系统的ls,cd,mv,ps等等全都得依赖它，很多人在更换/升级都有过惨痛的教训，甚至让整个系统奔溃无法启动。所以，强烈不建议更换/升级这些库！</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">当然如果你写的是C++代码，还有两个库也要非常重视了，libc++/libstdc++,这两个库有关系吗？有。两个都是C++标准库。libc++是针对clang编译器特别重写的C++标准库，那libstdc++自然就是gcc的事儿了。libstdc++与gcc的关系就像clang与libc++. 其中的区别这里不作详细介绍了。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">再说说libstdc++，glibc的关系。 libstdc++与gcc是捆绑在一起的，也就是说安装gcc的时候会把libstdc++装上。 那为什么glibc和gcc没有捆绑在一起呢？<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />相比glibc，libstdc++虽然提供了c++程序的标准库，但它并不与内核打交道。对于系统级别的事件，libstdc++首先是会与glibc交互，才能和内核通信。相比glibc来说，libstdc++就显得没那么基础了。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">说完了这些库，这些库最终都是拿来干嘛的？当然是要将它们与你的程序链接在一起！ 这时候就不得不说说gcc了(当然还有前文提到的clang以及llvm等编译器，本文就不细说它们的区别了)。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">你写的C代码.c文件通过gcc首先转化为汇编.S文件，之后汇编器as将.S文件转化为机器代码.o文件，生成的.o文件再与其它.o文件，或者之前提到的libc.so.6库文件通过ld链接器链接在一块生成可执行文件。当然，在你编译代码使用gcc的时候，gcc命令已经帮你把这些细节全部做好了。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">那g++是做什么的? 慢慢说来，不要以为gcc只能编译C代码，g++只能编译c++代码。 后缀为.c的，gcc把它当作是C程序，而g++当作是c++程序；后缀为.cpp的，两者都会认为是c++程序，注意，虽然c++是c的超集，但是两者对语法的要求是有区别的。在编译阶段，g++会调用gcc,对于c++代码，两者是等价的，但是因为gcc命令不能自动和C++程序使用的库联接，需要这样，gcc -lstdc++, 所以如果你的Makefile文件并没有手动加上libstdc++库，一般就会提示错误，要求你安装g++编译器了。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">好了，就说到这，理清这些库与编译器之间的关系，相信会对你解决编译链接过程中遇到的错误起到一点帮助。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">如果你的编译器不支持一些新的C/C++特性，想升级gcc/g++, 这里也给出一个基于ubuntu系统的参考方法。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">添加ppa</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; white-space: pre-wrap; word-wrap: break-word; overflow: auto; font-size: 13px; line-height: 1.6; color: #4d4d4c; text-align: justify; background-color: #f7f7f7;"><code cs"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; color: #383a42; background-image: none; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; line-height: 22px; white-space: pre; word-wrap: break-word; font-size: 14px; background-position: initial initial; background-repeat: initial initial;">sudo <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">add</span>-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">get</span> update </code></pre><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">添加ppa，是因为你所用的ubuntu版本的更新源中可能并没有你想要的gcc/g++版本。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">安装新版gcc/g++</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; white-space: pre-wrap; word-wrap: break-word; overflow: auto; font-size: 13px; line-height: 1.6; color: #4d4d4c; text-align: justify; background-color: #f7f7f7;"><code sql"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; color: #383a42; background-image: none; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; line-height: 22px; white-space: pre; word-wrap: break-word; font-size: 14px; background-position: initial initial; background-repeat: initial initial;">sudo apt-get <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">install</span> gcc<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #986801; word-wrap: break-word;">-4.8</span> sudo apt-<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">get</span> <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">install</span> g++<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #986801; word-wrap: break-word;">-4.8</span> </code></pre><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">可以到/usr/bin/gcc查看新安装的gcc,g++</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">配置系统gcc/g++</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">使用update-alternatives,统一更新gcc/g++</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; white-space: pre-wrap; word-wrap: break-word; overflow: auto; font-size: 13px; line-height: 1.6; color: #4d4d4c; text-align: justify; background-color: #f7f7f7;"><code sql"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; color: #383a42; background-image: none; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; line-height: 22px; white-space: pre; word-wrap: break-word; font-size: 14px; background-position: initial initial; background-repeat: initial initial;">sudo <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">update</span>-alternatives <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a0a1a7; font-style: italic; word-wrap: break-word;">--install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6</span> sudo <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">update</span>-alternatives <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a0a1a7; font-style: italic; word-wrap: break-word;">--install /usr/bin/gcc gcc /usr/bin/gcc-4.8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8</span> sudo <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word;">update</span>-alternatives <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a0a1a7; font-style: italic; word-wrap: break-word;">--config gcc</span> </code></pre><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">数字优先级(如60，80)高的会被系统选择为默认的编译器,也可以执行第三条命令就是来手动配置系统的gcc,此处按照提示,选择4.8版本的即可。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Lato, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #555555; line-height: 28px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">转载：http://www.chongh.wiki/blog/2016/05/25/lib-compile-link/</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216301.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-18 14:27 <a href="http://www.cppblog.com/prayer/archive/2019/03/18/216301.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux so 动态库中全局变量 被覆盖问题</title><link>http://www.cppblog.com/prayer/archive/2019/03/13/216294.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 13 Mar 2019 08:19:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/13/216294.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216294.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/13/216294.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216294.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216294.html</trackback:ping><description><![CDATA[<h2>问题描述</h2><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; border: 0px;">回归正题，前段时间项目开发中，实现了一个动态库，封装了一些方法。然后基于这个动态库，实现了一个应用程序。应用程序中含有全局变量A，动态库中也含有全局变量A，当我调用动态库中函数后，发现应用程序的A发生了变化！！！O，My God！对于我这种还没在Linux下做过开发的人来说，一头雾水。。。。。。 于是我尝试着，将A中的变量名称改为B，这样问题也就没有了~~~</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; border: 0px;"></p><h2><a name="t1" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: rgb(78, 161, 219); cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"></a>原因</h2><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; border: 0px;">应用程序进行链接的时候，动态库中全局变量定义，将会被应用程序中同名的全局变量所覆盖。这样也就造成了，在动态库中修改A变量时，应用程序中的A也发生了变化。</p><h2><a name="t2" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: rgb(78, 161, 219); cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"></a>解决方法</h2><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; border: 0px;">在创建动态链接库时，gcc/g++选项中添加编译选项</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 22px; background-color: #ffffff;"><code hljs"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; display: block; overflow-x: auto; color: #abb2bf; background-color: #282c34; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; white-space: pre; word-wrap: break-word;">-Wl,-Bsymbolic.</code></pre><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; word-wrap: break-word; color: #333333; background-color: #ffffff;">其中</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; word-wrap: break-word; color: #333333; background-color: #ffffff; border-width: 0px;">Wl</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; word-wrap: break-word; color: #333333; background-color: #ffffff;">表示将紧跟其后的参数，传递给连接器</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; word-wrap: break-word; color: #333333; background-color: #ffffff; border-width: 0px;">ld<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; border-width: 0px;">。</span>Bsymbolic</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; word-wrap: break-word; color: #333333; background-color: #ffffff;">表示强制采用本地的全局变量定义，这样就不会出现动态链接库的全局变量定义被应用程序的同名定义给覆盖了！</span><img src ="http://www.cppblog.com/prayer/aggbug/216294.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-13 16:19 <a href="http://www.cppblog.com/prayer/archive/2019/03/13/216294.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GCC的符号可见性——解决多个库同名符号冲突问题</title><link>http://www.cppblog.com/prayer/archive/2019/03/04/216284.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 04 Mar 2019 03:45:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/04/216284.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216284.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/04/216284.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216284.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216284.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 问题最近项目遇到一些问题，场景如下主程序依赖了两个库libA的funcA函数和libB的funcB函数。示意的代码(main.cpp)如下：#include &lt;cstdio&gt;  int funcA(int, int); int funcB(int, int);  int main() {     printf("%d,", funcA(2, 1));     printf("%d\n"...&nbsp;&nbsp;<a href='http://www.cppblog.com/prayer/archive/2019/03/04/216284.html'>阅读全文</a><img src ="http://www.cppblog.com/prayer/aggbug/216284.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-04 11:45 <a href="http://www.cppblog.com/prayer/archive/2019/03/04/216284.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>链接与自定义函数名同名的库函数</title><link>http://www.cppblog.com/prayer/archive/2019/03/04/216283.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 04 Mar 2019 03:37:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/04/216283.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216283.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/04/216283.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216283.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216283.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 遇到一个问题: 封装SQLite3成静态库，过程中发现SQLite3的源码的shell.c中有main函数：int SQLITE_CDECL main(int argc, char **argv){   char *zErrMsg = 0;   ShellState data;   const char *zInitFile = 0;   int i;   int rc = 0;   int wa...&nbsp;&nbsp;<a href='http://www.cppblog.com/prayer/archive/2019/03/04/216283.html'>阅读全文</a><img src ="http://www.cppblog.com/prayer/aggbug/216283.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-04 11:37 <a href="http://www.cppblog.com/prayer/archive/2019/03/04/216283.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>各unix平台动态连接库的编译与使用</title><link>http://www.cppblog.com/prayer/archive/2019/03/01/216276.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Fri, 01 Mar 2019 09:58:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/01/216276.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216276.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/01/216276.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216276.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216276.html</trackback:ping><description><![CDATA[<div>http://blog.itpub.net/283313/viewspace-2139257/<br /><br /><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">unix平台下，动态连接库的使用有两种方式，link方式和dlopen方式。需要设置LD_LIBRARY_PATH作为查找路径（低版本AIX使用LIBPATH）。link方式是程序启动时加载所有编译时连接的动态库，直到程序结束也不会关闭。dlopen方式是在运行中按照参数指定的方式加载动态库，并可以使用dlclose函数关闭，直到下次再使用dlopen重新加载。因此，link方式无法做到不重启应用程序而更新动态库版本，dlopen方式可以。</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">各平台动态连接库的编译参数如下。文件wel.c，编译成libwel.so（HPUX为libwel.sl）。</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"></p><div id="codeText" style="margin: 0px 0px 1.1em; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: Consolas, monospace; border: 1px solid #dddddd; width: 872.1875px; overflow: auto; word-break: break-all; background-color: #ffffff; font-size: 12px; line-height: normal; color: #444444;"><ol start="1" style="margin: 0px 1px 0px 0px; padding: 0px 0px 0px 40px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; list-style: disc; color: #5c5c5c; line-height: 1.3;"><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #000000;">#include&nbsp;<span style="color: #0000cc;">&lt;</span>stdio<span style="color: #0000cc;">.</span>h<span style="color: #0000cc;">&gt;</span><br /></span></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">void welcome<span style="color: #0000cc;">(</span>void<span style="color: #0000cc;">)</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #0000cc;">{</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;puts<span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"welcome,baby!"</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #0000cc;">}</span></li></ol></div><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br />&nbsp;linux:<br />&nbsp;&nbsp;编译libwel.so:<br />&nbsp;&nbsp;&nbsp;gcc -fPIC -shared -o libwel.so wel.c -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;-fPIC&nbsp;&nbsp;用相对地址定位<br />&nbsp;&nbsp;&nbsp;&nbsp;-shared&nbsp;&nbsp;生成动态连接库</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;sco unix open server:<br />&nbsp;&nbsp;编译libwel.so:<br />&nbsp;&nbsp;&nbsp;cc -K PIC -G -o libwel.so wel.c -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;-K PIC&nbsp;&nbsp;用相对地址定位<br />&nbsp;&nbsp;&nbsp;&nbsp;-G&nbsp;&nbsp;生成动态连接库，包含了-b elf选项</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;AIX:<br />&nbsp;&nbsp;编译libwel.so:<br />&nbsp;&nbsp;&nbsp;xlc -G -bnoentry -o libwel.so wel.c -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;-G&nbsp;&nbsp;指定生成动态连接库<br />&nbsp;&nbsp;&nbsp;&nbsp;-bnoentry&nbsp;动态库无需模块入口（低版本AIX需要此参数）</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;HP UX:<br />&nbsp;&nbsp;编译libwel.so:<br />&nbsp;&nbsp;&nbsp;cc +z -c wel.c<br />&nbsp;&nbsp;&nbsp;ld -b -o libwel.sl wel.o -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;+z&nbsp;&nbsp;用相对地址定位<br />&nbsp;&nbsp;&nbsp;&nbsp;-b&nbsp;&nbsp;生成动态连接库</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;SUN OS: 很类似SCO，man帮助不全面<br />&nbsp;&nbsp;编译libwel.so:<br />&nbsp;&nbsp;&nbsp;cc -G -o libwel.so wel.c -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;-G&nbsp;&nbsp;生成动态连接库</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">link方式使用动态连接库的编译参数如下。文件welcome.c，编译成welcome可执行程序。</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"></p><div id="codeText" style="margin: 0px 0px 1.1em; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: Consolas, monospace; border: 1px solid #dddddd; width: 872.1875px; overflow: auto; word-break: break-all; background-color: #ffffff; font-size: 12px; line-height: normal; color: #444444;"><ol start="1" style="margin: 0px 1px 0px 0px; padding: 0px 0px 0px 40px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; list-style: disc; color: #5c5c5c; line-height: 1.3;"><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #000000;">extern void welcome<span style="color: #0000cc;">(</span>void<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></span></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #ff0000;">int</span>&nbsp;main<span style="color: #0000cc;">(</span>void<span style="color: #0000cc;">)</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #0000cc;">{</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;welcome<span style="color: #0000cc;">(</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0<span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #0000cc;">}</span></li></ol></div><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;linux:<br />&nbsp;&nbsp;&nbsp;gcc -rdynamic -o welcome welcome.c -L. -lwel<br />&nbsp;&nbsp;&nbsp;&nbsp;-rdynamic&nbsp;&nbsp;导出所有符号供动态使用</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;sco unix open server:<br />&nbsp;&nbsp;&nbsp;cc -b elf -o welcome welcome.c -L. -lwel<br />&nbsp;&nbsp;&nbsp;&nbsp;-b elf&nbsp;&nbsp;必须使用ELF的可执行文件格式</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;AIX:<br />&nbsp;&nbsp;&nbsp;xlc -bdynamic -brtl -o welcome welcome.c -L. -lwel<br />&nbsp;&nbsp;&nbsp;&nbsp;-bdynamic&nbsp;使用动态连接方式，系统默认为此方式<br />&nbsp;&nbsp;&nbsp;&nbsp;-brtl&nbsp;&nbsp;-l参数使用.so为库</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;HP UX:<br />&nbsp;&nbsp;&nbsp;cc -dynamic -o welcome welcome.c -L. -lwel<br />&nbsp;&nbsp;&nbsp;&nbsp;-dynamic&nbsp;使用动态连接方式，系统默认为此方式</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;SUN OS:<br />&nbsp;&nbsp;编译libwel.so:<br />&nbsp;&nbsp;&nbsp;cc -B dynamic -o welcome welcome.c -L. -lwel<br />&nbsp;&nbsp;&nbsp;&nbsp;-B dynamic&nbsp;使用动态连接方式</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">dlopen方式，其实就是把打开动态库和查找符号的动作放在程序中执行。因此编译可执行文件与普通的相同，只要连接libdl.so库即可。文件welcome.c，编译成welcome可执行程序。</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"></p><div id="codeText" style="margin: 0px 0px 1.1em; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: Consolas, monospace; border: 1px solid #dddddd; width: 872.1875px; overflow: auto; word-break: break-all; background-color: #ffffff; font-size: 12px; line-height: normal; color: #444444;"><ol start="1" style="margin: 0px 1px 0px 0px; padding: 0px 0px 0px 40px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; list-style: disc; color: #5c5c5c; line-height: 1.3;"><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #000000;">#include&nbsp;<span style="color: #0000cc;">&lt;</span>dlfcn<span style="color: #0000cc;">.</span>h<span style="color: #0000cc;">&gt;</span><br /></span></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #ff0000;">int</span>&nbsp;main<span style="color: #0000cc;">(</span>void<span style="color: #0000cc;">)</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #0000cc;">{</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;<span style="color: #0000cc;">*</span>handle<span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;<span style="color: #0000cc;">(</span><span style="color: #0000cc;">*</span>welcome<span style="color: #0000cc;">)</span><span style="color: #0000cc;">(</span>void<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;handle<span style="color: #0000cc;">=</span>dlopen<span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"libwel.so"</span><span style="color: #0000cc;">,</span>RTLD_LAZY<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;welcome<span style="color: #0000cc;">=</span>dlsym<span style="color: #0000cc;">(</span>handle<span style="color: #0000cc;">,</span><span style="color: #ff00ff;">"welcome"</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">(</span><span style="color: #0000cc;">*</span>welcome<span style="color: #0000cc;">)</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;return 0<span style="color: #0000cc;">;</span><br /></li><li style="margin: 0px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><span style="color: #0000cc;">}</span></li></ol></div><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br />&nbsp;linux:<br />&nbsp;&nbsp;&nbsp;gcc -rdynamic -o welcome welcome.c -ldl<br />&nbsp;&nbsp;&nbsp;&nbsp;-rdynamic&nbsp;导出所有符号供动态使用<br />&nbsp;&nbsp;&nbsp;&nbsp;-ldl&nbsp;&nbsp;用系统libdl.so库dlopen(),dlsym(),dlerror(),dlclose()</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;sco unix open server:<br />&nbsp;&nbsp;&nbsp;cc -b elf -o welcome welcome.c -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;-b elf&nbsp;&nbsp;以elf格式，动态连接库必须</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;AIX:<br />&nbsp;&nbsp;&nbsp;xlc -bexpall -o welcome welcome.c -ldl -lc<br />&nbsp;&nbsp;&nbsp;&nbsp;-bexpall&nbsp;使主程序的符号可以被动态库使用</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;HP UX:<br />&nbsp;&nbsp;&nbsp;cc -o welcome welcome.c -ldl -lc</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">&nbsp;SUN OS:<br />&nbsp;&nbsp;&nbsp;cc -o welcome welcome.c -ldl -lc</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">------------------------------------------------------------------------------<br />参照的man手册：<br />&nbsp;cc, ld, ldd, dlopen, dlclose, dlerror, dlsym, CC</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;">另：AIX运行动态库时会加载系统内存统一管理，导致即使程序已退出，编译时仍然认为还在使用而无法覆盖。有两个办法，一是root用户执行slibclean，清理系统加载而无人使用的动态库，另一种是编译的库chmod go-rwx，使得组和其他人无权限，这种库系统会认为用户特有而不加载。</p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 28px; font-size: 16px; color: #666666; text-align: justify; background-color: #ffffff;"><br /></p><p style="margin: 0px 0px 18px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; color: #666666; font-size: 16px; line-height: 28px; text-align: justify; background-color: #ffffff;">来自 &#8220; ITPUB博客 &#8221; ，链接：http://blog.itpub.net/283313/viewspace-2139257/，如需转载，请注明出处，否则将追究法律责任。</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216276.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-01 17:58 <a href="http://www.cppblog.com/prayer/archive/2019/03/01/216276.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell脚本报错"[: =: unary operator expected"的解决方案</title><link>http://www.cppblog.com/prayer/archive/2019/03/01/216275.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Fri, 01 Mar 2019 07:20:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/03/01/216275.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216275.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/03/01/216275.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216275.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216275.html</trackback:ping><description><![CDATA[<p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">&nbsp; 在匹配字符串相等时，我用了类似这样的语句：</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">if [ $STATUS == "OK" ]; then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">echo "OK"</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">fi</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">&nbsp;&nbsp;&nbsp; 在运行时出现了&#8220; [: =: unary operator expected&#8221; 的错误，就一直找不到原因，尝试了删除等号两侧的空格和括号里的空格都不管用。最后<a href="https://www.baidu.com/s?wd=baidu&amp;tn=24004469_oem_dg&amp;rsv_dl=gh_pl_sl_csd" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">baidu</a>了一下，才找到原因，在条件表达式外再添加一层&#8220;[]&#8221;，就不会出错了，如下：</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">if [[ $STATUS = "OK" ]];&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">then &nbsp;</span>&nbsp;&nbsp;</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">echo "OK"</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">fi&nbsp;</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">&nbsp;&nbsp;&nbsp; 究其原因，是因为如果变量STATUS值为空，那么<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Arial; word-wrap: break-word;">条件表达式</span>就成了 [ = "OK"] ，显然 [ 和 "OK" 不相等并且缺少了&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">[ 符号，所以报了这样的错误。当然不总是出错，如果变量STATUS值不为空，程序就正常了，所以这样的错误还是很隐蔽的。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">&nbsp;&nbsp;&nbsp; 或者，用下面的方法也能避免这种错误：</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">if [ "$STATUS"x == "OK"x ]; then&nbsp;&nbsp;&nbsp;</span>&nbsp;&nbsp;</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">echo</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">"OK"</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">fi。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">当然，x也可以是其他字符。顺便提一点，shell中有没有双引号在很多情况下是一致的。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">转自：<a href="http://hi.baidu.com/vishare/blog/item/bd8ab9ee289753252cf53417.html" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #336699; text-decoration: none; cursor: pointer; word-wrap: break-word;">http://hi.baidu.com/vishare/blog/item/bd8ab9ee289753252cf53417.html</a></span></p><img src ="http://www.cppblog.com/prayer/aggbug/216275.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-03-01 15:20 <a href="http://www.cppblog.com/prayer/archive/2019/03/01/216275.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux 静态库的链接 undefined reference</title><link>http://www.cppblog.com/prayer/archive/2019/02/28/216273.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 28 Feb 2019 08:59:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/28/216273.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216273.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/28/216273.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216273.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216273.html</trackback:ping><description><![CDATA[<div>https://blog.csdn.net/csdn66_2016/article/details/70145962<br /><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Arial; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 26px;">最近在Linux下编程发现一个诡异的现象，就是在链接一个静态库的时候总是报错，类似下面这样的错误：</span><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">(.text+0x13):&nbsp;undefined&nbsp;reference&nbsp;to&nbsp;`func'&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 关于undefined reference这样的问题，大家其实经常会遇到，在此，我以详细地示例给出常见错误的各种原因以及解决方法，希望对初学者有所帮助。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ff0000;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 16px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">1.&nbsp; 链接时缺失了相关目标文件（.o）</span></span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; font-size: 16px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 14px;">测试代码如下：</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><a href="http://img1.51cto.com/attachment/201011/133144914.png" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><img border="0" alt="" src="http://img1.51cto.com/attachment/201011/133144914.png" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 1px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a>&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 然后编译。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-c&nbsp;test.c &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;&#8211;c&nbsp;main.c&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 得到两个 .o 文件，一个是 main.o，一个是 test.o ，然后我们链接 .o 得到可执行程序：</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这时，你会发现，报错了：</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">main.o:&nbsp;In&nbsp;function&nbsp;`main': &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">main.c:(.text+0x7):&nbsp;undefined&nbsp;reference&nbsp;to&nbsp;`test' &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">collect2:&nbsp;ld&nbsp;returned&nbsp;1&nbsp;exit&nbsp;status&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这就是最典型的undefined reference错误，因为在链接时发现找不到某个函数的实现文件，本例中test.o文件中包含了test()函数的实现，所以如果按下面这种方式链接就没事了。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;test.o&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; 【扩展】：其实上面为了让大家更加清楚底层原因，我把编译链接分开了，下面这样编译也会报undefined reference错，其实底层原因与上面是一样的。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.c&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">//缺少test()的实现文件</span>&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">需要改成如下形式才能成功，将test()函数的实现文件一起编译。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.c&nbsp;test.c&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">//ok,没问题了</span>&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ff0000;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 16px;">2.&nbsp;&nbsp;&nbsp; 链接时缺少相关的库文件（.a/.so）</span></span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;在此，只举个静态库的例子，假设源码如下。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><a href="http://img1.51cto.com/attachment/201011/133734483.png" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><img border="0" alt="" src="http://img1.51cto.com/attachment/201011/133734483.png" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 1px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 先把test.c编译成静态库(.a)文件</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-c&nbsp;test.c &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">ar&nbsp;-rc&nbsp;test.a&nbsp;test.o&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 至此，我们得到了test.a文件。我们开始编译main.c</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-c&nbsp;main.c&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这时，则生成了main.o文件，然后我们再通过如下命令进行链接希望得到可执行程序。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 你会发现，编译器报错了：</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">/tmp/ccCPA13l.o:&nbsp;In&nbsp;function&nbsp;`main': &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">main.c:(.text+0x7):&nbsp;undefined&nbsp;reference&nbsp;to&nbsp;`test' &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">collect2:&nbsp;ld&nbsp;returned&nbsp;1&nbsp;exit&nbsp;status&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 其根本原因也是找不到test()函数的实现文件，由于该test()函数的实现在test.a这个静态库中的，故在链接的时候需要在其后加入test.a这个库，链接命令修改为如下形式即可。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;./test.a&nbsp;&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">//注：./&nbsp;是给出了test.a的路径</span>&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp; 【扩展】：同样，为了把问题说清楚，上面我们把代码的编译链接分开了，如果希望一次性生成可执行程序，则可以对main.c和test.a执行如下命令。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.c&nbsp;./test.a&nbsp;&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">//同样，如果不加test.a也会报错</span>&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ff0000;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 16px;">3.&nbsp;&nbsp;&nbsp; 链接的库文件中又使用了另一个库文件</span></span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这种问题比较隐蔽，也是我最近遇到的与网上大家讨论的不同的问题，举例说明如下，首先，还是看看测试代码。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><a href="http://img1.51cto.com/attachment/201011/134157339.png" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><img border="0" alt="" src="http://img1.51cto.com/attachment/201011/134157339.png" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 1px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 从上图可以看出，main.c调用了test.c的函数，test.c中又调用了fun.c的函数。<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />&nbsp;&nbsp;&nbsp; 首先，我们先对fun.c，test.c，main.c进行编译，生成 .o文件。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-c&nbsp;func.c &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-c&nbsp;test.c &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-c&nbsp;main.c&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 然后，将test.c和func.c各自打包成为静态库文件。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">ar&nbsp;&#8211;rc&nbsp;func.a&nbsp;func.o &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">ar&nbsp;&#8211;rc&nbsp;test.a&nbsp;test.o&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这时，我们准备将main.o链接为可执行程序，由于我们的main.c中包含了对test()的调用，因此，应该在链接时将test.a作为我们的库文件，链接命令如下。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;test.a&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这时，编译器仍然会报错，如下：</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">test.a(test.o):&nbsp;In&nbsp;function&nbsp;`test': &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">test.c:(.text+0x13):&nbsp;undefined&nbsp;reference&nbsp;to&nbsp;`func' &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">collect2:&nbsp;ld&nbsp;returned&nbsp;1&nbsp;exit&nbsp;status&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 就是说，链接的时候，发现我们的test.a调用了func()函数，找不到对应的实现。由此我们发现，原来我们还需要将test.a所引用到的库文件也加进来才能成功链接，因此命令如下。</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;test.a&nbsp;func.a&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; ok，这样就可以成功得到最终的程序了。同样，如果我们的库或者程序中引用了第三方库（如pthread.a）则同样在链接的时候需要给出第三方库的路径和库文件，否则就会得到undefined reference的错误。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; color: #ff0000;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 16px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">4 多个库文件链接顺序问题</span></span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 这种问题也非常的隐蔽，不仔细研究你可能会感到非常地<a href="https://www.baidu.com/s?wd=%E8%8E%AB%E5%90%8D%E5%85%B6%E5%A6%99&amp;tn=24004469_oem_dg&amp;rsv_dl=gh_pl_sl_csd" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">莫名其妙</a>。我们依然回到第3小节所讨论的问题中，在最后，如果我们把链接的库的顺序换一下，看看会发生什么结果？</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">gcc&nbsp;-o&nbsp;main&nbsp;main.o&nbsp;func.a&nbsp;test.a&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 我们会得到如下报错.</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">test.a(test.o):&nbsp;In&nbsp;function&nbsp;`test': &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">test.c:(.text+0x13):&nbsp;undefined&nbsp;reference&nbsp;to&nbsp;`func' &nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">collect2:&nbsp;ld&nbsp;returned&nbsp;1&nbsp;exit&nbsp;status&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 因此，我们需要注意，在链接命令中给出所依赖的库时，需要注意库之间的依赖顺序，依赖其他库的库一定要放到被依赖库的前面，这样才能真正避免undefined reference的错误，完成编译链接。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; font-size: 16px;">5. 在c++代码中链接c语言的库</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;如果你的库文件由c代码生成的，则在c++代码中链接库中的函数时，也会碰到undefined reference的问题。下面举例说明。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;首先，编写c语言版库文件：&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;<a href="http://img1.51cto.com/attachment/201109/150358466.png" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><img border="0" alt="" src="http://img1.51cto.com/attachment/201109/150358466.png" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 1px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;编译，打包为静态库：test.a</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: 'Courier New', monospace; white-space: pre-wrap; word-wrap: break-word; overflow: auto; line-height: 26px; background-color: #ffffff; width: 587px;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; background-color: #ffffff; border-style: none none none solid; border-left-color: #146b00; border-left-width: 3px; color: #5c5c5c;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word; border-style: none; line-height: 1.3em; list-style-position: outside !important;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #000000; border-style: none;">gcc&nbsp;-c&nbsp;test.c &nbsp;</span></li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word; border-style: none; line-height: 1.3em; list-style-position: outside !important;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #000000; border-style: none;">ar&nbsp;-rc&nbsp;test.a&nbsp;test.o&nbsp;</span></li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 至此，我们得到了test.a文件。下面我们开始编写c++文件main.cpp</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;<a href="http://img1.51cto.com/attachment/201109/150553287.png" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><img border="0" alt="" src="http://img1.51cto.com/attachment/201109/150553287.png" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 1px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;然后编译main.cpp生成可执行程序：</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">g++&nbsp;-o&nbsp;main&nbsp;main.cpp&nbsp;test.a&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;会发现报错：</p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">/tmp/ccJjiCoS.o:&nbsp;In&nbsp;function&nbsp;`main':&nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">main.cpp:(.text+0x7):&nbsp;undefined&nbsp;reference&nbsp;to&nbsp;`test()'&nbsp;</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">collect2:&nbsp;ld&nbsp;returned&nbsp;1&nbsp;exit&nbsp;status&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;原因就是main.cpp为c++代码，调用了c语言库的函数，因此链接的时候找不到，解决方法：即在main.cpp中，把与c语言库test.a相关的头文件包含添加一个extern "C"的声明即可。例如，修改后的main.cpp如下：</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;<a href="http://img1.51cto.com/attachment/201109/150959476.png" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><img border="0" alt="" src="http://img1.51cto.com/attachment/201109/150959476.png" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 1px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a></p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 26px; background-color: #ffffff;"></pre><ol style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; word-wrap: break-word; color: #333333; background-color: #ffffff;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 40px; padding: 0px; list-style-type: decimal; word-wrap: break-word;">g++&nbsp;-o&nbsp;main&nbsp;main.cpp&nbsp;test.a&nbsp;</li></ol><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp; &nbsp;再编译会发现，问题已经成功解决。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word; font-size: 16px;">6.&nbsp; 总 结</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; 当然，上面几种是我目前发现的比较常见的undefined reference错误的原因和解决方法，可能也有其他各种原因，欢迎大家来信<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#117;&#106;&#117;&#110;&#46;&#104;&#117;&#115;&#116;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #ca0000; text-decoration: none; cursor: pointer; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">lujun.hust@gmail.com</a>交流，对本文档进行补充，方面新手们解决学习过程中遇到的各种问题。</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216273.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-28 16:59 <a href="http://www.cppblog.com/prayer/archive/2019/02/28/216273.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>头文件string与string.h的区别</title><link>http://www.cppblog.com/prayer/archive/2019/02/27/216267.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 27 Feb 2019 01:58:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/27/216267.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216267.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/27/216267.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216267.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216267.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在C++中，#include&lt;iostream&gt;与#include&lt;iostream.h&gt;的区别，前者要使用更新的编译器（其实大部分编译器多比较前卫了，出了有些搞嵌入式的用变态的编译器）。喔，原来iostream是C++的头文件，iostream.h是C的头文件，即标准的C++头文件没有.h扩展名，将以前的C的头文件转化为C++的头文件后，有时加上c的前缀表示来自于c，例如...&nbsp;&nbsp;<a href='http://www.cppblog.com/prayer/archive/2019/02/27/216267.html'>阅读全文</a><img src ="http://www.cppblog.com/prayer/aggbug/216267.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-27 09:58 <a href="http://www.cppblog.com/prayer/archive/2019/02/27/216267.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++程序中出现undefined reference to ......</title><link>http://www.cppblog.com/prayer/archive/2019/02/26/216261.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Tue, 26 Feb 2019 08:28:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/26/216261.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216261.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/26/216261.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216261.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216261.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px;">在C++代码中调用C的库文件，需加上extern "C"，用来告知编译器：这是一个用C写成的库文件，请用C的方式连接他们。<br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px;">Undefined reference to 错误:这类错误是在连接过程中出现的，可能有两种原因&#8758;</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px;">一是使用者自己定义的函数或者全局变量所在源代码文件，没有被编译、连接，或者干脆还没有定义，这 需要使用者根据实际情况修改源程序，给出全局变量或者函数的定义体；</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">1) 可以头文件中类加上</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">　　#ifndef xxx_H<br style="margin: 0px; padding: 0px;" />　　#define xxx_H<br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">　　namespace xxx{</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">　　};</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">　　#endif</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">　　cpp文件中包含头文件，using namespace xxx; 或者 其他访问方式（自行百度namepace 使用方法）<br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">2）若全局 namespace xxx 被class xxx 覆盖，请加上::</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px; color: #ff0000;">3) 请注意原来的makefile文件是否包含新增的文件，并且逻辑包含关系正确。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px;">二是未定义的符号是一个标准的库函数，在源程序中使用了该库函数，而连接过程中还没有 给定相应的函数库的名称，或者是该档案库的目录名称有问题.</span></p><p data-find="_3" style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span data-find="_2" style="margin: 0px; padding: 0px; font-family: simsun; line-height: 23px;">&nbsp; &nbsp;在使用厂商新提供的库文件时，遇到了新问题，库中的函数程序不识别，同样的方法在使用以前的库时是可行的，在CSDN上求助并查阅一些资料后，问题已经解决。<br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">1）使用库的时候首先要明确库（或者头文件）是用什么编写的，如果是用C++编写的，那么在MFC中可以直接使用，如果是用c编写的，那么在#include头文件的时候要加上</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">extern &#8220;C&#8221;{</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">#include &#8220;OOXX.h&#8221;</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">}</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">注意格式换行要正确。</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">2）为什么要如1）中那样操作呢，因为在c++中编译的方法和c不用，这是由于函数的重载造成的。在c中不涉及重载，如函数func（int a，float b）编译成_func.在c++中涉及重载，如果只凭函数名是不足以判断是使用的哪个函数，所以编译的时候要加上变量，如函数func(int a,float b)编译成_func_int_float,这样不但确定了函数名，还确定了使用哪中重载。正式由于以上的不同，使得c中编的函数不能在c++中直接使 用，c++中为了兼容c的代码，提出了extern &#8220;C&#8221;的方法，在编译声明了extern &#8220;C&#8221;的函数函数时，会按照c语言的方法编译。</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #ff0000;">如果不加extern &#8220;C&#8221;，在编译的时候会出现link错误：error link 2001：unresolved extern symbol &#8220;long __stdcall func(int a,float b)@QDASCFQAE@&#8221;之类的错误，说明编译器没有识别出函数func</span></span></p><img src ="http://www.cppblog.com/prayer/aggbug/216261.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-26 16:28 <a href="http://www.cppblog.com/prayer/archive/2019/02/26/216261.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++ "multiple definition of .. first defined here"</title><link>http://www.cppblog.com/prayer/archive/2019/02/26/216260.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Tue, 26 Feb 2019 08:02:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/26/216260.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216260.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/26/216260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216260.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216260.html</trackback:ping><description><![CDATA[<div>https://www.cnblogs.com/zjutzz/p/4018816.html<br /><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">在C++中，有时候需要在不同文件中使用同一个变量。对于这类变量如果处理不当，很容易出现&#8220;multiple definition of... first defined here&#8221;的错误。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">例如，定义了如下3个文件：global.h, a.cpp, b.cpp</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//global.h:</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">ifndef</span> _GLOBAL_H_</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">define</span> _GLOBAL_H_</span>  <span style="margin: 0px; padding: 0px; color: #0000ff;">const</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">int</span> a=<span style="margin: 0px; padding: 0px;">1</span>; <span style="margin: 0px; padding: 0px; color: #0000ff;">int</span> b;  <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">endif</span></span></code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//a.cpp</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;iostream&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;stdlib.h&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">"global.h"</span></span>  <span style="margin: 0px; padding: 0px; color: #0000ff;">using</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">namespace</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">std</span>;  <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> <span style="margin: 0px; padding: 0px; color: #a31515;">test1</span><span style="margin: 0px; padding: 0px;">()</span> </span>{      <span style="margin: 0px; padding: 0px; color: #0000ff;">cout</span>&lt;&lt;<span style="margin: 0px; padding: 0px; color: #a31515;">"test1"</span>&lt;&lt;<span style="margin: 0px; padding: 0px; color: #0000ff;">endl</span>; }</code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//b.cpp</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;iostream&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;stdlib.h&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">"global.h"</span></span>  <span style="margin: 0px; padding: 0px; color: #0000ff;">using</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">namespace</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">std</span>;  <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> <span style="margin: 0px; padding: 0px; color: #a31515;">test2</span><span style="margin: 0px; padding: 0px;">()</span> </span>{     <span style="margin: 0px; padding: 0px; color: #0000ff;">cout</span>&lt;&lt;<span style="margin: 0px; padding: 0px; color: #a31515;">"test2"</span>&lt;&lt;<span style="margin: 0px; padding: 0px; color: #0000ff;">endl</span>; }  <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> <span style="margin: 0px; padding: 0px; color: #a31515;">main</span><span style="margin: 0px; padding: 0px;">()</span> </span>{   <span style="margin: 0px; padding: 0px; color: #0000ff;">cout</span>&lt;&lt;<span style="margin: 0px; padding: 0px; color: #a31515;">"hello world"</span>&lt;&lt;<span style="margin: 0px; padding: 0px; color: #0000ff;">endl</span>; }</code></pre><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">执行编译命令：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;">g++ -o main a.cpp b.cpp</code></pre><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">提示错误为：</p><blockquote style="margin: 20px 0px; padding: 20px 50px; background-image: url(https://www.cnblogs.com/skins/iMetro/images/quote.png); background-color: #f0f0f0; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; color: #000000; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-position: -75px -20px; background-repeat: no-repeat no-repeat;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">[chris@zz jojo]g++ -o main a.cpp b.cpp<br style="margin: 0px; padding: 0px;" />/tmp/ccc7OcsO.o:(.bss+0x0): multiple definition of `b'<br style="margin: 0px; padding: 0px;" />/tmp/ccs7q2VA.o:(.bss+0x0)：第一次在此定义</p></blockquote><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">出错原因：a.cpp和b.cpp先分别被编译为.o格式的目标文件，两个目标文件再被链接器链接起来，这当中a.cpp和b.cpp分别进行了一次include，相当于global.h中的代码重复出现了一次。因为a是const类型，所以重新定义也没事；但是b只是普通变量，重复定义显然不行。</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">显然，一个解决办法是<strong style="margin: 0px; padding: 0px;">把b定义为const int类型。或者，定义成static int类型</strong>也行。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">还有一种解决方案，就是<strong style="margin: 0px; padding: 0px;">把global.h变为global.c文件，a.cpp和b.cpp中不再include它，但是编译的时候把global.c也编译进去</strong>，就可以了：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;">g++ -o main global.c  a.cpp b.cpp</code></pre><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">再举一个class相关的例子。比如有Body和Mouth两个类，Body的greet方法会调用Mouth的say方法，而main函数中会调用全局变量body的greet方法。为了只是用一个body和一个mouth对象，可以这么写：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//body.h</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">ifndef</span> BODY_H</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">define</span> BODY_H</span>  <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;mouth.h&gt;</span></span>  <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">class</span> <span style="margin: 0px; padding: 0px; color: #a31515;">Body</span> {</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">public</span>:     Body();     ~Body();     <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> <span style="margin: 0px; padding: 0px; color: #a31515;">greet</span><span style="margin: 0px; padding: 0px;">()</span></span>; };  <span style="margin: 0px; padding: 0px; color: #0000ff;">extern</span> Body body;   <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">endif</span></span> </code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//body.cpp</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;body.h&gt;</span></span>  Body::Body(){}  Body::~Body() {}  <span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> Body::greet() {     mouth.say(); } </code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//mouth.h</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">ifndef</span> MOUTH_H</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">define</span> MOUTH_H</span>  <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">class</span> <span style="margin: 0px; padding: 0px; color: #a31515;">Mouth</span> {</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">public</span>:     Mouth();     ~Mouth();     <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> <span style="margin: 0px; padding: 0px; color: #a31515;">say</span><span style="margin: 0px; padding: 0px;">()</span></span>; };  <span style="margin: 0px; padding: 0px; color: #0000ff;">extern</span> Mouth mouth;   <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">endif</span></span> </code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//mouth.cpp</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;mouth.h&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;iostream&gt;</span></span>  <span style="margin: 0px; padding: 0px; color: #0000ff;">using</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">namespace</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">std</span>;  Mouth::Mouth() {}  Mouth::~Mouth() {}  <span style="margin: 0px; padding: 0px; color: #0000ff;">void</span> Mouth::say() {     <span style="margin: 0px; padding: 0px; color: #0000ff;">cout</span> &lt;&lt; <span style="margin: 0px; padding: 0px; color: #a31515;">"Have a Nice day!"</span> &lt;&lt; <span style="margin: 0px; padding: 0px; color: #0000ff;">endl</span>; } </code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//class.cpp</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;body.h&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;mouth.h&gt;</span></span>   Body body; Mouth mouth; </code></pre><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code cpp"="" style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;"><span style="margin: 0px; padding: 0px; color: green;">//main.cpp</span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;iostream&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #2b91af;">#<span style="margin: 0px; padding: 0px;">include</span> <span style="margin: 0px; padding: 0px;">&lt;body.h&gt;</span></span> <span style="margin: 0px; padding: 0px; color: #0000ff;">using</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">namespace</span> <span style="margin: 0px; padding: 0px; color: #0000ff;">std</span>;  <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #0000ff;">int</span> <span style="margin: 0px; padding: 0px; color: #a31515;">main</span><span style="margin: 0px; padding: 0px;">()</span> </span>{     body.greet(); } </code></pre><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">上面代码中的include，虽然都是用的尖括号，但因为编译时可以通过指定include路径，不会出问题～<br style="margin: 0px; padding: 0px;" />编译命令：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;"><code style="margin: auto; vertical-align: middle; display: block; height: auto; overflow-x: auto; padding: 5px !important; line-height: 1.5 !important; font-family: 'Courier New', sans-serif !important; font-size: 12px !important; border: 1px solid #cccccc !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important;">g++ -I ./ mouth.cpp body.cpp class.cpp main.cpp -o main</code></pre><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 微软雅黑; font-size: 13.920000076293945px; line-height: 20px; background-color: #ffffff;">能够正常运行。</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-26 16:02 <a href="http://www.cppblog.com/prayer/archive/2019/02/26/216260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>g++: 错误： –fpermissive：没有那个文件或目录</title><link>http://www.cppblog.com/prayer/archive/2019/02/25/216258.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 25 Feb 2019 02:56:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/25/216258.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216258.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/25/216258.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216258.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216258.html</trackback:ping><description><![CDATA[<div>https://www.xuebuyuan.com/1611225.html<br /><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">错误：</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">host C++: libutils &lt;= frameworks/base/libs/utils/AssetDir.cpp</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">host C++: libutils &lt;= frameworks/base/libs/utils/Asset.cpp<br />g++: 错误： &#8211;fpermissive：没有那个文件或目录<br />make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/Asset.o] 错误 1<br />make: *** 正在等待未完成的任务....<br />g++: 错误： &#8211;fpermissive：没有那个文件或目录<br />make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/AssetDir.o] 错误 1</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">true</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"></p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">是传递给g++的标志被误判成文件名了。用"make showcommands"编译，贴出完整的输出。</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">host C++: libutils &lt;= frameworks/base/libs/utils/AssetDir.cpp<br />g++&nbsp;&nbsp;-I external/zlib&nbsp;&nbsp; -I frameworks/base/libs/utils&nbsp;&nbsp; -I out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates&nbsp;&nbsp; -I dalvik/libnativehelper/include/nativehelper&nbsp;&nbsp; -I system/core/include&nbsp;&nbsp; -I hardware/libhardware/include&nbsp;&nbsp; -I hardware/libhardware_legacy/include&nbsp;&nbsp;<br />-I hardware/ril/include&nbsp;&nbsp; -I dalvik/libnativehelper/include&nbsp;&nbsp; -I frameworks/base/include&nbsp;&nbsp; -I frameworks/base/opengl/include&nbsp;&nbsp; -I frameworks/base/native/include&nbsp;&nbsp; -I external/skia/include&nbsp;&nbsp; -I tools/include&nbsp;&nbsp; -I out/host/linux-x86/obj/include&nbsp;&nbsp;-c&nbsp;&nbsp;-fno-exceptions<br />-Wno-multichar -m32 -fPIC -include system/core/include/arch/linux-x86/AndroidConfig.h -D_FORTIFY_SOURCE=0 -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -O2 -g -fno-strict-aliasing -DNDEBUG -UDEBUG -DANDROID -fmessage-length=0<br />-W -Wall -Wno-unused -Winit-self -Wpointer-arith -Wsign-promo -DNDEBUG -UDEBUG&nbsp;&nbsp; -DLIBUTILS_NATIVE=1&nbsp;&nbsp;&#8211;fpermissive&nbsp;&nbsp;&nbsp;&nbsp; -MD -o out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/AssetDir.o frameworks/base/libs/utils/AssetDir.cpp<br />g++: error: &#8211;fpermissive: No such file or directory<br />make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/AssetDir.o] Error 1</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"></p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px; color: #ff0000;">问题解决方法是：</p><p style="margin: 0px 0px 10px; padding: 0px; border: 0px; font-size: 13px; background-color: #ffffff; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"><span style="margin: 0px; padding: 0px; border: 0px; background-color: transparent; color: #ff0000;">因为&#8211;fpermissive 前边的－是带输入法的，改为不带输入法的 短线就可以了</span></p></div><img src ="http://www.cppblog.com/prayer/aggbug/216258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-25 10:56 <a href="http://www.cppblog.com/prayer/archive/2019/02/25/216258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>error: ‘memset’ was not declared in this scope</title><link>http://www.cppblog.com/prayer/archive/2019/02/25/216257.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 25 Feb 2019 02:53:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/25/216257.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216257.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/25/216257.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216257.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216257.html</trackback:ping><description><![CDATA[<div>http://blog.sina.com.cn/s/blog_79d599dc0100r2vz.html<br /><br /><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">昨天一同事把代码准备重新全新布置到新的环境上去的时候，代码报错了，先开始报错如下：</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">&nbsp;<wbr>error: &#8216;memset&#8217; was not declared in this scope</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">&nbsp;<wbr>error: &#8216;strcat&#8217; was not declared in this scope</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">首先想到的肯定是头文件未包含进来。但不可能，因为这套代码已经在其它环境上编译过N次啦，不可能代码里面没有添加对头文件的包含啊。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">于是想到是否是因为新环境里面include目录下没有对应的头文件呢？还或是include目录在这个环境下变了呢？不是/usr/include下面啦。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">经过一查，上面两个都不是。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">这个时候就有点郁闷啦。把环境按以前的都设置一遍，还是一样。没办法啦。只好放今天来重新查查啦。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">今天早上来，可能时间也比较充足，所以慢慢来。既然报这个错误，那我把这个头文件再添加到.h代码里面呢？</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">然后发现添加一个，某个错误就能编译过去。看来还真是头文件的问题。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;"></div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">但接下来还有其他几个警告，如下：</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:213: warning: ignoring return value of &#8216;int system(const char*)&#8217;, declared with attribute warn_unused_result</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp: In member function &#8216;int test::load(std::string&amp;)&#8217;:</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:58: warning: deprecated conversion from string constant to &#8216;char*&#8217;</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:58: warning: deprecated conversion from string constant to &#8216;char*&#8217;</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:58: warning: deprecated conversion from string constant to &#8216;char*&#8217;</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:58: warning: deprecated conversion from string constant to &#8216;char*&#8217;</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:58: warning: deprecated conversion from string constant to &#8216;char*&#8217;</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:94: warning: suggest explicit braces to avoid ambiguous &#8216;else&#8217;</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">test.cpp:61: warning: ignoring return value of &#8216;int pipe(int*)&#8217;, declared with attribute warn_unused_result</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;"></div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">居然这些还报，一一为这些没有判断函数返回值的作了修改。编译通过。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;"></div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">然后就想为什么这个系统要求这么严格呢？查一下GCC的版本，如下：</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">以前旧环境：</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">gcc 版本 4.1.2 20070626 (Red Hat 4.1.2-14)</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">现在的新环境：</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)&nbsp;<wbr></div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;"></div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">原来是GCC版本不同。所以想GCC新版本对代码的这些要求现在越来越严格啦。还有函数的一些检查项，还有编译时候MAKEFILE里面对库文件的引用。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">具体是否是因为这个严格的原因，没有去详细查GCC的文档。但猜想是这样吧。</div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;"></div><div style="color: #494949; font-family: simsun; background-color: #c9f3e9;">以后如果代码里面对某个函数的头文件还是尽量包含进来吧，至少不是什么坏事。</div></div><img src ="http://www.cppblog.com/prayer/aggbug/216257.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-25 10:53 <a href="http://www.cppblog.com/prayer/archive/2019/02/25/216257.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>LINUX下默认搜索头文件及库文件的路径</title><link>http://www.cppblog.com/prayer/archive/2019/02/21/216251.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 21 Feb 2019 07:38:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/21/216251.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216251.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/21/216251.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216251.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216251.html</trackback:ping><description><![CDATA[<div>https://www.jianshu.com/p/3eb25114576e 编写c/c++的代码的经常，经常#include 头文件。include有两种方式，一个是#include &#8220;头文件&#8221;，一个是#include &lt;头文件&gt;。区别在于：<div><div> <ol> <li>前者是从当前的目录来搜索</li> <li>后者是一般会先搜索 -I 选项后的路径（即用gcc编译时的-I选项），之后就是标准的系统头文件路径。</li> </ol> <p>通过cpp -v命令，可以查看标准系统头文件的路径。</p> <pre ruby"=""><code>zhaozheng@ubuntu:~$ cpp -v Using built-in specs. COLLECT_GCC=cpp Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)  COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=x86-64'  /usr/lib/gcc/x86_64-linux-gnu/5/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu - -mtune=generic -march=x86-64 -fstack-protector-strong -Wformat -Wformat-security ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include" #include "..." search starts here: #include &lt;...&gt; search starts here:  /usr/lib/gcc/x86_64-linux-gnu/5/include  /usr/local/include  /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed  /usr/include/x86_64-linux-gnu  /usr/include End of search list. </code></pre> <p>所以基本上是三个目录：<br /> usr/include<br /> usr/lib/include<br /> usr/local/include</p> <blockquote> <p>注意：usr并不是user用户的缩写，而是unix system resource 的缩写。</p> </blockquote> <p>如果我们在给要编译的代码加上自己的头文件，应该怎么办？</p> <ol> <li><p>在path里面加上自己的目录</p></li> <li><p>gcc或g++ 编译的时候 ，加上 -I  选项, 比如这样</p></li> </ol> <pre undefined"=""><code>g++ -c ttcp_blocking.cc -I /home/zhaozheng/code/chensuo/code/muduo-master/ -o ttcp_blocking.o </code></pre> <p>这里把这个实际的例子写在这里</p></div><br /><br />作者：zhaozhengcoder<br />链接：https://www.jianshu.com/p/3eb25114576e<br />來源：简书<br />简书著作权归作者所有，任何形式的转载都请联系作者获得授权并注明出处。</div><br /><br /><br /></div><img src ="http://www.cppblog.com/prayer/aggbug/216251.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-21 15:38 <a href="http://www.cppblog.com/prayer/archive/2019/02/21/216251.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【错误总结】expected specifier-qualifier-list before sth</title><link>http://www.cppblog.com/prayer/archive/2019/02/21/216250.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 21 Feb 2019 06:51:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/21/216250.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216250.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/21/216250.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216250.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216250.html</trackback:ping><description><![CDATA[<div><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: KaiTi_GB2312; word-wrap: break-word; font-size: 18px;">转载于：http://www.cnblogs.com/lu-yang/archive/2011/11/24/2261282.html</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: KaiTi_GB2312; word-wrap: break-word; font-size: 18px;">在oc中经常会遇到expected specifier-qualifier-list before sth之类得编译错误，造成这种错误得主要原因就是使用了未被定义的变量。关于specifier-qualifier-list的定义：It's a list of specifiers and qualifiers :-) Specifiers are things like void, char, struct Foo, etc., and qualifiers are keywords like const and volatile. See this C grammar for the definition.如：</span></p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 22px; background-color: #ffffff;"><code hljs=""  objectivec"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; display: block; overflow-x: auto; color: #383a42; background-color: #fafafa; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; white-space: pre; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">typedef</span> <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">struct</span> { <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">char</span> *key; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">long</span> canTag; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">long</span> canSet; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">long</span> allowMultiple; confType *next; } confType;</code></pre><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">由于confType未被完全定义即在定义中使用，这样就会报错，常规得解决办法有：</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: KaiTi_GB2312; word-wrap: break-word; font-size: 18px;">1.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"></p><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 22px; background-color: #ffffff;"><code hljs=""  objectivec"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; display: block; overflow-x: auto; color: #383a42; background-color: #fafafa; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; white-space: pre; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: KaiTi_GB2312; word-wrap: break-word; font-size: 18px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">typedef</span> <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">struct</span> confType { <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">char</span> *key; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">long</span> canTag; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">long</span> canSet; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">long</span> allowMultiple; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; color: #a626a4; word-wrap: break-word; font-size: 14px;">struct</span> confType*next; } confType;</span></code></pre><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: KaiTi_GB2312; word-wrap: break-word; color: #333333; background-color: #ffffff; font-size: 18px;">2.<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; word-wrap: break-word; overflow-x: auto; line-height: 22px; background-color: #ffffff;"><code hljs=""  objectivec"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; display: block; overflow-x: auto; color: #383a42; background-color: #fafafa; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; white-space: pre; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">typedef</span> structconfType confType;  <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">struct</span> confType { <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">char</span> *key; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">long</span> canTag; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">long</span> canSet; <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #a626a4; word-wrap: break-word;">long</span> allowMultiple; confType *next; };</code></pre><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">上述例子可参考</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">http://stackoverflow.com/questions/2894639/what-is-a-specifier-qualifier-list</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">http://stackoverflow.com/questions/3888569/expected-specifier-qualifier-list-before</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">在实际实验过程中会发现还会有一种方法可以解决此问题：</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;" /><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">假如有一个ParserDemo工程，而其中有ParserDemoAppDelegate.h/ParserDemoAppDelegate.m和ParserDemoViewController.h/ParserDemoViewController.m. 有时候为了使用C++代码，经常会将后者改为.mm但忘记将前者修改，这样也会报类似的错误，解决办法很简单，就是将二者都改成.mm即可。</span></div><img src ="http://www.cppblog.com/prayer/aggbug/216250.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-21 14:51 <a href="http://www.cppblog.com/prayer/archive/2019/02/21/216250.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>secureCrt中文乱码问题</title><link>http://www.cppblog.com/prayer/archive/2019/02/21/216249.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 21 Feb 2019 06:13:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2019/02/21/216249.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216249.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2019/02/21/216249.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216249.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216249.html</trackback:ping><description><![CDATA[<p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;"><span style="font-family: 宋体; font-size: 10.5pt;">保证secureCrt的显示方式与你的系统环境相同。</span></p><p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;"><span style="font-family: 宋体; font-size: 10.5pt;">在linux的.profile中设置为了export LANG=zh_CN.gbk</span></p><p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;">&nbsp;</p><p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;"><span style="font-family: 宋体; font-size: 10.5pt;">在secureCrt，连接该环境的会话中，选择&#8220;选项&#8221;--&#8220;会话选项&#8221;-&#8220;终端&#8221; -&#8220;外观&#8221;</span></p><p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;"><span style="font-family: 宋体; font-size: 10.5pt;">在字符编码中选择&#8220;简体中文GB2313&#8221;</span></p><p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;">&nbsp;</p><p align="justify" style="margin-right:0.0000pt;margin-left:0.0000pt;text-indent:0.0000pt;text-autospace:ideograph-numeric;text-align:justify;text-justify:inter-ideograph;"><span style="font-family: 宋体; font-size: 10.5pt;">原来的选项是&#8220;UTF-8&#8221;</span></p><img src ="http://www.cppblog.com/prayer/aggbug/216249.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2019-02-21 14:13 <a href="http://www.cppblog.com/prayer/archive/2019/02/21/216249.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>打印出double类型在内存中的每个位</title><link>http://www.cppblog.com/prayer/archive/2018/11/29/216101.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 29 Nov 2018 05:50:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/29/216101.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216101.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/29/216101.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216101.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216101.html</trackback:ping><description><![CDATA[<p>printf没有打印二进制。</p><p>自己写了一个，先转成16进制，再转成二进制。</p><p>也可以先转成8进制，再转成二进制。</p><p>我测试环境double是8个字节，64位长度。</p><p>*/</p><p>/*打印出double在内存的中的每一个二进制位<br />*/<br />void DoubleToString(double *p_dl){<br />&nbsp;&nbsp; &nbsp; &nbsp; char c_dl_16[16+1];<br />&nbsp;&nbsp; &nbsp; &nbsp; char c_dl_64[64+1];<br />&nbsp;&nbsp; &nbsp; &nbsp; memset(c_dl_64,'\0',sizeof(c_dl_64));<br />&nbsp;&nbsp; &nbsp; &nbsp; unsigned char *t = (unsigned char *)p_dl;<br />&nbsp;&nbsp; &nbsp; &nbsp; int charCount = sizeof(double) ;<br />&nbsp;&nbsp; &nbsp; &nbsp; memset(c_dl_16,'\0',sizeof(c_dl_16));<br />&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<br />&nbsp;&nbsp; &nbsp; &nbsp; if (t == NULL)<br />&nbsp; &nbsp; &nbsp; &nbsp;return;<br />&nbsp; &nbsp; &nbsp;int i=0;<br />&nbsp; &nbsp; &nbsp;for(i=0;i&lt;charCount;i++){<br />&nbsp; &nbsp; &nbsp; &nbsp; sprintf(c_dl_16+i*2,"%02x",t[i]);<br />&nbsp; &nbsp; &nbsp;}<br />&nbsp; &nbsp; &nbsp;/*printf("\n"); &nbsp;*/<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("[%s]\n",c_dl_16); &nbsp;<br />&nbsp; &nbsp; &nbsp;/*对每一位，将十六进制转换为二进制的字符串*/<br />&nbsp; &nbsp; &nbsp;char tmpc[4+1];<br />&nbsp; &nbsp; &nbsp;for(int j=0;j&lt;16;j++){<br />&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; memset(tmpc,'\0',sizeof(tmpc));<br />&nbsp; &nbsp; &nbsp; &nbsp;switch(c_dl_16[j]){<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '0':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;memcpy(tmpc,"0000",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '1':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0001",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '2':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0010",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '3':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0011",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '4':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0100",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '5':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0101",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '6':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0110",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '7':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"0111",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '8':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1000",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case '9':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1001",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 'a':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1010",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 'b':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1011",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 'c':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1100",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 'd':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1101",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 'e':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1110",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 'f':<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;memcpy(tmpc,"1111",4);<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp;}<br />&nbsp; &nbsp; &nbsp; &nbsp;sprintf(c_dl_64+j*4,"%s",tmpc);<br />&nbsp; &nbsp; &nbsp;}<br />&nbsp; &nbsp; &nbsp;printf("[%s]\n",c_dl_64);<br />&nbsp; &nbsp; &nbsp;<br />}<br /><br /></p><p><br /></p><p>使用</p><p>&nbsp; &nbsp; double tmpd=0.01;<br />&nbsp; &nbsp; DoubleToString(&amp;tmpd);<br />&nbsp; &nbsp; printf("%x\n",0.01);</p><img src ="http://www.cppblog.com/prayer/aggbug/216101.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-29 13:50 <a href="http://www.cppblog.com/prayer/archive/2018/11/29/216101.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>机器中float的二进制舍入问题  --很好</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216099.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 09:34:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216099.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216099.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216099.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216099.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216099.html</trackback:ping><description><![CDATA[<div style="box-sizing: inherit; outline: 0px; margin: 0px 0px 8px; word-wrap: break-word; font-size: 12px; color: #999999; font-family: Lato, -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; background-color: #ffffff;">版权声明：转载请注明出处，谢谢	 https://blog.csdn.net/laeen/article/details/51194684</div><div id="content_views" data-find="_1" style="box-sizing: inherit; outline: 0px; margin: 0px; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; word-wrap: break-word; color: rgba(0, 0, 0, 0.74902); line-height: 23px; background-color: #ffffff; word-break: break-word !important;"><pre style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px 16px 6px 56px; position: relative; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; overflow-y: hidden; overflow-x: auto; line-height: 22px; color: #000000; word-wrap: break-word; background-color: #f6f8fa; border: none;"><code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0.5em; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; background-color: rgba(0, 0, 0, 0.0470588); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; display: block; overflow-x: auto; word-wrap: break-word;">                    **机器中float的二进制舍入问题** </code><ul style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px 0px; list-style: none; font-size: inherit; word-wrap: break-word; position: absolute; width: 48px; background-color: #eef0f4; top: 0px; left: 0px;"><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">1</li></ul></pre><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">在计算机中，对于精度取舍的问题一直都比较模糊，到底什么时候要舍去，什么时候要输出，下面我就以计算机中的float型为例子，来简单地说一说float的四舍五&#8220;入&#8221;。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">float型在计算机用32位来表示（4个字节），和计算机表示int长度相同，但是表示的机制完全不一样，它由sign，exp，frac部分组成，也就是我们所说的符号位，阶码位，和小数位。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">具体的表示方法在这里就不细说了（主要谈论舍入问题）。我们知道的四舍五入就是小于5舍去，大于等于5就进位，那么为什么要这样。举个例子：&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />1 2 3 4&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />9 8 7 6 5&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />比如11，如果在个位运用了四舍五入，那么就会产生一个1的误差（丢失1）得到10.还有19，如果个位数是9的话，相应的产生了1的误差（增加1），四舍五入得到20.所以9个数字中1234恰好和9876对应（后面会说到5），我们期望的是在大量的数据下产生的误差中得到和失去的概率近似为1/2，这就有了四舍五入。但是，我们知道5并没有什么数字和它对应，所以增加的期望值要大于减小的期望值，这是我们不希望看到的。&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />但是在计算机中所用的表示方法都是二进制的，1,0出现的概率我们可以认为相等。所以，我们就得到了一个新型的四舍五入的方法，小于所对应精度1/2就舍去，大于就进位，等于就有两种情况，一种情况是最低有效位是1，进位。为0，舍去。（向偶取整）&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />用一道例题来说明：</p><pre name="code" style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 24px; padding: 8px 16px 6px 56px; position: relative; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; overflow-y: hidden; overflow-x: auto; line-height: 22px; color: #000000; word-wrap: break-word; background-color: #f6f8fa; border: none;"><code cpp=""  has-numbering"="" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px; font-family: 'Source Code Pro', 'DejaVu Sans Mono', 'Ubuntu Mono', 'Anonymous Pro', 'Droid Sans Mono', Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, 'PingFang SC', 'Microsoft YaHei', sans-serif; display: block; overflow-x: auto; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word; color: #4f4f4f !important;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-size: inherit; word-wrap: break-word; color: #009900 !important;">#include&lt;stdio.h&gt;</span> main() {     <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #000088 !important; font-size: inherit; word-wrap: break-word;">float</span> a;                 <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #000088 !important; font-size: inherit; word-wrap: break-word;">double</span> b;                 a = <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #006666 !important; font-size: inherit; word-wrap: break-word;">123456.789e4</span>;                 b = <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #006666 !important; font-size: inherit; word-wrap: break-word;">123456.789e4</span>;                 <span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-size: inherit; word-wrap: break-word;">printf</span>(&#8220;%f/n%f/n&#8221;,a,b); }   </code><ul style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 8px 0px; list-style: none; font-size: inherit; word-wrap: break-word; position: absolute; width: 48px; background-color: #eef0f4; top: 0px; left: 0px;"><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">1</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">2</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">3</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">4</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">5</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">6</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">7</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">8</li><li style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px 8px; font-size: inherit; list-style: none; word-wrap: break-word; color: #999999;">9</li></ul></pre><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">运行结果如下：&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />1234567936.000000&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />1234567890.000000</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;">由于double的尾数位没有进行舍入操作，因此并没有改变原数，但1234567890&gt;2^23-1要进行舍入操作。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; word-wrap: break-word;"><img src="https://img-blog.csdn.net/20160419222515662" alt="这里写图片描述" title="" style="box-sizing: border-box; outline: 0px; margin: 24px 0px; padding: 0px; border-style: none; max-width: 100%; word-wrap: break-word; cursor: -webkit-zoom-in;" />&nbsp;<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />所以就增加了46。</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216099.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 17:34 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216099.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在linux中install命令和cp命令的区别</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216098.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 09:03:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216098.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216098.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216098.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216098.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216098.html</trackback:ping><description><![CDATA[<div>https://blog.csdn.net/bugouyonggan/article/details/11962201<br /><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">转：http</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">://blog.yikuyiku.com/?p=2659</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">基本上，在Makefile里会用到install，其他地方会用cp命令。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">它们完成同样的任务&#8212;&#8212;拷贝文件，它们之间的区别主要如下：</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">1、最重要的一点，如果目标文件存在，cp会先清空文件后往里写入新文件，而install则会先删除掉原先的文件然后写入新文件。这是因为往正在 使用的文件中写入内容可能会导致一些问题，比如说写入正在执行的文件可能会失败，比如说往已经在持续写入的文件句柄中写入新文件会产生错误的文件。而使用 install先删除后写入（会生成新的文件句柄）的方式去安装就能避免这些问题了；</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">2、install命令会恰当地处理文件权限的问题。比如说，install -c会把目标文件的权限设置为rwxr-xr-x；</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">3、install命令可以打印出更多更合适的debug信息，还会自动处理SElinux上下文的问题。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">转：http://blog.csdn.net/stevenliyong/article/details/4663583</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">install&nbsp; - copy files and set attributes</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">install 在做拷贝的同时，设置attributes.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">因此Makefile 中尽量使用install 命令。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">例如</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">@install&nbsp;-d /usr/bin</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">@install -p -D -m 0755 targets /usr/bin</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">相当于</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">@mkdir -p /usr/bin</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">@cp targets /usr/bin</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">@chmod 755 /usr/bin/targets</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">@touch /usr/bin/tagets&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;---- 更新文件时间戳</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">install 命令好强大啊。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">另外@前缀的意思是不在控制台输出结果。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; background-color: #ffffff;">转载:http://www.cnblogs.com/wwwsinagogogo/archive/2011/08/15/2139124.html</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; color: #4f4f4f; overflow-x: auto; word-wrap: break-word; font-family: 'black verdana';">&nbsp;</p><div id="cnblogs_post_body" style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; font-family: 'black verdana';"><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">【概述】</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">Install和cp类似，都可以将文件/目录拷贝到指定的地点。但是，install允许你控制目标文件的属性。install通常用于程序的makefile，使用它来将程序拷贝到目标（安装）目录。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">【语法】</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install [OPTION]... [-T] SOURCE DEST</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install [OPTION]... SOURCE... DIRECTORY</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install [OPTION]... -t DIRECTORY SOURCE...</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install [OPTION]... -d DIRECTORY...</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">*如果指定了两个文件名, `install' 将第一个文件拷贝到第二个</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">* 如果使用了 `--target-directory' (`-t') 选项，或者如果最后一个文件是一个目录并且没有使用`--no-target-directory' (`-T')选项， `install'将每一个源文件拷贝到指定的目录，目标文件名与SOURCE文件名相同。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">* 如果使用了 `--directory' (`-d') 选项, `install' 将逐级创建缺失的目标目录</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">【常用选项】</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-s:对待拷贝的可执行文件进行strip操作，取出文件中的符号表。（一般在做成nand rom时去除符号表，NFS时为了调试方便，一般不会使用此选项）</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-d(--directory)：创建制定的目录结构（逐级创建）。如，指定安装位置为/usr/local/aaa/bbb，/usr/loacal已存在，install会帮助我们创建aaa和bbb目录，并把程序安装到指定位置</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">If we hand write a&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">Makefile</code>, we should always stick to&nbsp;<a href="http://linux.die.net/man/1/install" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">install</a>&nbsp;instead of using&nbsp;<a href="http://linux.die.net/man/1/cp" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">cp</a>&nbsp;for the installation commands. Not only is it more convenient, but it does things right (cp does things&nbsp;<em style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">wrong</em>).</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">For example, if we attempt to update&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">/bin/bash</code>, which is currently running, with &#8220;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">cp ... /bin/bash</code>&#8221;, we get a &#8220;text busy&#8221; error. If we attempt to update&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">/lib/libc.so.6</code>&nbsp;with &#8220;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">cp ... /lib/libc.so.6</code>&#8221;, then we either get &#8220;text busy&#8221; (in ancient versions of Linux) or breaks each and every running program within a fraction of a second (in recent versions of Linux).&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">install</code>&nbsp;does the thing right in both situations.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">The reason why&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">cp</code>&nbsp;fails is that it simply attempts to open the destination file in write-only mode and write the new contents. This causes problem because Linux (and all contemporary&nbsp;<a href="http://en.wikipedia.org/wiki/Unix-like" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">Unices</a>&nbsp;as well as Microsoft Windows) uses&nbsp;<a href="http://en.wikipedia.org/wiki/Memory-mapped_file" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">memory mapping</a>&nbsp;(mmap) to load executables and dynamic libraries.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">The contents of an executable or dynamic library are mmap&#8217;d into the linear address space of relevant processes. Therefore, any change in the underlying file affects the mmap&#8217;d memory regions and can potentially break programs. (<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">MAP_PRIVATE</code>&nbsp;guarantees changes by processes to those memory regions are handled by&nbsp;<a href="http://en.wikipedia.org/wiki/Copy-on-write" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">COW</a>without affecting the underlying file. On the contrary,&nbsp;<a href="http://en.wikipedia.org/wiki/POSIX" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">POSIX</a>&nbsp;leaves to implementations whether COW should be used if the underlying file is modified. In fact, for purpose of efficiency, in Linux, such modifications are&nbsp;<em style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">visible</em>&nbsp;to processes even though&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">MAP_PRIVATE</code>&nbsp;may have be used.)</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">There is an option&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">MAP_DENWRITE</code>&nbsp;which disallows any modification to the underlying file, designed to avoid situations described above. Executables and dynamic libraries are all mmap&#8217;d with this option. Unfortunately, it turned out&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">MAP_DENYWRITE</code>&nbsp;became a source of&nbsp;<a href="http://en.wikipedia.org/wiki/Denial-of-service_attack" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">DoS attacks</a>, forcing Linux to ignore this option in recent versions.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">Executables are mmap&#8217;d by the kernel (in the&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">execve</code>&nbsp;<a href="http://en.wikipedia.org/wiki/System_call" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">syscall</a>). For kernel codes,&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">MAP_DENYWRITE</code>&nbsp;still works, and therefore we get &#8220;text busy&#8221; errors if we attempt to modify the executable.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">On the other hand, dynamic libraries are mmap&#8217;d by userspace codes (for example, by loaders like&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">/lib/ld-linux.so</code>). These codes still pass&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">MAP_DENYWRITE</code>&nbsp;to the kernel, but newer kernels silently ignores this option. The bad consequence is that you can break the whole system if you think you&#8217;re only upgrading the&nbsp;<a href="http://en.wikipedia.org/wiki/C_standard_library" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">C runtime library</a>.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">Then, how does&nbsp;<code style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-wrap: break-word;">install</code>&nbsp;solve this problem? Very simple &#8211;&nbsp;<a href="http://en.wikipedia.org/wiki/Unlink_(Unix)" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">unlinking</a>&nbsp;the file before writing the new one. Then the old file (no longer present in directory entries but still in disk until the last program referring to it exits) and the new file have different&nbsp;<a href="http://en.wikipedia.org/wiki/Inode" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">inodes</a>. Programs started before the upgrading (continuing using the old file) and those after the upgrading (using the new version) will both be happy.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 13px; color: #4f4f4f; line-height: 18px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff; font-family: Verdana, Tahoma, 'Microsoft YaHei', sans-serif;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-family: Arial, Helvetica, simsun, u5b8bu4f53; font-size: 14px; line-height: 23px;">记得在大学的时候在编译LFS 6 的时候, 一直搞不懂 install 的命令 和 cp 以及和 chmod, chgrp 的区别?　</span></p><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">工作之后才明白一个Running 的进程不能随便进行 cp , 经常会提示 &nbsp;"text busy＂, 运维部的前辈们给的建议是采用mv 来替代 cp , 今天看起来前辈好像不知道install 这个命令啊.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">今天就简单介绍一下 install 命令.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />install copy 文件列表且同时能够设置文件的属性(包括 owner, group) , 通常用在 Makefiles 中 用来copy 程序到指定的目录. &nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">常见的用法有以下3中形式:　</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">1: &nbsp;install &nbsp;-d [option] &nbsp; DIRECTORY [DIRECTORY...] &nbsp;支持多个. 类似 mkdir -p &nbsp;支持递归.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">例如: &nbsp;install -d a/b/c e/f &nbsp;结果和 mkdir -p &nbsp;a/b/c &nbsp;e/f 一样. &nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">2: install [option] &nbsp;SOURCE DEST&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">复制 SOURCE &nbsp;文件(测试不能是目录) &nbsp; 到DEST file(文件) .&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install &nbsp;a/e &nbsp; c &nbsp; 结果类似 &nbsp;cp &nbsp;a/e &nbsp;c &nbsp; # 注意c必须是文件.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">有用选项 -D</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install -D x a/b/c &nbsp; &nbsp;# 效果类似 mkdir -p a/b &nbsp;&amp;&amp; cp &nbsp;x a/b/c &nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">3: install [option] &nbsp;SOURCE [SOURCE...] DIRECTORY</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">复制 &nbsp;多个SOURCE 文件到目的目录. &nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install a/* &nbsp;d &nbsp; 其中 d 是目录.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">有用选项</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">&nbsp;-b ：自动备份.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-m : 设置安装文件的权限</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-p :保留文件的timestamps. 也就是说文件的timestaamps 和 source 文件一样. &nbsp;当我们想要利用安装文件的mtime来跟踪文件的build时间而不是 安装时间.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-s :&nbsp;Strip the symbol tables from installed binary executables.</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-S : 备份文件的后缀.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">install &nbsp;-S .bak new &nbsp;old &nbsp;#old 文件自动被 mv 为 old.bak.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-v: verbose ,打印install 的文件的详细信息.&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">`-c'</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">&nbsp; &nbsp; &nbsp;Ignored; for compatibility with old Unix versions of `install'. &nbsp;#用来兼容旧版的unix.&nbsp;</span></div></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">-C: (大写)&nbsp;</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px; word-wrap: break-word; color: #333333; background-color: #ffffff; line-height: 23px; font-family: Arial, Helvetica, simsun, u5b8bu4f53;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">安装文件, 但是如果目标文件和源文件一样( 判断方法需要看看代码确认) 就跳过, 这样的好处是 能够保持一样文件的mtime. &nbsp;</span></div><div><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><br /></span></div></div></div><img src ="http://www.cppblog.com/prayer/aggbug/216098.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 17:03 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216098.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux下So覆盖导致coredump问题的分析</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216097.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 09:01:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216097.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216097.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216097.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216097.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216097.html</trackback:ping><description><![CDATA[<div>https://blog.csdn.net/chuanhehuan1/article/details/40651891<br /><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">转自：http://blog.sina.com.cn/s/blog_622a99700100pjv3.html 感谢这位大神，我刚好遇到这个问题</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">尝试解答以下问题：</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.为什么cp的方式更新运行中进程的so，程序会coredump</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.采用什么方式更新已经加载了的so,就可以避免coredump</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">我们的公共组件绝大部分都支持so形式的自定义插件，比如s++,qzhttp,ttc。在不停进程更新so的时候往往会产生coredump，并且肯定core得莫名其妙，core得让人心碎。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">先看一下用cp的方式更新so的时候发生了什么事情</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">strace cp new.so old.so</span>&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #008080;">#strace</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #008080;">是人间利器</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=622a99700100pjv3&amp;url=http://s10.sinaimg.cn/orignal/622a9970g7637a02e3389" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;"><img src="http://s10.sinaimg.cn/middle/622a9970g7637a02e3389&amp;690" alt="linux下So覆盖导致coredump问题的分析" title="linux下So覆盖导致coredump问题的分析" height="90" width="536" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 0px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">发现老的so被trunc了，这个过程发生的具体的事情是：</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.应用程序通过dlopen打开so的时候，kernel通过mmap把so加载到进程地址空间，对应于vma里的几个page.</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.在这个过程中loader会把so里面引用的外部符号例如malloc printf等解析成真正的虚存地址。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.当so被cp覆盖时，确切地说是被trunc时，kernel会把so文件在虚拟内的页purge 掉。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.当运行到so里面的代码时，因为物理内存中不再有实际的数据（仅存在于虚存空间内），会产生一次缺页中断。</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.Kernel从so文件中copy一份到内存中去,a)但是这时的全局符号表并没有经过解析，当调用到时就产生segment fault ,&nbsp;b)如果需要的文件偏移大于新的so的地址范围，就会产生buserror.</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">所以，如果用相同的so去覆盖</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A) 如果so里面依赖了外部符号，coredump</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B) 如果so里面没有依赖外部符号，运气不错，不会coredump</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">所有问题的产生都是因为so被trunc了一把，所以如果不用turnc的方式就避免这个问题。Ok,该我们的install 上场了。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">strace install new.so old.so</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=622a99700100pjv3&amp;url=http://s10.sinaimg.cn/orignal/622a9970g9e2c446138d9" rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;"><img src="http://s10.sinaimg.cn/middle/622a9970g9e2c446138d9&amp;690" name="image_operate_46561299813150712" alt="linux下So覆盖导致coredump问题的分析" title="linux下So覆盖导致coredump问题的分析" height="111" width="547" style="box-sizing: border-box; outline: #0782c1 solid 1px; margin: 0px; padding: 1px; max-width: 100%; border: none; word-wrap: break-word; cursor: -webkit-zoom-in;" /></a><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">install 的方式跟cp不同，先unlink再creat，当unlink的时候，已经map的虚拟空间vma中的inode结点没有变，只有inode结点的引用计数为0是，kernel才把它干掉。</p><span style="color: #333333; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">也就是新的so和旧的so用的不是同一个inode结点，所以不会相互影响。这时只有得启程序才会使用到新的so。所以采用这种方式的话就可以避免先stop进程，更新so，再重启进程这样比较耗时的操作。</span></div><img src ="http://www.cppblog.com/prayer/aggbug/216097.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 17:01 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216097.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>程序的静态链接，动态链接和装载</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216095.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 07:53:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216095.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216095.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216095.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216095.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216095.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">参考自：<a href="http://www.xuebuyuan.com/1730287.html" target="_blank" style="margin: 0px; padding: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333;">http://www.xuebuyuan.com/1730287.html</a></strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;"><strong style="margin: 0px; padding: 0px;">一、程序编译链接的整体流程</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;"><strong style="margin: 0px; padding: 0px;">二、目标文件的样子（以linux下的elf文件格式为例）</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;"><strong style="margin: 0px; padding: 0px;">三、静态链接</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;"><strong style="margin: 0px; padding: 0px;">四、装载</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;"><strong style="margin: 0px; padding: 0px;">五、动态链接</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">一、程序编译链接的整体流程</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">通常我们使用gcc来生成可执行程序，命令为：gcc hello.c，默认生成可执行文件a.out</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">其实编译（包括链接）的命令：gcc hello.c 可分解为如下4个大的步骤：</p><ul style="margin: 0px 0px 0px 30px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><ul style="margin: 0px 0px 0px 30px; padding: 0px;"><li style="margin: 0px; padding: 0px; list-style: circle;"><strong style="margin: 0px; padding: 0px;">预处理</strong>(Preprocessing)</li><li style="margin: 0px; padding: 0px; list-style: circle;"><strong style="margin: 0px; padding: 0px;">编译</strong>(Compilation)</li><li style="margin: 0px; padding: 0px; list-style: circle;"><strong style="margin: 0px; padding: 0px;">汇编</strong>(Assembly)</li><li style="margin: 0px; padding: 0px; list-style: circle;"><strong style="margin: 0px; padding: 0px;">链接</strong>(Linking)</li></ul></ul><div id="attachment_354"  alignnone"="" style="margin: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><a href="http://smilejay.com/2012/01/c_compilation_stages/gcc_compilation_stages/" rel="attachment wp-att-354" style="margin: 0px; padding: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333;"><img wp-image-354"="" title="gcc_compilation_stages" src="http://smilejay.b0.upaiyun.com/wp-content/uploads/2012/01/gcc_compilation_stages.jpg" alt="gcc compilation" width="554" height="267" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></a><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">gcc compilation</p></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><strong style="margin: 0px; padding: 0px;">预处理(Preproceessing)</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">预处理的过程主要处理包括以下过程：</strong></p><ul style="margin: 0px 0px 0px 30px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: disc;">将所有的#define删除，并且<strong style="margin: 0px; padding: 0px;">展开所有的宏定义</strong></li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">处理</strong>所有的<strong style="margin: 0px; padding: 0px;">条件预编译指令</strong>，比如#if #ifdef #elif #else #endif等</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">处理#include</strong>&nbsp;预编译指令，将被包含的文件插入到该预编译指令的位置。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">删除所有注释&nbsp;</strong>&#8220;//&#8221;和&#8221;/* */&#8221;.</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">添加行号和文件标识</strong>，以便编译时产生调试用的行号及编译错误警告行号。</li><li style="margin: 0px; padding: 0px; list-style: disc;">保留所有的<strong style="margin: 0px; padding: 0px;">#pragma</strong><strong style="margin: 0px; padding: 0px;">编译器指令</strong>，因为编译器需要使用它们</li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">通常使用以下命令来进行预处理：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">gcc -E hello.c -o hello.i</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">参数<strong style="margin: 0px; padding: 0px;">-E</strong>表示只进行预处理 或者也可以使用以下指令完成预处理过程</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">cpp hello.c &gt; hello.i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* &nbsp;cpp &#8211; The C Preprocessor&nbsp; */</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">直接cat hello.i 你就可以看到预处理后的代码</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><strong style="margin: 0px; padding: 0px;">编译(Compilation)</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">编译过程就是把预处理完的文件进行一系列的词法分析，语法分析，语义分析及优化后生成相应的汇编代码。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">$gcc &#8211;S hello.i &#8211;o hello.s</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">或者</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">$ /usr/lib/gcc/i486-linux-gnu/4.4/cc1 hello.c</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">注：现在版本的GCC把预处理和编译两个步骤合成一个步骤，用cc1工具来完成。gcc其实是后台程序的一些包装，根据不同参数去调用其他的实际处理程序，比如：预编译编译程序cc1、汇编器as、连接器ld</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><strong style="margin: 0px; padding: 0px;">汇编(Assembly)</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">汇编器是将汇编代码转变成机器可以执行的命令，每一个汇编语句几乎都对应一条机器指令。汇编相对于编译过程比较简单，根据汇编指令和机器指令的对照表一一翻译即可。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">$ gcc &#8211;c hello.c &#8211;o hello.o</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">或者</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">$ as hello.s &#8211;o hello.co</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">由于hello.o的内容为机器码，不能以普通文本形式的查看（vi 打开看到的是乱码）。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><strong style="margin: 0px; padding: 0px;">链接(Linking)</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">通过调用链接器ld来链接程序运行需要的一大堆目标文件，以及所依赖的其它库文件，最后生成可执行文件。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">ld -static crt1.o crti.o crtbeginT.o hello.o -start-group -lgcc -lgcc_eh -lc-end-group crtend.o crtn.o (省略了文件的路径名)。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">helloworld的大体编译和链接过程就是这样了，那么编译器和链接器到底做了什么呢？</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">编译过程可分为6</strong><strong style="margin: 0px; padding: 0px;">步：扫描（词法分析）、语法分析、语义分析、源代码优化、代码生成、目标代码优化。</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">词法分析：扫描器（Scanner）将源代的字符序列分割成一系列的记号（Token）。lex工具可实现词法扫描。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">语法分析：语法分析器将记号（Token）产生语法树（Syntax Tree）。yacc工具可实现语法分析(yacc: Yet Another Compiler Compiler)。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">语义分析：静态语义（在编译器可以确定的语义）、动态语义（只能在运行期才能确定的语义）。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">源代码优化：源代码优化器(Source Code Optimizer)，将整个语法书转化为中间代码（Intermediate Code）（中间代码是与目标机器和运行环境无关的）。中间代码使得编译器被分为前端和后端。编译器前端负责产生机器无关的中间代码；编译器后端将中间代码转化为目标机器代码。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">目标代码生成：代码生成器(Code Generator).</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">目标代码优化：目标代码优化器(Target Code Optimizer)。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">链接的主要内容是把各个模块之间相互引用的部分处理好，使得各个模块之间能够正确地衔接。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">链接的主要过程包括：地址和空间分配（Address and Storage Allocation），符号决议（Symbol Resolution），重定位（Relocation）等。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">链接分为静态链接和动态链接。</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">静态链接</strong>是指在编译阶段直接把静态库加入到可执行文件中去，这样可执行文件会比较大。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">而<strong style="margin: 0px; padding: 0px;">动态链接</strong>则是指链接阶段仅仅只加入一些描述信息，而程序执行时再从系统中把相应动态库加载到内存中去。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">静态链接的大致过程如下图所示：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><a href="http://smilejay.com/2012/01/c_compilation_stages/linking/" rel="attachment wp-att-355" style="margin: 0px; padding: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333;"><img wp-image-355"="" title="static linking" src="http://smilejay.b0.upaiyun.com/wp-content/uploads/2012/01/linking.jpg" alt="static linking" width="467" height="675" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></a></p><div style="margin: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><dl id="attachment_355"  alignnone"="" style="margin: 0px; padding: 0px;"><dd style="margin: 0px; padding: 0px;">static linking</dd></dl></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><br style="margin: 0px; padding: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><strong style="margin: 0px; padding: 0px;">二、目标文件的样子（以linux下的elf文件格式为例）</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;">&nbsp;</p><div id="embody" style="margin: 0px -20px; padding: 10px; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #ededed; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><div id="lib" style="margin: 0px 10px 0px 0px; font-size: 12px; display: inline-block;">&nbsp;</div></div><div id="article_content" style="margin: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><img src="http://img.my.csdn.net/uploads/201302/02/1359777256_5685.jpg" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">夹在<strong style="margin: 0px; padding: 0px;">ELF头</strong>和<strong style="margin: 0px; padding: 0px;">节头部表</strong>之间的都是节。一个典型的ELF可重定位目标文件包含下面几个节：</p><ul style="margin: 0px 0px 0px 30px; padding: 0px;"><li style="margin: 0px; padding: 0px; list-style: disc;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;"><strong style="margin: 0px; padding: 0px;">.text</strong></span>：已编译程序的机器代码。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">.rodata</strong>：只读数据，比如printf语句中的格式串和开关（switch）语句的跳转表。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;"><strong style="margin: 0px; padding: 0px;">.data</strong></span>：已初始化的全局C变量。局部C变量在运行时被保存在栈中，既不出现在.data中，也不出现在.bss节中。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;"><strong style="margin: 0px; padding: 0px;">.bss</strong></span>：未初始化的全局C变量。在目标文件中这个节不占据实际的空间，它仅仅是一个占位符。目标文件格式区分<strong style="margin: 0px; padding: 0px;">初始化</strong>和<strong style="margin: 0px; padding: 0px;">未初始化</strong>变量是为了空间效率在：在目标文件中，未初始化变量不需要占据任何实际的磁盘空间。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;"><strong style="margin: 0px; padding: 0px;">.symtab</strong>：<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #333300;">一个符号表（symbol table），它存放在程序中被<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">定义</span>和<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">引用</span>的<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">函数和全局变量（包括引用到的外部变量和函数，不含有局部变量）</span>的信息。一些程序员错误地认为必须通过-g选项来编译一个程序，得到符号表信息。实际上，每个可重定位目标文件在.symtab中都有一张符号表。然而，和编译器中的符号表不同，.symtab符号表不包含局部变量的表目。</span></span></li><li style="margin: 0px; padding: 0px; list-style: disc;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;"><strong style="margin: 0px; padding: 0px;">.rel.text</strong></span>：当链接噐把这个目标文件和其他文件结合时，.text节中的许多位置都需要修改。一般而言，<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">任何调用外部函数或者引用全局变量（包括本目标文件内的全局变量，因为在链接时要多个目标文件的相同段合并，这样数据的地址就会改变，所以要重定位）的指令都需要修改</span>。另一方面调用本地函数的指令则不需要修改。注意，可执行目标文件中并不需要重定位信息，因此通常省略，除非使用者显式地指示链接器包含这些信息。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">.rel.data</strong>：被模块定义或引用的任何全局变量的信息。一般而言，任何已初始化全局变量的初始值是全局变量或者外部定义函数的地址都需要被修改。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">.debug</strong>：一个调试符号表，其有些表目是程序中定义的局部变量和类型定义，有些表目是程序中定义和引用的全局变量，有些是原始的C源文件。只有以-g选项调用编译驱动程序时，才会得到这张表。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">.line</strong>：原始C源程序中的行号和.text节中机器指令之间的映射。只有以-g选项调用编译驱动程序时，才会得到这张表。</li><li style="margin: 0px; padding: 0px; list-style: disc;"><strong style="margin: 0px; padding: 0px;">.strtab</strong>：一个字符串表，其内容包括.symtab和.debug节中的符号表，以及节头部中的节名字。字符串表就是以null结尾的字符串序列。</li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">旁注：为什么未初始化的数据称为.bss?</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用术语.bss来表示未初始化的数据是很普遍的。它起始于IBM 704汇编语言（大约在1957年）中&#8221;块存储开始（Block Storage Start）&#8220;指令的首字母缩写，并沿用至今。一个记住区分.data和.bss节的简单方法是把&#8220;bss&#8221;看成是&#8220;更好地节省空间（Better Save Space）！&#8220;的缩写。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">三、静态链接</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">虚拟存储器是建立在主存--辅存物理结构基础上，有附加的硬件装置及操作系统存储管理软件组成的一种存储体系。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><img src="http://hi.csdn.net/attachment/201011/28/0_1290943844z9zN.gif" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" />&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">顾名思义，虚拟存储器是虚拟的存储器，它其实是不存在的，而仅仅是由一些硬件和软件管理的一种&#8220;系统&#8221;。他提供了三个重要的能力：1，它将主存看成一个存储在磁盘上的地址空间的高速缓存，在主存中只保存活动区域，并根据需要在磁盘和主存之间来回传送数据（这里存在&#8220;交换空间&#8221;以及&#8220;页面调度&#8221;等概念），通过这种方式，高效地利用主存；2，它为每个进程提供了统一的地址空间（以虚拟地址编址），从而简化了存储器管理；3，操作系统会为每个进程提供独立的地址空间，从而保护了每个进程的地址空间不被其他进程破坏。&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">虚拟存储器与虚拟地址空间是两个不同的概念：虚拟存储器是假想的存储器，而虚拟存储空间是假想的内存。它们之间的关系应该与主存储器与内存空间之间的关系类似。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">链接部分：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">链接就是将不同部分的<span style="margin: 0px; padding: 0px; line-height: 1.8;"><a title="代码" href="http://www.xuebuyuan.com/" target="_blank" style="margin: 0px; padding: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333;">代码</a>和数据收集和组合成一个单一文件的过程，也就是把不同目标文件合并成最终可执行文件的过程。当然，务必知道：这个过程不涉及内存。链接可以分为三种情形：1，编译时链接，也就是我们常说的静态链接；2，装载时链接；3，运行时链接。装载时链接和运行时链接合称为动态链接。在此，我们的链接部分将主要讲述静态链接，而装载时链接我们放在装载部分讲，运行时链接忽略。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1、什么是静态链接？</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">静态链接就是将多个目标文件组合在一起形成一个可执行文件，如将a.o 和 b.o 链接在一起形成 可执行文件ab。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">2、静态链接的过程包括哪几个部分？</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 静态链接包括两个大部分：一是空间和地址的分配；二是符号解析和重定位</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">（1）空间和地址的分配</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">编译器在将a.o 和 b.o 是如何合并在一起的？？</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">第二种方法就是 相似段合并：顾名思义 就是把不同目标文件的相同名字的段合并成一个段，如下图：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><img src="https://images2015.cnblogs.com/blog/376860/201608/376860-20160806114729590-1085295555.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">　　　　　　　　　　　　　　　　　　　　　　图1</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">这是编译器实际进行合并目标文件的策略。</strong></p><div style="margin: 5px 0px; color: #000000; font-size: 12px !important;"><div style="margin: 5px 0px 0px;"><span style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.8;"><a title="复制代码" style="margin: 0px; padding: 0px; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">/*a.c*/<br style="margin: 0px; padding: 0px;" />extern</span> <span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">int</span><span style="margin: 0px; padding: 0px; line-height: 1.8;"> shared; </span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">int</span><span style="margin: 0px; padding: 0px; line-height: 1.8;"> main() { </span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">int</span> a = <span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">100</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">; swap(</span>&amp;a,&amp;<span style="margin: 0px; padding: 0px; line-height: 1.8;">shared); }</span></pre><div style="margin: 5px 0px 0px;"><span style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.8;"><a title="复制代码" style="margin: 0px; padding: 0px; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;" /></a></span></div></div><div style="margin: 5px 0px; color: #000000; font-size: 12px !important;"><div style="margin: 5px 0px 0px;"><span style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.8;"><a title="复制代码" style="margin: 0px; padding: 0px; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">/*b.c*/<br style="margin: 0px; padding: 0px;" />int</span> shared = <span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">1</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">; </span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">void</span> swap(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">int</span> *a,<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">int</span> *<span style="margin: 0px; padding: 0px; line-height: 1.8;">b) {    </span>*a ^= *b ^= *a ^= *<span style="margin: 0px; padding: 0px; line-height: 1.8;">b; }  </span></pre><div style="margin: 5px 0px 0px;"><span style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.8;"><a title="复制代码" style="margin: 0px; padding: 0px; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">编译这两个文件得到&#8220;a.o&#8221;和&#8220;b.o&#8221;两个目标文件</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&#167;gcc&nbsp;-c&nbsp;a.c&nbsp;b.c</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">从代码中可以看到三个符号：share，swap和main。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">静态链接的整个过程分为两步:<br style="margin: 0px; padding: 0px;" />&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">第一步：空间和地址分配。扫描所有的输入目标文件，获得他们的各个段的长度、属性和位置，并且将输入目标文件中的符号表中所有的符号定义和符号引用收集起来，统一放到一个全局符号表。这样，连接器将能够获得所有输入目标文件的段长度，并且将它们合并，计算出输出文件中各个段合并后的长度与位置，并建立映射关系。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">这里可能会有一个问题：建立了什么样的映射关系。如上面的图1，你可能就会有所了解。映射关系就是指可执行文件与进程虚拟地址空间之间的映射。那么，这里程序还没有执行，更不会出现进程，哪里来的进程地址空间呢？此时虚拟存储器便发挥了很大的作用：虽然此时没有进程，但是每个进程的虚拟地址空间的格式都是一致的。所以，为可执行文件的每个段甚至每个符号符号分配地址也就不会有什么错了。注意：在链接之前，目标文件中的所有段的虚拟地址都是0，因为虚拟空间还没有被分配，默认都为0.等到链接之后，可执行文件中的各个段已经都被分配到了相应的虚拟地址</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">第二步：符号解析与重定位</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">首先，符号解析。解析符号就是将每个符号引用与它输入的可重定位目标文件中的符号表中的一个确定的符号定义联系起来。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">若找不到，则出现编译时错误。&nbsp;&nbsp;&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">其次是重定位；</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">不同的处理器指令对于地址的格式和方式都不一样。我们这里采用的是32位的x86处理器，介绍两种寻址方式。</p><table border="1" style="margin: 0px; padding: 0px; border-style: solid; border-color: silver; border-collapse: collapse; word-break: break-word;"><tbody style="margin: 0px; padding: 0px;"><tr style="margin: 0px; padding: 0px;"><td colspan="3" valign="top" width="573" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">X86基本重定位类型</p></td></tr><tr style="margin: 0px; padding: 0px;"><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">宏定义</p></td><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">值</p></td><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">重定位修正方法</p></td></tr><tr style="margin: 0px; padding: 0px;"><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">R_386_32</p></td><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1</p></td><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">绝对寻址修正S&nbsp;+&nbsp;A</p></td></tr><tr style="margin: 0px; padding: 0px;"><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">R_386_PC32</p></td><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">2</p></td><td valign="top" width="191" style="margin: 0px; padding: 8px 14px; border-style: solid; border-color: silver; border-collapse: collapse;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">相对寻址修正S&nbsp;+&nbsp;A&nbsp;-&nbsp;P</p></td></tr></tbody></table><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">注：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">A：保存在被修正位置的值，对于32位cpu的话，采用<br style="margin: 0px; padding: 0px;" />R_386_PC32寻址的话<br style="margin: 0px; padding: 0px;" />它应该为0xFFFFFFFC即-4，它是代表地址的四个字节；而采用<br style="margin: 0px; padding: 0px;" />R_386_32寻址，它应该为0.</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">P：被修正的位置。考虑以下程序</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">...</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1023:&nbsp;11&nbsp;11&nbsp;11</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1026:e8&nbsp;<br style="margin: 0px; padding: 0px;" />fc<br style="margin: 0px; padding: 0px;" />&nbsp;ff&nbsp;ff&nbsp;ff</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">102b:&nbsp;11&nbsp;11&nbsp;11</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">...</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">上述蓝色fc标记处即是被修正的位置，即0x1027.</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">S：符号的实际地址。也就是第一步中空间和地址分配时得到的符号虚拟地址。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">举例来说吧！链接成的可执行文件中，假设main函数的虚拟地址为0x1000，swap函数的虚拟地址为0x2000；shared变量的虚拟地址为0x3000;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">绝对地址修正：对shared变量的地址修正。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">l&nbsp;<br style="margin: 0px; padding: 0px;" />S：shared的实际地址为0x3000；</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">l&nbsp;<br style="margin: 0px; padding: 0px;" />A：被修正位置的值，即0.</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">所以最后这个重定位修正地址为：0x3000，不变！</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">相对寻址修正：对符号&#8220;swap&#8221;进行修正。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">l&nbsp;<br style="margin: 0px; padding: 0px;" />S：符号swap的实际地址，即0x2000；</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">l&nbsp;<br style="margin: 0px; padding: 0px;" />A：被修正位置的值，即0xFFFFFFFC（-4）；</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">l&nbsp;<br style="margin: 0px; padding: 0px;" />P：被修正位置，及0x1027</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">最后的重定位修正地址为：S&nbsp;+&nbsp;A&nbsp;-P&nbsp;=&nbsp;0x2000&nbsp;+（-4）-&nbsp;0x1027&nbsp;=&nbsp;0xFD5.即修正后的程序为：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">...</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1023:&nbsp;11&nbsp;11&nbsp;11</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1026:e8&nbsp;<br style="margin: 0px; padding: 0px;" />d5&nbsp;0f&nbsp;00&nbsp;00</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">102b:&nbsp;11&nbsp;11&nbsp;11</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">...</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">发现熟悉的规则了吗？下一条指令（PC）的地址为0x102b，加上这个修正值正好等于0x2000，</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">0x102b&nbsp;+&nbsp;0xFD5&nbsp;=&nbsp;0x2000，刚好是swap函数的地址。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;<img src="http://hi.csdn.net/attachment/201011/28/0_1290945325g9vI.gif" alt="" width="277" height="239" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">以上内容没有涉及到c标准库，仅仅是自己实现的两个<span style="margin: 0px; padding: 0px; line-height: 1.8;"><a title="c语言" href="http://www.xuebuyuan.com/zt/17447.html%E2%80%8E" target="_blank" style="margin: 0px; padding: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333;">c语言</a>程序之间的链接状况，也就是&#8220;程序里面的printf怎么处理&#8221;没有说明。这里，我们就要提及&#8220;静态库&#8221;的概念。其实一个静态库可以简单地看成一组目标文件的集合，即很多目标文件经过压缩打包后形成的一个文件。与静态库链接的过程是这样的：ld链接器自动查找全局符号表，找到那些为决议的符号，然后查出它们所在的目标文件，将这些目标文件从静态库中&#8220;解压&#8221;出来，最终将它们链接在一起成为一个可执行文件。也就是说只有少数几个库和目标文件被链接入了最终的可执行文件，而非所有的库一股脑地被链接进了可执行文件。<br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8;">四、装载：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">以Linux内核装载ELF为例简述一下装载过程。当我们在Linux系统的bash下输入一个命令执行某个ELF程序时，在用户层面，bash进程会调用fork()系统调用创建一个新的进程，然后新的进程调用execve()来执行指定的ELF文件，原先的bash进程继续返回等待刚才启动时新进程结束，然后继续等待用户输入命令。这里需注意，随着一个新进程的出现，操作系统会为它创建一个独立的虚拟地址空间。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">【创建虚拟地址空间】我们知道一个虚拟空间由一组映射函数将虚拟空间的各个页映射到相应的物理空间，那么创建一个虚拟空间实际上并不是创建空间而是创建映射函数所需要的数据结构。举例来说，在x86的Linux下创建虚拟地址空间实际上只是分配一个页目录（页表）就可以了，甚至不设置页映射关系，这些映射关系等到后面程序发生&#8220;缺页&#8221;时在进行设置。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">在进入execve()系统调用之后，Linux内核就开始进行真正的装载工作。在内核中，execve()系统调用相应的入口是sys_execve(),作用：参数的检查复制；调用do_execve(),流程：查找被执行的文件，读取文件的前128个字节以判断文件的格式是elf还是其它；调用search_binary_handle(),流程：通过判断文件头部的魔数确定文件的格式，并且调用相应的装载处理程序。ELF可执行文件的装载处理过程叫load_elf_binary(),它的主要步骤如下：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1，检查ELF可执行文件格式的有效性，比如魔数、程序头表中段的数量。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">2，寻找动态链接的&#8220;.interp&#8221;段，找到动态链接器的路径，以便于后面动态链接时会用上。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">3，读取可执行文件的程序头，并且创建虚拟空间与可执行文件的映射关系。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">【读取可执行文件的<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">程序头（存储了哪些部分被映射）</span>，<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">并且创建虚拟空间与可执行文件的映射关系</span>】创建虚拟空间时的页映射关系函数是虚拟空间到物理内存的映射关系，而这一步所做的事虚拟空间与可执行文件的映射关系。我们知道，当程序发生缺页是，操作系统会为物理内存分配一个物理页，然后将该缺页从磁盘中读取到内存，在设置缺页的虚拟页与物理页之间的映射关系，这样程序才可以得以正常运行。但是明显的一点是，当操作系统捕获到缺页错误时，他应当知道程序当前需要的页在可执行文件中的哪一个位置。而这就是虚拟存储与可执行文件之间的映射关系。实际上，这种映射关系仅仅是保存在操作系统内部的一个数据结构。当发生缺页错误是，CPU将控制权交给操作系统，操作系统利用专门的缺页处理例程来查询这个数据结构（映射关系），然后找到所需页所在的虚拟内存区域，以及在可执行文件的偏移，然后把该页加载进物理内存，同时将该虚拟页与物理页之间建立映射关系，最后把控制权还给进程，进程从刚才缺页位置重新开始执行。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">4，初始化ELF进程环境。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">5，将系统调用的返回地址修改成ELF可执行文件的入口点，这个入口点取决于程序的链接方式，对于静态链接的ELF可执行文件，它就是ELF文件的文件头中e_entry所指的地址；对于动态链接的ELF可执行文件，程序入口点就是动态链接器。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">【将CPU指令寄存器设置成可执行文件的入口，启动运行】对动态链接来讲，此时就启动了动态链接器。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">当load_elf_binary()执行完毕，返回至do_execve()在返回至sys_execve()时，系统调用的返回地址已经被改写成了被装载的ELF程序的入口地址了。所以，当sys_execve()系统调用从内核态返回到用户态时，EIP寄存器直接跳转到ELF程序的入口地址。此时，ELF可执行文件装载完成。接下来就是动态链接器对程序进行动态链接了。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">进程的虚拟空间：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><img src="https://images2015.cnblogs.com/blog/376860/201608/376860-20160806161516825-542579710.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><strong style="margin: 0px; padding: 0px;">五、动态链接</strong></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">动态链接ELF文件的生成过程</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><img src="https://images2015.cnblogs.com/blog/376860/201608/376860-20160806163246840-1854978573.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">主要原因有两个：第一，考虑内存和磁盘空间。静态链接极大地浪费内存空间。因为在静态链接的情况下，假设有两个程序共享一个模块，那么在静态链接后输出的两个可执行文件中各有一个共享模块的副本。如果同时运行这两个可执行文件，那么这个共享模块将在磁盘和内存中都有两个副本，对磁盘和内存造成极大地浪费；第二，程序的更新。一旦程序中的一个模块被修改，那么整个程序都要重新链接、发布给用户。如果这个程序相当的大，那么后果就会更加严重！</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;对于一个共享对象（linux下共享的模块），<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">要实现被其他程序之间的共享，就要使其代码和数据分开，每个程序都会有该模块的数据部分的副本，代码部分是共享的。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">共享模块被映射的虚拟地址空间就在上面进程虚拟空间中的 （Memery Mapping部分）</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">共享模块被映射的模样是</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;"><img src="https://images2015.cnblogs.com/blog/376860/201608/376860-20160806164350590-605002435.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">动态链接做了什么？</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">务必知道，动态链接是相对于共享对象而言的。动态链接器将程序所需要的所有共享库装载到进程的地址空间，并且将程序汇总所有为决议的符号绑定到相应的动态链接库（共享库）中，并进行重定位工作。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">对于共享模块来说，要实现共享，那么其代码对数据的访问必须是地址无关（就是代码中的地址是固定的，当然这是用的相对地址喽）的，如何做到地址无关，编译器是这么干的，每一个共享模块，都会在其代码段有一个GOT(global offset table)段，如上图所示，<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">Got是一个指针数组，用来存储外部变量的地址</span>，而代码相对于Got的距离是固定的，当对<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">外部模块变量数据和函数</span>进行访问时，就去访问变量在GOT中的位置。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">共享模块对于数据的访问方式：</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">本模块的全局变量和函数------相对地址</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">外模块的全局变量和函数-------GOT段</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">动态链接重定位时修改GOT中的值就实现了对变量的正确访问。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">动态链接的ELF文件启动过程</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">动态链接基本分为三步：先是启动动态链接器本身，然后装载所有需要的共享对象，最后重定位和初始化。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">1，动态链接器自举</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">就我们所知道的，对普通的共享对象文件来说，它的重定位工作是由动态链接器来完成；它也可以依赖于其他共享对象，其中被依赖的共享对象由动态链接器负责链接和装载。那么，对于动态链接器本身呢，它也是一个共享对象，它的重定位工作由谁完成？它是否可以依赖于其他的共享对象文件？</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">动态链接器有其自身的特殊性：首先，动态链接器本身不可以依赖其他任何共享对象（人为控制）；其次动态链接器本身所需要的全局和静态变量的重定位工作由它自身完成（自举代码）。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">我们知道，在Linux下，动态链接器ld.so实际上也是一个共享对象，操作系统同样通过映射的方式将它加载到进程的地址空间中。操作系统在加载完动态链接器之后，就将控制权交给动态链接器。动态链接器入口地址即是自举代码的入口。动态链接器启动后，它的自举代码即开始执行。自举代码首先会找到它自己的GOT(全局偏移表，记录每个段的偏移位置）。而GOT的第一个入口保存的就是&#8220;.dynamic&#8221;段的偏移地址，由此找到动态链接器本身的&#8220;.dynamic&#8221;段。通过&#8220;.dynamic&#8221;段中的信息，自举代码便可以获得动态链接器本身的重定位表和符号表等，从而得到动态链接器本身的重定位入口，然后将它们重定位。完成自举后，就可以自由地调用各种函数和全局变量。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">2，装载共享对象</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">完成自举后，<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">动态链接器将可执行文件和链接器本身的符号表都合并到一个符号表当中</span>，称之为&#8220;全局符号表&#8221;。然后链接器开始寻找可执行文件所依赖的共享对象：从&#8220;.dynamic&#8221;段中找到DT_NEEDED类型，它所指出的就是可执行文件所依赖的共享对象。由此，动态链接器可以列出可执行文件所依赖的所有共享对象，并将这些共享对象的名字放入到一个装载集合中。然后链接器开始从集合中取出一个所需要的共享对象的名字，找到相应的文件后打开该文件，读取相应的ELF文件头和&#8220;.dynamic&#8221;，然后将它相应的代码段和数据段映射到进程空间中。如果这个ELF共享对象还依赖于其他共享对象，那么将依赖的共享对象的名字放到装载集合中。如此循环，直到所有依赖的共享对象都被装载完成为止。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;"><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #ff0000;">当一个新的共享对象被装载进来的时候，它的符号表会被合并到全局符号表中</span>。所以当所有的共享对象都被装载进来的时候，全局符号表里面将包含动态链接器所需要的所有符号。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">3，重定位和初始化</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">当上述两步完成以后，动态链接器开始重新遍历可执行文件和每个共享对象的重定位表，将表中每个需要重定位的位置进行修正，原理同前。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">重定位完成以后，如果某个共享对象有&#8220;.init&#8221;段，那么动态链接器会执行&#8220;.init&#8221;段中的代码，用以实现共享对象特有的初始化过程。</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px;">此时，所有的共享对象都已经装载并链接完成了，动态链接器的任务也到此结束。同时装载链接部分也将告一段落！接下来便是程序的执行了。。。</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216095.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 15:53 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216095.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态装入(Dynamic loading) 和动态链接 (dynamic linking)的区别是什么?</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216094.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 07:42:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216094.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216094.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216094.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216094.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216094.html</trackback:ping><description><![CDATA[<div>https://blog.csdn.net/giantpoplar/article/details/46485649<br /><br /><br /><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">首先，原文地址如下：</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">http://www.quora.com/Systems-Programming/What-is-the-exact-difference-between-Dynamic-loading-and-dynamic-linking</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">翻译内容如下：</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;">动态装入(Dynamic loading) 指的是当一个<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">进程启动后</span>，将一个<em style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">可执行的文件</span></em>（原文是<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">executable，我理解为磁盘上的文件 或者驻留在内存中的例程</span>）或库映射到（或者不常发生的复制）到进程内存空间。动态链接 (dynamic linking)指的是<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">在编译（汇编）之后</span>，分解字符（<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">resolving symbols）&#8212;&#8212;把名字和地址或者偏移量联系起来。这两者难以区分的原因是，大约在<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">进程启动后，</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">编译（汇编）之后这</span>两个过程，通常对二者的微妙区别不加区分地一起完成。大概最清晰的方式来解释这二者的区别就是分别展示二者的各种组合在实践中意味着什么。</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 18px;">动态装入，静态链接。</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;"><em style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">可执行的文件</em></span>拥有一个在编译时生成的 地址/偏移量表，但是实际的代码/数据在进程刚启动时没有装到内存中。</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">这并不是大多数现在的操作系统的处理方式，但是它可能描述了一些老式的</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">overlay&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">systems.如果现在的嵌入式系统也使用这种方式，我一点也不感觉到奇怪。无论是哪种情况，其目的都是给予程序员内存控制的自由同时避免运行时的链接花费。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 18px;">静态装入，动态链接</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">这通常是在编译时确定<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">动态库的工作方式。<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;"><em style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">可执行文件</em></span>包含动态/共享库的引用，但是字符表（<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">symbol table</span>）没有或者不完整。装入和链接都在进程启动时进行，被认为是&#8220;动态的&#8221;链接但不是&#8220;动态的&#8221;装入。</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 18px;">动态装入，动态链接</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">这是你调用dlopen或其他系统里的等价调用的时候发生的事情。object<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">(.obj)</span>文件在程序的控制下动态装入（也就是开始之后），包括调用程序和库里的字符都根据那个时刻进程的可能独一无二的内存布局进行解析（把名字和地址/偏移量联系起来）.</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 18px;">静态装入，静态链接</span></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">所有的东西都在编译时解析完成。进程开始的时候所有东西都立即加载到内存中，不需要其他的解析（链接linking）。概括地说，加载发生自单个文件是不必要的，但是我认为实际的格式或者实现并不能够在不使用动态链接的情况下实现多文件的装入。</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;"><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /></span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">原文内容如下</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">Dynamic&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">loading refe</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">rs to mapping (or less often&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">copying</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">) an&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">executable&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">or&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">library&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">into a process</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">'s memory&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">after is has started</span></span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">.&nbsp; Dynamic</span>&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">linking refers to resolving symbols - associating their names with addresses&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">or&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">offsets -&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-weight: 700; word-wrap: break-word;">after compile&nbsp;time</span></span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">.&nbsp; The</span>&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">reason&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">it's hard</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">&nbsp;to make a distinction is that the two are often done together without recognizing the&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">subtle&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">d</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">i</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">stinc</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">tions around the parts I&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">put&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">in bold</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">.&nbsp; Perhaps the</span>&nbsp;<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">clearest&nbsp;</span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 21px;">way to explain is to go through what the different combinations would mean in practice.</span><br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; font-size: 15px; line-height: 21px; color: #333333; font-family: Georgia, Times, 'Times New Roman', serif;" /></p><ul style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 0px; list-style: none; word-wrap: break-word; color: #333333; background-color: #ffffff; font-family: Georgia, Times, 'Times New Roman', serif; font-size: 15px;"><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;">Dynamic loading, static linking.&nbsp; The executable has an address/offset table generated at compile time, but the actual code/data aren't loaded into memory at process start.&nbsp; This is not the way things tend to work in most systems nowadays, but it would describe some old-fashioned overlay systems.&nbsp; I'd also be utterly unsurprised if some current embedded systems work this way too.&nbsp; In either case, the goal is to give the programmer control over memory use while also avoiding the overhead of linking at runtime.</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;">Static loading, dynamic linking.&nbsp; This is how dynamic libraries specified at compile time usually work.&nbsp; The executable contains a reference to the dynamic/shared library, but the symbol table is missing or incomplete.&nbsp; Both loading and linking occur at process start, which is considered "dynamic" for linking but not for loading.</li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;">Dynamic loading, dynamic linking.&nbsp; This is what happens when you call<em style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">dlopen</em>&nbsp;or its equivalent on other systems.&nbsp; The object file is loaded dynamically under program control (i.e. after start), and symbols both in the calling program and in the library are resolved based on the process's possibly-unique memory layout at that time.<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word; line-height: 1.4;" /></li><li style="box-sizing: border-box; outline: 0px; margin: 8px 0px 0px 32px; padding: 0px; list-style-type: disc; word-wrap: break-word;">Static loading, static linking.&nbsp; Everything is resolved at compile time.&nbsp; At process start everything is loaded into memory immediately and no extra resolution (linking) is necessary.&nbsp; In the abstract it's not necessary for the loading to&nbsp;occur&nbsp;from a single file, but I don't think the actual formats or implementations (at least those I'm familiar with) can do multi-file loading without dynamic linking.</li><li></li></ul></div><img src ="http://www.cppblog.com/prayer/aggbug/216094.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 15:42 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216094.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Shared libraries and shared memory</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216093.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 07:25:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216093.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216093.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216093.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216093.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216093.html</trackback:ping><description><![CDATA[<div>https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/shared_libs_mem.htm<br /><div conbody"="" style="margin: 0px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;"><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">This topic provides information about the operating system facilities provided for sharing libraries and memory allocation.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The operating system provides facilities for the creation and use of dynamically bound shared libraries. Dynamic binding allows external symbols referenced in user code and defined in a shared library to be resolved by the loader at run time.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The shared library code is not present in the executable image on disk. Shared code is loaded into memory once in the shared library segment and shared by all processes that reference it. The advantages of shared libraries are:</p><ul ibm-colored-list=""  ibm-textcolor-gray-80"="" style="border: 0px; vertical-align: baseline; padding: 0px 0px 15px; margin: 0px; list-style: none; font-size: 1rem; line-height: 1.625rem;"><li style="margin: 0px; padding: 0px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">Less disk space is used because the shared library code is not included in the executable programs.</li><li style="margin: 0px; padding: 7px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">Less memory is used because the shared library code is only loaded once.</li><li style="margin: 0px; padding: 7px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">Load time may be reduced because the shared library code may already be in memory.</li><li style="margin: 0px; padding: 7px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">Performance may be improved because fewer page faults will be generated when the shared library code is already in memory. However, there is a performance cost in calls to shared library routines of one to eight instructions.</li></ul><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The symbols defined in the shared library code that are to be made available to referencing modules must be explicitly exported using an exports file, unless the -bexpall options is used. The first line of the file optionally contains the path name of the shared library. Subsequent lines contain the symbols to be exported.</p></div><div ibm-padding-top-1=""  ibm-padding-bottom-2"="" style="margin: 0px; padding: 18px 17.03125px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff; overflow: auto;"><ul ibm-colored-list="" ibm-textcolor-gray-80="" ibm-plain-list=""  ibm-link-list"="" style="border: 0px; vertical-align: baseline; padding: 0px 0px 15px; margin: 0px; list-style: none; font-size: 1rem; line-height: 1.625rem;"><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/shared_object_runtime_linking.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Shared objects and run time linking</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">By default, programs are linked so that a reference to a symbol imported from a shared object is bound to that definition at load time.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/lazyload.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Shared libraries and lazy loading</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">By default, when a module is loaded, the system loader automatically loads all of the module's dependents at the same time. Loading of dependents occurs because when a module is linked, a list of the module's dependent modules is saved in the loader section of the module.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/named_shared_lib_areas.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Named shared library areas</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">By default,&nbsp;<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">AIX&#174;</span>&nbsp;shares libraries among processes using a global set of segments, referred to as the global shared library area.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/create_shared_lib.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Creating a shared library</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">This section describes how to create a shared library.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/address_space.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Program address space overview</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The Base Operating System provides a number of services for programming application program memory use.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/understanding_mem_mapping.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Understanding memory mapping</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The speed at which application instructions are processed on a system is proportionate to the number of access operations required to obtain data outside of program-addressable memory.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/ipc_limits.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Interprocess communication limits</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">This topic describes system limits for Interprocess communication (IPC) mechanisms.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/creating_mapped_data.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Creating a mapped data file with the shmat subroutine</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">This section describes how to create a mapped data file using&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">shmat</span>&nbsp;subroutine.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/creating_copy_write.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Creating a copy-on-write mapped data file with the shmat subroutine</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">This section describes how to create a copy-on-write mapped data file with the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">shmat</span>&nbsp;subroutine.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/creating_shared_memory.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Creating a shared memory segment with the shmat subroutine</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">This section describes how to create a shared memory segment with the<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">&nbsp;shmat</span>&nbsp;subroutine</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/paging_space_prg_req.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">Paging space programming requirements</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The amount of paging space required by an application depends on the type of activities performed on the system. If paging space runs low, processes may be lost.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/ls_mem_manipulation_srvcs.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">List of memory manipulation services</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The memory functions operate on arrays of characters in memory called memory areas.</p></li><li ulchildlink=""  ibm-link-description"="" style="margin: 0px; padding: 12px 0px 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span ibm-ind-link"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;"><a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/ls_mem_mapping_srvcs.htm?view=kc" style="margin: 0px; padding: 0px 0px 0px 2.1em; border: 0px; vertical-align: baseline; font-size: inherit; color: #3b6caa; text-decoration: none; -webkit-transition: color 0.3s; transition: color 0.3s; display: inline-block; position: relative;">List of memory mapping services</a></span><br /><p style="margin: 0px; padding: 0px 0px 3px 2.1em; border: 0px; vertical-align: baseline; font-size: 1rem; line-height: 1.625rem;">The memory mapping subroutines operate on memory regions that have been mapped with the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">mmap</span>subroutine.</p></li></ul></div></div><img src ="http://www.cppblog.com/prayer/aggbug/216093.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 15:25 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216093.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Shared objects and run time linking</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216092.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 07:23:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216092.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216092.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216092.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216092.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216092.html</trackback:ping><description><![CDATA[<div>https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/shared_object_runtime_linking.htm<br /><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">By default, programs are linked so that a reference to a symbol imported from a shared object is bound to that definition at load time.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">This is true even if the program, or another shared object required by the program, defines the same symbol.</p><dl ibm-padding-top-1"="" style="margin: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border: 0px; vertical-align: baseline; font-size: 16px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; line-height: 26px; background-color: #ffffff; padding-top: 20px !important;"><dt dlterm"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">run time linker</dt><dd margin-left-2"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 2em !important; padding: 0px 0px 0px 40px; border: 0px; vertical-align: baseline; font-size: inherit;">A shared object that allows symbols to be rebound for appropriately linked programs</dd></dl><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">You include the run time linker in a program by linking the program with the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-brtl</span>&nbsp;option. This option has the following effects:</p><ul ibm-colored-list=""  ibm-textcolor-gray-80"="" style="border: 0px; vertical-align: baseline; padding: 0px 0px 15px; margin: 0px; list-style: none; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;"><li style="margin: 0px; padding: 0px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">A reference to the run time linker is added to your program. When program execution begins, the startup code (<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">/lib/crt0.o</span>) will call the run time linker before the main function is called.</li><li style="margin: 0px; padding: 7px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">All input files that are shared objects are listed as dependents of your program in your program's loader section. The shared objects are listed in the same order as they were specified on the command line. This causes the system loader to loadall these shared objects so that the run time linker can use their definitions. If the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-brtl</span>option is not used, a shared object that is not referenced by the program is not listed, even if it provides definitions that might be needed by another shared object used by the program.</li><li style="margin: 0px; padding: 7px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;">A shared object contained in an archive is only listed if the archive specifies automatic loading for the shared object member. You specify automatic loading for an archive member&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">foo.o</span>&nbsp;by creating a file with the following lines:<div ibm-padding-bottom-1"="" style="margin: 0px; border: 0px; vertical-align: baseline; font-size: 1rem; padding-bottom: 20px !important; line-height: 1.625rem !important;"><pre codeblock="" ibm-textcolor-default=""  ibm-background-neutral-white-40"="" id="shared_object_runtime_linkingect_runtime_linkingect_runtime_linkingect_runtime_linkingect_runtime_linkingect_runtime_linkingect_runtime_linking__a103ec44944stcl" style="margin-top: 0px; margin-bottom: 5px; padding: 5px 30px 5px 5px; border: 0px; vertical-align: baseline; font-size: 0.9375rem; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; overflow: auto; background-color: #ececec; position: relative;"><code data-clipboard-id="codeblock0" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: 0.9375rem; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; line-height: 1.3rem; overflow: auto; max-width: 100%; display: inline-block;"># autoload #! (foo.o)</code><a id="copy-btn-0" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; -webkit-transition: color 0.3s; transition: color 0.3s; position: absolute; right: 5px; top: 5px; color: #734098 !important;"><img src="https://www.ibm.com/support/knowledgecenter/images/icons/copy.png" title="复制" alt="复制" width="26" height="26" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;" /></a></pre>and adding the file as a member to the archive.</div></li><li style="margin: 0px; padding: 7px 0px 0px 28px; border: 0px; vertical-align: baseline; font-size: inherit;"><span style="color: #ff0000;">In dynamic mode, input files specified with the&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">-l</span><span style="color: #ff0000;">&nbsp;flag may end in&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">.so</span><span style="color: #ff0000;">, as well as in&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">.a</span><span style="color: #ff0000;">. That is, a reference to&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">-lfoo</span><span style="color: #ff0000;">is satisfied by the first&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">libfoo.so</span><span style="color: #ff0000;">&nbsp;or&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">libfoo.a</span><span style="color: #ff0000;">&nbsp;found in any of the directories being searched. Dynamic mode is in effect by default unless the&nbsp;</span><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; color: #ff0000;">-bstatic</span><span style="color: #ff0000;">&nbsp;option is used.</span></li></ul><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">The run time linker mimics the behavior of the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">ld</span>&nbsp;command when static linking is used, except that only exported symbols can be used to resolve symbols. Even when run time linking is used, the system loader must be able to load and resolve all symbol references in the main program and any module it depends on. Therefore, if a definition is removed from a module, and the main program has a reference to this definition, the program will not execute, even if another definition for the symbol exists in another module.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">The run time linker can rebind all references to symbols imported from another module. A reference to a symbol defined in the same module as the reference can only be rebound if the module was built with run time linking enabled for that symbol.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">Shared modules shipped with&nbsp;<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">AIX&#174; 4.2</span>&nbsp;or later have run time linking enabled for most exported variables. run time linking for functions is only enabled for functions called through a function pointer. For example, as shipped, calls to the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">malloc</span>&nbsp;subroutine within shared object&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">shr.o</span>&nbsp;in&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">/lib/libc.a</span>&nbsp;cannot be rebound, even if a definition of&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">malloc</span>&nbsp;exists in the main program or another shared module. You can link most shipped shared modules to enable run time linking for functions as well as variables by running the&nbsp;<a href="https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.cmds4/rtl_enable.htm?view=kc" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; -webkit-transition: color 0.3s; transition: color 0.3s; color: #734098 !important; text-decoration: none !important;"><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">rtl_enable</span></a>&nbsp;command.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;"><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">Operation of the run time linker</span></p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">The main program is loaded and resolved by the system loader in the usual way. If the executable program cannot be loaded for any reason, the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">exec()</span>&nbsp;subroutine fails and the run time linker is not invoked at all. If the main program loads successfully, control passes to the run time linker, which rebinds symbols as described below. When the run time linker completes, initialization routines are called, if appropriate, and then the main function is called.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">The run time linker processes modules in breadth-first search order, starting with the main executable and continuing with the direct dependents of the main executable, according to the order of dependent modules listed in each module's loader section. This order is also used when searching for the defining instance of a symbol. The "defining instance" of a symbol is usually the first instance of a symbol, but there are two exceptions. If the first instance of a symbol is an unresolved, deferred import, no defining instance exists. If the first instance is a BSS symbol (that is, with type&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">XTY_CM</span>, indicating an uninitialized variable), and there is another instance of the symbol that is neither a BSS symbol nor an unresolved, deferred import, the first such instance of the symbol is the defining instance.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">The loader section of each module lists imported symbols, which are usually defined in another specified module, and exported symbols, which are usually defined in the module itself. A symbol that is imported and exported is called a "passed-through'' import. Such a symbol appears to be defined in one module, although it is actually defined in another module.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">Symbols can also be marked as "deferred imports." References to deferred import symbols are never rebound by the run time linker. Resolution of these symbols must be performed by the system loader, either by calling&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">loadbind()</span>&nbsp;or by loading a new module explicitly with&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">load()</span>&nbsp;or&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">dlopen()</span>.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">References to imported symbols (other than deferred imports) can always be rebound. The system loader will have already resolved most imports. References to each imported symbol are rebound to the symbol's defining instance. If no defining instance exists, an error message will be printed to standard error. In addition, if the typechecking hash string of an imported symbol does not match the hash string of the defining symbol, an error message is printed.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">References to exported symbols are also rebound to their defining instances, as long as the references appear in the relocation table of the loader section. (Passed-through imports are processed along with other imports, as described above.) Depending on how the module was linked, some references to exported symbols are bound at link time and cannot be rebound. Since exported symbols are defined in the exporting module, a defining instance of the symbol will always exist, unless the first instance is a deferred import, so errors are unlikely, but still possible, when rebinding exported symbols. As with imports, errors are printed if the typechecking hash strings do not match when a symbol is rebound.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">Whenever a symbol is rebound, a dependency is added from the module using the symbol to the module defining the symbol. This dependency prevents modules from being removed from the address space prematurely. This is important when a module loaded by the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">dlopen</span>&nbsp;subroutine defines a symbol that is still being used when an attempt is made to unload the module with the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">dlclose</span>&nbsp;subroutine.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">The loader section symbol table does not contain any information about the alignment or length of symbols. Thus, no errors are detected when symbols are rebound to instances that are too short or improperly aligned. Execution errors may occur in this case.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">Once all modules have been processed, the run time linker calls the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">exit</span>&nbsp;subroutine if any run time linking errors occurred, passing an exit code of&nbsp;<samp tt"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">144</samp>&nbsp;(0x90). Otherwise, execution continues by calling initialization routines or<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">main()</span>.</p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;"><span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">Creating a shared object with run time linking enabled</span></p><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">To create a shared object enabled for run time linking, you link with the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-G</span>&nbsp;flag. When this flag is used, the following actions take place:</p><ol ibm-colored-list=""  ibm-textcolor-gray-80"="" style="border: 0px; vertical-align: baseline; padding: 0px 0px 15px; margin: 0px; list-style: none; font-size: 16px; line-height: 26px; counter-reset: item 0; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;"><li style="margin: 0px; padding: 5px 0px 0px 26px; border: 0px; vertical-align: baseline; font-size: inherit;">Exported symbols are given the&nbsp;<samp tt"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: top; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">nosymbolic</samp>&nbsp;attribute, so that all references to the symbols can be rebound by the run time linker.</li><li style="margin: 0px; padding: 7px 0px 0px 26px; border: 0px; vertical-align: baseline; font-size: inherit;">Undefined symbols are permitted (see the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: top; font-size: inherit; font-weight: 700;">-berok</span>&nbsp;option). Such symbols are marked as being imported from the symbolic module name "..". Symbols imported from ".." must be resolved by the run time linker before they can be used because the system loader will not resolve these symbols.</li><li style="margin: 0px; padding: 7px 0px 0px 26px; border: 0px; vertical-align: baseline; font-size: inherit;">The output file is given a module type of&nbsp;<samp tt"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: top; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">SRE</samp>, as if the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: top; font-size: inherit; font-weight: 700;">-bM:SRE</span>&nbsp;option had been specified.</li><li style="margin: 0px; padding: 7px 0px 0px 26px; border: 0px; vertical-align: baseline; font-size: inherit;">All shared objects listed on the command line are listed as dependents of the output module, in the same manner as described when linking a program with the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: top; font-size: inherit; font-weight: 700;">-brtl</span>&nbsp;option.</li><li style="margin: 0px; padding: 7px 0px 0px 26px; border: 0px; vertical-align: baseline; font-size: inherit;">Shared objects in archives are listed if they have the&nbsp;<samp tt"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: top; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">autoload</samp>&nbsp;attribute.</li></ol><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">Using the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-berok</span>&nbsp;option, implied by the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-G</span>&nbsp;flag, can mask errors that could be detected at link time. If you intend to define all referenced symbols when linking a module, you should use the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-bernotok</span>&nbsp;option after the&nbsp;<span b"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700;">-G</span>&nbsp;flag. This causes errors to be reported for undefined symbols.</p></div><img src ="http://www.cppblog.com/prayer/aggbug/216092.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 15:23 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216092.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linking a library to an application</title><link>http://www.cppblog.com/prayer/archive/2018/11/28/216091.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 28 Nov 2018 07:12:00 GMT</pubDate><guid>http://www.cppblog.com/prayer/archive/2018/11/28/216091.html</guid><wfw:comment>http://www.cppblog.com/prayer/comments/216091.html</wfw:comment><comments>http://www.cppblog.com/prayer/archive/2018/11/28/216091.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/prayer/comments/commentRss/216091.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/prayer/services/trackbacks/216091.html</trackback:ping><description><![CDATA[<div>https://www.ibm.com/support/knowledgecenter/SSGH2K_12.1.0/com.ibm.xlc121.aix.doc/proguide/link_app.html#link_app<br /><div ibm-padding-bottom-1"="" style="margin: 0px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff; padding-bottom: 20px !important;">You can use the same command string to link a static or shared library to your main program. For example:<br />与静态库一样的连接方式<br /><pre codeblock="" ibm-textcolor-default=""  ibm-background-neutral-white-40"="" style="margin-top: 0px; margin-bottom: 5px; padding: 5px 30px 5px 5px; border: 0px; vertical-align: baseline; font-size: 0.9375rem; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; overflow: auto; background-color: #ececec; position: relative;"><code data-clipboard-id="codeblock0" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: 0.9375rem; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; line-height: 1.3rem; overflow: auto; max-width: 100%; display: inline-block;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">xlc</span> -o myprogram main.c -L<span synph"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span var=""  ibm-item-note-alternate"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; color: #660066; font-style: italic;">directory</span></span>  -ltest</code><a id="copy-btn-0" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; -webkit-transition: color 0.3s; transition: color 0.3s; position: absolute; right: 5px; top: 5px; color: #734098 !important;"><img src="https://www.ibm.com/support/knowledgecenter/images/icons/copy.png" title="复制" alt="复制" width="26" height="26" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;" /></a></pre>where&nbsp;<em i"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">directory</em>&nbsp;is the path to the directory containing the library&nbsp;<samp codeph"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">libtest.a</samp>.</div><div ibm-padding-bottom-1"="" style="margin: 0px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff; padding-bottom: 20px !important;"><span style="color: #ff0000;">If your library uses runtime linking,</span> add the&nbsp;<span cmdname"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-all;">-brtl</span>&nbsp;option to the command:<pre codeblock="" ibm-textcolor-default=""  ibm-background-neutral-white-40"="" style="margin-top: 0px; margin-bottom: 5px; padding: 5px 30px 5px 5px; border: 0px; vertical-align: baseline; font-size: 0.9375rem; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; overflow: auto; background-color: #ececec; position: relative;"><code data-clipboard-id="codeblock1" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: 0.9375rem; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; line-height: 1.3rem; overflow: auto; max-width: 100%; display: inline-block;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">xlc</span> -brtl -o myprogram main.c -L<span synph"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;"><span var=""  ibm-item-note-alternate"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; color: #660066; font-style: italic;">directory</span></span> -ltest</code><a id="copy-btn-1" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; -webkit-transition: color 0.3s; transition: color 0.3s; position: absolute; right: 5px; top: 5px; color: #734098 !important;"><img src="https://www.ibm.com/support/knowledgecenter/images/icons/copy.png" title="复制" alt="复制" width="26" height="26" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;" /></a></pre></div><p style="margin: 0px; padding: 0px 0px 15px; border: 0px; vertical-align: baseline; font-size: 16px; line-height: 26px; color: #323232; font-family: ibm-plex-sans, 'Helvetica Neue', Arial, sans-serif; background-color: #ffffff;">By using the&nbsp;<span cmdname"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-all;">-l</span>&nbsp;option, you instruct the linker to search in the directory specified via the&nbsp;<span cmdname"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-all;">-L</span>&nbsp;option for&nbsp;<samp codeph"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">libtest.so</samp>; if it is not found, the linker searches for&nbsp;<samp codeph"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-word;">libtest.a</samp>.<br />先找静态库，再找动态库。如果找不到so，则找a<br /><br />&nbsp;<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">For additional linkage options, including options that modify the default behavior, see the&nbsp;<span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit;">AIX&#174;</span>&nbsp;<span cmdname"="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; font-weight: 700; font-family: ibm-plex-mono, Menlo, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Courier, monospace; word-break: break-all;">ld</span>&nbsp;documentation<a tipso_style=""  ibm-widget-processed"="" href="http://publib.boulder.ibm.com/infocenter/aix/v7r1/index.jsp?topic=/com.ibm.aix.cmds/doc/aixcmds3/ld.htm" target="_blank" data-widget="tooltip" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-size: inherit; -webkit-transition: color 0.3s; transition: color 0.3s; color: #734098 !important; text-decoration: none !important;">&nbsp;(http://publib.boulder.ibm.com/infocenter/aix/v7r1/index.jsp?topic=/com.ibm.aix.cmds/doc/aixcmds3/ld.htm)</a>.</span></p></div><img src ="http://www.cppblog.com/prayer/aggbug/216091.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/prayer/" target="_blank">Prayer</a> 2018-11-28 15:12 <a href="http://www.cppblog.com/prayer/archive/2018/11/28/216091.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>