<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[linux 静态库创建与使用]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 26 Oct 2010 05:27:21 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	静态库其实就是一堆.o文件的集合.<br/>下面引用一个例子来说说静态库的创建与使用<br/><br/>//1.c中有一个简单的输出函数 func1<br/>#include &lt;stdio.h&gt;<br/><br/>void func1()<br/>&#123;<br/>printf(&quot;call func1&#92;n&quot;);<br/>&#125;<br/><br/>//2.c中也有一个简单的输出函数 func2<br/>#include &lt;stdio.h&gt;<br/><br/>void func2()<br/>&#123;<br/>printf(&quot;call func2&#92;n&quot;);<br/>&#125;<br/><br/><br/>gcc生成目标文件<br/>gcc -c 1.c 2.c<br/>这样会生成1.o 2.o<br/><br/>接下来&nbsp;&nbsp;我们创建静态库<br/>使用ar命令<br/>ar rv libmytest.a 1.o 2.o<br/>这样就生成了静态库libmytest.a<br/><br/>使用静态库<br/>//test.c<br/>int main()<br/>&#123;<br/>func1();<br/>func2();<br/>return 0;<br/>&#125;<br/><br/>编译<br/>gcc -o test test.c libmytest.a<br/>这样就生成了可执行文件<br/>./test<br/>call func1<br/>call func2<br/><br/><br/>PS:说说ar的简单使用<br/>ar r test.a 1.o 2.o&nbsp;&nbsp;r选项是插入&nbsp;&nbsp;将1.o, 2.o插入到test.a中 加上v 会打印提示<br/>ar d test.a 1.o&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d选项是删除&nbsp;&nbsp;将1.o从test.a中删除<br/>ar t test.a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t选项是列出详细信息&nbsp;&nbsp;打印出test.a中的包含的文件<br/>ar x test.a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x选项是解压&nbsp;&nbsp;解压出test.a中的文件<br/> <br/>来源：http://hi.baidu.com/teng0210/blog/item/8df32a7ae666e3f90ad18780.html<br/><br/>上面的创建是没有问题的，但是你发现没有，1.c 2.c里面都有#include &quot;stdio.h&quot;,而这时候对大规模开发程序是没有得到重复用的，<br/>把它放到一个文件里,于是头文件出现了.h文件：<br/>如下目录结构：<br/>include/&nbsp;&nbsp;lib/&nbsp;&nbsp;libprint.a&nbsp;&nbsp;main*&nbsp;&nbsp;main.cpp&nbsp;&nbsp;Makefile&nbsp;&nbsp;print.cpp&nbsp;&nbsp;run.sh<br/>print.cpp：注意没有包含 stdio.h<br/><br/><div class="code">#include &quot;print.h&quot;<br/>void printhello()&#123;<br/>printf(&quot;Hello, world&#92;n&quot;);<br/>&#125;</div><br/><br/><br/>vi include/print.h :包含了stdio.h<br/><div class="code">#include &lt;stdio.h&gt;<br/>//#include &lt;cstdio&gt;<br/>void printhello();</div><br/><br/>vi main.cpp 这儿得包含了print.h了。<br/><br/><br/><div class="code">#include &quot;print.h&quot;<br/>using namespace std;<br/>int main(void)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;printhello();<br/>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br/>&#125;</div><br/><br/>编译如下：<br/><div class="code">g++ -o ./lib/print.o -c print.cpp -I/root/c++/include<br/>ar q&nbsp;&nbsp;./lib/libprint.a&nbsp;&nbsp;./lib/print.o<br/>g++&nbsp;&nbsp;main.cpp -o main&nbsp;&nbsp;-I/root/c++/include&nbsp;&nbsp;-L/root/c++/lib&nbsp;&nbsp;-lprint</div><br/><br/><br/><div class="code">./main <br/>Hello, world</div><br/><br/>把上面的print.cpp 里面的#include &quot;print.h&quot;修改为:#include &lt;stdio.h&gt;,去掉main.cpp里面的#include &quot;print.h&quot;,后按照上面开头那样用g++编译，会在最后报错：<br/><br/><div class="code"># gcc main.cpp ./lib/print.o&nbsp;&nbsp; <br/>main.cpp: In function ¡®int main()¡¯:<br/>main.cpp:5: error: ¡®printhello¡¯ was not declared in this scop</div><br/>所以，建议还是加上.h头文件为好！<br/><br/>g++ -c print.cpp -o lib/print.o<br/>cd lib/<br/>ar q libprint.a print.o <br/>cd ..<br/>g++ main.cpp ./lib/libprint.a&nbsp;&nbsp; <br/>main.cpp: In function ¡®int main()¡¯:<br/>main.cpp:5: error: ¡®printhello¡¯ was not declared in this s sco<br/><br/>全变为:.c后用：<br/>gcc -c print.c -o lib/print.o<br/>cd lib/<br/>ar q libprint.a print.o <br/>cd ..<br/>gcc main.c ./lib/libprint.a<br/><br/>Ok了：<br/><div class="code">./main <br/>Hello, world</div>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] linux 静态库创建与使用]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>