﻿<?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++博客-Hannosogno-随笔分类-原创</title><link>http://www.cppblog.com/hannosogno/category/17453.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 09 Sep 2011 04:24:11 GMT</lastBuildDate><pubDate>Fri, 09 Sep 2011 04:24:11 GMT</pubDate><ttl>60</ttl><item><title>手写一次静态编译全过程</title><link>http://www.cppblog.com/hannosogno/archive/2011/09/08/155397.html</link><dc:creator>hannosogno~</dc:creator><author>hannosogno~</author><pubDate>Thu, 08 Sep 2011 15:25:00 GMT</pubDate><guid>http://www.cppblog.com/hannosogno/archive/2011/09/08/155397.html</guid><wfw:comment>http://www.cppblog.com/hannosogno/comments/155397.html</wfw:comment><comments>http://www.cppblog.com/hannosogno/archive/2011/09/08/155397.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hannosogno/comments/commentRss/155397.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hannosogno/services/trackbacks/155397.html</trackback:ping><description><![CDATA[<font class="Apple-style-span" color="#283769" face="Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="font-size: 12px; line-height: 18px;"><span style="color: #283769; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; "><span style="font-size: 12px; ">首先建一个静态链接库项目：lllibbb</span><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><ol style="list-style-type: decimal; font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">将它的　项目属性&nbsp;&nbsp; &nbsp; &nbsp;-&gt;&nbsp;&nbsp; &nbsp; &nbsp;配置属性&nbsp;&nbsp; &nbsp; &nbsp;-&gt;&nbsp;&nbsp; &nbsp; &nbsp;常规&nbsp;&nbsp; &nbsp; &nbsp;-&gt;&nbsp;&nbsp; &nbsp; &nbsp;配置类型 &nbsp;修改为：静态库(.lib)&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />这个一般都是在新建项目的时候直接选择的。</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">新建一个头文件：myFun.h<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#ifndef MYFUN<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#define MYFUN<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />int max_in_3( const int a, const int b, const int c );<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#endif</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">再新建一个源文件：myFun.cpp<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#include "myFun.h"<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />int max_in_3( const int a, const int b, const int c )<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />{<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> if ( a &gt; b )<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> {<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> return a &gt; c ? a : c;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> }<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> else&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> {<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> return b &gt; c ? b : c;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> }<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />}</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">编译这个静态链接库项目lllibbb，在它的debug目录下得到lllibbb.lib</li></ol><div style="font-size: 12px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="font-size: 12px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">再新建一个控制台程序：test</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><ol style="list-style-type: decimal; font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">将 &nbsp;myFun.h &nbsp;和 &nbsp;lllibbb.lib &nbsp;二个文件copy到test项目的目录下面。</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">在test的项目中添加现有的头文件myFun.h。</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">在test.cpp中编写：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#include "stdafx.h"<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#include "myFun.h"<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#include &lt;iostream&gt;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />#pragma comment(lib, "lllibbb.lib") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//这行也可以用在 &nbsp;项目属性-&gt;链接器-&gt;输入-&gt;附加依赖项 中添加 lllibbb.lib 来替代<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />int main( int argc, char** argv )<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />{ <br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> int i = 3, j = 33332, k = 323;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> std::cout &lt;&lt; max_in_3( i, j, k ) &lt;&lt; std::endl;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /> return 0;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />}</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">编译运行。</li></ol><div style="font-size: 12px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="font-size: 12px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">注意点：</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><ol style="list-style-type: decimal; font-size: 12px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">二个项目（lllibbb 和 test）的运行时库版本必须相同。这里指的版本是debug版本和非debug版本的区别。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />/MT 和 /MD 同属于非debug版本<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />/MTd 和 /MDd 同属于debug版本<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />否则的话会报很多的错误：（以下二种）<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />error LNK2005 ： 重定义错误<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />warning LNK4098： 默认库&#8220;LIBCMT&#8221;与其他库的使用冲突；请使用 /NODEFAULTLIB:library</li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">详解 ： warning LNK4098： 默认库&#8220;LIBCMT&#8221;与其他库的使用冲突；请使用 /NODEFAULTLIB:library&nbsp;&nbsp; &nbsp; &nbsp;您试图与不兼容的库链接。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />注意 运行时库现在包含可防止混合不同类型的指令。如果试图在同一个程序中使用<span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #ff0000; ">不同类型的运行时库</span>或使用<span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #ff0000; ">调试和非调试版本的运行时库</span>，则将收到此警告。例如，如果编译一个文件以使用一种运行时库，而编译另一个文件以使用另一种运行时库（例如单线程运行时库对多线程运行时库），并试图链接它们，则将得到此警告。应将所有源文件编译为使用同一个运行时库。有关更多信息，请参见使用运行时库（/MD、/ML、/MT、/LD）编译器选项。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />同样的道理，自己的工程在<span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #0000ff; ">Debug模式下应该使用/MTd 或 /MDd、</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #0000ff; ">&nbsp;&nbsp; &nbsp; &nbsp;在Release模式下应该使用/MT 或 /MD。</span></li><li style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /></li></ol><div style="font-size: 12px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Arial; line-height: normal; "><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><u style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; "><strong style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; color: red; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; text-decoration: none; font-size: 24pt; ">静态链接库LIB和动态链接库DLL的区别</span></strong></u></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">1.什么是静态连接库，什么是动态链接库<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 静态链接库与动态链接库都是共享代码的方式，如果采用静态链接库，则无论你愿不愿意，</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; color: red; ">lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">。但是若使用 DLL，该&nbsp;</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; color: red; ">DLL 不必被包含在最终 EXE 文件中</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">，EXE 文件执行时可以&#8220;动态&#8221;地引用和卸载这个与 EXE 独立的 DLL 文件。</span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><strike style="font-size: 12px; color: #333333; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">静态链接库和动态链接库的另外一个区别在于</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; color: red; ">静态链接库中不能再包含其他的动态链接库或者静态库，而在动态链接库中还可以再包含其他的动态或静态链接 库</span></strike><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; "><strike style="font-size: 12px; color: #333333; ">。静态链接库与静态链接库调用规则总体比较如下。</strike><br />实践证明：这是完全可以的，静态库中什么都能包含，静态库可以想象成就是代码！所以它什么都能做！</span></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">对于静态链接库(比较简单)：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />首先，静态链接库的使用需要库的开发者提供生成库的.h头文件和.lib文件。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />生成库的.h头文件中的声明格式如下：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />extern "C" 函数返回类型 函数名(参数表);<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />在调用程序的.cpp源代码文件中如下：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />#include "..\lib.h"<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />#pragma comment(lib,"..\\debug\\libTest.lib")&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />//指定与静态库一起链接<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " /><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />第二，因为</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; color: red; ">静态链接库是将全部指令都包含入调用程序生成的EXE文件中</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">。因此如果用的是静态链接库，那么也就不存在&#8220;导出某个函数提供给用户使用&#8221;的情况，要想用就得全要！要不就都别要！:)　　</span></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">对于动态链接库：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />动态链接库的使用需要库的开发者提供生成的.lib文件和.dll文件。或者只提供dll文件。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />首先我们必须先注意到DLL内的函数分为两种：&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />(1)DLL 导出函数，可供应用程序调用；&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />(2)DLL 内部函数，只能在 DLL 程序使用，应用程序无法调用它们。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; " />因此调用程序若想调用DLL中的某个函数就要以某种形式或方式指明它到底想调用哪一个函数。</span></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " /></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; ">总结评论：</p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; ">&nbsp;&nbsp; &nbsp; &nbsp;所以，在生成静态链接库时，不需要给函数加上导出属性。即　<span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #000000; font-family: verdana; line-height: 25px; ">int max_in_3( const int a, const int b, const int c );　即可，不像动态链接的时候，要加上<span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: simsun; font-size: 16px; line-height: 20px; color: #000000; ">extern "C" _declspec (dllexport)</span><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: simsun; font-size: 16px; line-height: 20px; color: #ff0000; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #000000; font-family: verdana; line-height: 25px; font-size: 14px; ">。　在静态链接库中，所有的函数都是公开的，因为静态链接库是将全部指令包含入调用程序生成的EXE中去的，相当于把源代码都给别人了，所有的函数当然无法隐藏。</span></span></span></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 25px; ">&nbsp;&nbsp; &nbsp; &nbsp;在使用静态链接库的时候，只要有函数的原型就可以使用，如上面可以不用&nbsp;#include "myFun.h" 。而直接声明一个&nbsp;int max_in_3( const int a, const int b, const int c ); 就可以了。复制 myFun.h 过去仅仅是为了方便。对于声明了很多类和函数的时候几乎只能复制。</span></p><p style="font-size: 12px; color: #333333; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-bottom: 10px; line-height: normal; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 25px; ">&nbsp;&nbsp; &nbsp; &nbsp;使用静态链接库后，因为静态链接库是将全部指令包含入调用程序生成的EXE中去的。所以最后生成的EXE文件中已经包含了所有的代码，因此EXE运行的时候完全不需要静态库 lllibbb.lib，单个EXE中够运行。动态链接时就完全不一样，动态链接的程序必须能找到引用的DLL文件。</span></p></span></div></div></div></div></span></span></font><img src ="http://www.cppblog.com/hannosogno/aggbug/155397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hannosogno/" target="_blank">hannosogno~</a> 2011-09-08 23:25 <a href="http://www.cppblog.com/hannosogno/archive/2011/09/08/155397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>完全从头到尾实现一个C#,C++/CLR,C++的三层架构</title><link>http://www.cppblog.com/hannosogno/archive/2011/09/08/155396.html</link><dc:creator>hannosogno~</dc:creator><author>hannosogno~</author><pubDate>Thu, 08 Sep 2011 15:23:00 GMT</pubDate><guid>http://www.cppblog.com/hannosogno/archive/2011/09/08/155396.html</guid><wfw:comment>http://www.cppblog.com/hannosogno/comments/155396.html</wfw:comment><comments>http://www.cppblog.com/hannosogno/archive/2011/09/08/155396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hannosogno/comments/commentRss/155396.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hannosogno/services/trackbacks/155396.html</trackback:ping><description><![CDATA[<span style="color: #283769; font-family: arial; font-size: 13px; line-height: 19px; ">用C++写核心的算法，用C++/CLR写界面（逻辑层）与核心算法的粘合层，用C#写界面或者逻辑<div></div><div>这是相当好的架构，没有必要把C++学到太死，C#写界面和逻辑等等对性能没有要求的代码效率还真是很高的，用起来也很顺手！</div><div></div><div></div><div>首先建好一个解决方案CSharpCppClrCppDll，里面包含有三个项目CppDll、CppClrLib、CSharpCall</div><div></div><div>CppDll是一个Win32应用程序，将其应用程序类型选择为DLL文件。这个CppDll项目不需要CLR的支持，因为它是用纯C++来实现核心代码的</div><div>CppClrLib是一个Visual C++ CLR Class Library，这个类用来包装CppDll生成的Dll文件，需要clr的支持，用来生成符合.NET标准的.NET DLL。</div><div></div><div>1：首先从CppDll中导出一个Dll的公用方法，在CppDll.cpp中添加代码：</div><div></div><div><div><div>extern "C" _declspec (dllexport)</div><div>void fill_int_array(vector&lt;int&gt;&amp; v)</div><div>{</div><div> v.clear();</div><div></div><div> for(int i = 0; i &lt; 10; ++i)</div><div> {</div><div> v.push_back(i);</div><div> }</div><div>}</div></div></div><div></div><div>接着在$(SolutionDir)$(Configuration)\ 目录下生成CppDll.dll以及其它的相当lib等等的文件。</div><div></div><div></div><div>2：再把生成的CppDll.dll等文件导入到CppClrLib项目中去。将CppClrLib项目的属性的Linker的Input设置为$(SolutionDir)$(Configuration)\CppDll.lib，加载CppDll项目生成的动态链接库。</div><div>再在CppClrLib.h中添加代码来包装CppDll中导出的方法</div><div></div><div><div><div>#pragma once</div><div></div><div>#include &lt;vector&gt;</div><div></div><div>using namespace std;</div><div>using namespace System;</div><div>using namespace System::Collections::Generic;</div><div></div><div>namespace CppClrLib {</div><div></div><div> extern "C" _declspec (dllimport)</div><div> void fill_int_array(vector&lt;int&gt;&amp; v);</div><div></div><div> public ref class MyPackage</div><div> {</div><div> // TODO: Add your methods for this class here.</div><div> public:</div><div> static void fill_a_int_array(List&lt;int&gt;^ a)</div><div> {</div><div> vector&lt;int&gt; v;</div><div> fill_int_array(v);</div><div></div><div> for(size_t i = 0; i &lt; v.size(); ++i)</div><div> {</div><div> a-&gt;Add(v[i]);</div><div> }</div><div> }</div><div> };</div><div>}</div></div></div><div></div><div>至此，就在$(SolutionDir)$(Configuration)\ 目录下生成了一个C++ .NET DLL（CppClrLib.dll），这个.NET DLL可以被所有基于.NET的程序语言所访问。</div><div>到这里，通过CppClrLib项目包装好的纯C++项目CppDll就可以完美的被C#等.NET语言所调用了。</div><div></div><div>3：在CSharpCall项目中加载第2步生成的那个符合.NET规范的DLL（CppClrLib.dll），在CSharpCall项目的引用中添加解决方案中的CppClrLib。这种完全符合.NET规范的程序集被加载之后，就可以非常方便的使用了。</div><div>这里，还有一点就是需要设置C#项目的输出目录为&#8220;..\Debug\&#8221;，这样所有的输入都在一个目录下，就可以相互调用了。<br />如果这里没有CppDll生成的非托管的DLL，就完全可以不用设置C#的输出目录了，因为VS会把项目需要的.NET程序集copy到本地目录下的。但是不会复制非引用的.NET DLL（CppDll.dll），所以，这里还是需要把输出目录全设在一起，或者是把CppDll.dll复制到默认的bin\Debug下去。</div><div><div style="display: block; text-align: left; "><img src="http://www.cppblog.com/images/cppblog_com/hannosogno/1_1.png" width="681" height="647" alt="" /></div><div style="display: block; text-align: left; "></div><div style="display: block; text-align: left; ">再在Program.cs中添加代码来调用引用的程序集中的方法了</div><div style="display: block; text-align: left; "></div><div style="display: block; text-align: left; "><div style="display: block; text-align: left; "><div style="display: block; text-align: left; ">using System;</div><div style="display: block; text-align: left; ">using System.Collections.Generic;</div><div style="display: block; text-align: left; ">using System.Linq;</div><div style="display: block; text-align: left; ">using System.Text;</div><div style="display: block; text-align: left; "></div><div style="display: block; text-align: left; ">namespace CSharpCall</div><div style="display: block; text-align: left; ">{</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp;class Program</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp;{</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;static void Main(string[] args)</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List&lt;int&gt; a = new List&lt;int&gt;();</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CppClrLib.MyPackage.fill_a_int_array(a); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div style="display: block; text-align: left; "></div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach (var item in a)</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(item);</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.ReadKey();</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div style="display: block; text-align: left; ">&nbsp;&nbsp; &nbsp;}</div><div style="display: block; text-align: left; ">}</div></div><div></div><div>最后的调用结果：</div><div><div style="display: block; text-align: left; "><img src="http://www.cppblog.com/images/cppblog_com/hannosogno/3_1.png" width="673" height="436" alt="" /></div></div><div></div><h4><a name="TOC-CSharpCall.exe-CppClrLib.dll-CppDll" style="color: rgb(68, 90, 169); "></a>最后的程序需要三个文件来运行：CSharpCall.exe、CppClrLib.dll、CppDll.dll就可以了，不需要中途的lib等等文件&#8230;</h4></div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div><span style="color: #5064ae; line-height: 36px; font-size: 24px; letter-spacing: 2px; "><br /></span></div><div><span style="color: #5064ae; line-height: 36px; font-size: 24px; letter-spacing: 2px; "><br /></span></div><div><span style="color: #5064ae; line-height: 36px; font-size: 24px; letter-spacing: 2px; ">场景：现有C++原代码，包装后供C#调用。</span></div><div><span style="color: #000000; font-family: verdana, sans-serif; line-height: 21px; font-size: 14px; "><br /></span><span style="font-family: verdana, sans-serif; line-height: 21px; font-size: 14px; ">C++的原代码，实际上可以直接编译成托管代码。</span><span style="color: #000000; font-family: verdana, sans-serif; line-height: 21px; font-size: 14px; ">MFC也好ATL也好&#8230;&#8230;这样看起来在.NET中最强大的编程语言就是C++了：它不仅可以编写托管程序，甚至可以将标准C++的代码也编译成托管程序！其实VC++最强大的地方不止如此，它还在于能够编写混合了托管和非托管的代码的程序！！！这样最大的好处不仅可以将关键代码直接编译成非托管的代码，还可以避免被反编译。<br /><br />　　假设现有C++代码如下：<br /><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 1198px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; display: inline; " /><span style="color: #0000ff; ">class</span><span style="color: #000000; ">&nbsp;UnmanagedClass&nbsp;</span><span style="display: inline; "><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></span><span style="color: #0000ff; ">public</span><span style="color: #000000; ">:<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;LPCWSTR&nbsp;GetPropertyA()&nbsp;</span><span style="color: #000000; ">{&nbsp;<span style="color: #0000ff; ">return</span><span style="color: #000000; ">&nbsp;L"Hello!"</span><span style="color: #000000; ">;&nbsp;}</span></span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; ">void</span><span style="color: #000000; ">&nbsp;MethodB(&nbsp;LPCWSTR&nbsp;)&nbsp;</span><span style="color: #000000; ">{}</span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />}</span></span><span style="color: #000000; ">;</span></div>我们只要再增加一个包装类到工程文件中：（托管的类公开的方法才会出现在最后的.NET DLL程序集中）<div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 1198px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/None.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><span style="color: #0000ff; ">namespace</span><span style="color: #000000; ">&nbsp;wrapper<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">public</span>&nbsp;<span style="color: #0000ff; ">ref</span>&nbsp;<span style="color: #0000ff; ">class</span><span style="color: #000000; ">&nbsp;ManagedClass&nbsp;</span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">public</span><span style="color: #000000; ">:<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Allocate&nbsp;the&nbsp;native&nbsp;object&nbsp;on&nbsp;the&nbsp;C++&nbsp;Heap&nbsp;via&nbsp;a&nbsp;constructor</span><span style="color: #008000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ManagedClass()&nbsp;:&nbsp;m_Impl(&nbsp;</span><span style="color: #0000ff; ">new</span><span style="color: #000000; ">&nbsp;UnmanagedClass&nbsp;)&nbsp;</span><span style="color: #000000; ">{}</span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Deallocate&nbsp;the&nbsp;native&nbsp;object&nbsp;on&nbsp;a&nbsp;destructor</span><span style="color: #008000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">~</span><span style="color: #000000; ">ManagedClass()&nbsp;</span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;m_Impl;<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; ">protected</span><span style="color: #000000; ">:<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Deallocate&nbsp;the&nbsp;native&nbsp;object&nbsp;on&nbsp;the&nbsp;finalizer&nbsp;just&nbsp;in&nbsp;case&nbsp;no&nbsp;destructor&nbsp;is&nbsp;called</span><span style="color: #008000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">!</span><span style="color: #000000; ">ManagedClass()&nbsp;</span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;m_Impl;<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; ">public</span><span style="color: #000000; ">:<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property&nbsp;String&nbsp;</span><span style="color: #000000; ">^</span><span style="color: #000000; ">&nbsp;&nbsp;get_PropertyA&nbsp;</span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;<span style="color: #000000; ">^</span>&nbsp;<span style="color: #0000ff; ">get</span><span style="color: #000000; ">()&nbsp;</span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">return</span><span style="color: #000000; ">&nbsp;gcnew&nbsp;String(&nbsp;m_Impl</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">GetPropertyA());<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; ">void</span><span style="color: #000000; ">&nbsp;MethodB(&nbsp;String&nbsp;</span><span style="color: #000000; ">^</span><span style="color: #000000; ">&nbsp;theString&nbsp;)&nbsp;</span><span style="color: #000000; ">{<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pin_ptr<span style="color: #000000; ">&lt;</span><span style="color: #0000ff; ">const</span><span style="color: #000000; ">&nbsp;WCHAR</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;str&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;PtrToStringChars(theString);<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_Impl</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">MethodB(str);<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000; "><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; ">private</span><span style="color: #000000; ">:<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/InBlock.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnmanagedClass&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;m_Impl;<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000; ">;<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />}</span></span></div><br />　然后，改变编译选项为&#8220;使用公共语言扩展 /clr&#8221;就可以了。这样，我们把代码编译成DLL文件就可以供.NET其它语言调用了。</span></div><div><span style="color: #000000; font-family: verdana, sans-serif; line-height: 21px; font-size: 14px; ">&nbsp;&nbsp; &nbsp;&nbsp;<span style="font-size: large; "><strong><em><u>所以说：C++的原代码，实际上可以直接编译成托管代码。</u></em></strong></span><br />　　最后，C#中可以象如下的代码一样调用C++类了：<div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 1198px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/None.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><span style="color: #000000; ">ManagedClass&nbsp;mc&nbsp;</span><span style="color: #000000; ">=</span>&nbsp;<span style="color: #0000ff; ">new</span><span style="color: #000000; ">&nbsp;ManagedClass();<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/None.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />mc.MethoB(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Hello</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /><img align="top" alt="" src="http://ly4cn.cnblogs.com/Images/OutliningIndicators/None.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></span><span style="color: #0000ff; ">string</span><span style="color: #000000; ">&nbsp;s&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;mc.get_PropertyA;</span></div></span></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div><span style="font-size: medium; ">总结：</span></div><div><span style="font-size: medium; ">这样，在有源代码的情况下，直接把C++代码编译成托管代码，C#就可以直接调用了，就没有了上述所说的三层架构了。</span></div><div><span style="font-size: medium; ">上述的三层架构在以下情况下比较适用：</span></div><div><span style="font-size: medium; ">1：只有ISO C++的DLL文件，没有源代码时，用C++/CLR包装是最佳选择。</span></div><div><span style="font-size: medium; ">2：接口类型有vector, list, deque或者自定义的复杂类型时，因为C++/CLR可以直接引用C++的头文件，所以这种包装方式安全可靠，而且完美无缺！</span></div><div><span style="font-size: medium; ">3：需要用ISO C++的DLL来保密自己的算法。</span></div><div><span style="font-size: medium; "><br /></span></div><div><span style="font-size: medium; ">否则的话，直接把C++编译成托管的DLL就可以完美的在.NET上被复用了！<br /><br />最后：完整的代码下载&nbsp;<a href="/Files/hannosogno/CSharpCppClrCppDll.rar">/Files/hannosogno/CSharpCppClrCppDll.rar</a></span></div></span><img src ="http://www.cppblog.com/hannosogno/aggbug/155396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hannosogno/" target="_blank">hannosogno~</a> 2011-09-08 23:23 <a href="http://www.cppblog.com/hannosogno/archive/2011/09/08/155396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自已总结的动态链接库</title><link>http://www.cppblog.com/hannosogno/archive/2011/09/08/155395.html</link><dc:creator>hannosogno~</dc:creator><author>hannosogno~</author><pubDate>Thu, 08 Sep 2011 15:19:00 GMT</pubDate><guid>http://www.cppblog.com/hannosogno/archive/2011/09/08/155395.html</guid><wfw:comment>http://www.cppblog.com/hannosogno/comments/155395.html</wfw:comment><comments>http://www.cppblog.com/hannosogno/archive/2011/09/08/155395.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hannosogno/comments/commentRss/155395.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hannosogno/services/trackbacks/155395.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一、概念：1、&nbsp;&nbsp;静态链接库：应用程序从函数库中得到所的函数的执行代码，然后把招生代码自身的执行文件中，应用程序在运行时，不再需要函数库的支持。2、&nbsp;&nbsp;动态链接库：应用程序的中不包含函数库中的函数的执行代码，编译和连接时只是包含包含它们的参考，运行时再将它们的执行代码加入内存，所以在程序运行时需要函数的支持。二、编写DLL：建立一个Windows Dynam...&nbsp;&nbsp;<a href='http://www.cppblog.com/hannosogno/archive/2011/09/08/155395.html'>阅读全文</a><img src ="http://www.cppblog.com/hannosogno/aggbug/155395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hannosogno/" target="_blank">hannosogno~</a> 2011-09-08 23:19 <a href="http://www.cppblog.com/hannosogno/archive/2011/09/08/155395.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自动调正一副图片的方向</title><link>http://www.cppblog.com/hannosogno/archive/2011/09/08/155392.html</link><dc:creator>hannosogno~</dc:creator><author>hannosogno~</author><pubDate>Thu, 08 Sep 2011 15:08:00 GMT</pubDate><guid>http://www.cppblog.com/hannosogno/archive/2011/09/08/155392.html</guid><wfw:comment>http://www.cppblog.com/hannosogno/comments/155392.html</wfw:comment><comments>http://www.cppblog.com/hannosogno/archive/2011/09/08/155392.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hannosogno/comments/commentRss/155392.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hannosogno/services/trackbacks/155392.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;1&nbsp;//&nbsp;OpenCV_Test.cpp&nbsp;:&nbsp;定义控制台应用程序的入口点。&nbsp;&nbsp;2&nbsp;//&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;4&nbsp;#include&nbsp;"stdafx.h"&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;6&nbsp;#include&nbsp;"c...&nbsp;&nbsp;<a href='http://www.cppblog.com/hannosogno/archive/2011/09/08/155392.html'>阅读全文</a><img src ="http://www.cppblog.com/hannosogno/aggbug/155392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hannosogno/" target="_blank">hannosogno~</a> 2011-09-08 23:08 <a href="http://www.cppblog.com/hannosogno/archive/2011/09/08/155392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>opencv 绘制直方图</title><link>http://www.cppblog.com/hannosogno/archive/2011/09/08/155388.html</link><dc:creator>hannosogno~</dc:creator><author>hannosogno~</author><pubDate>Thu, 08 Sep 2011 14:50:00 GMT</pubDate><guid>http://www.cppblog.com/hannosogno/archive/2011/09/08/155388.html</guid><wfw:comment>http://www.cppblog.com/hannosogno/comments/155388.html</wfw:comment><comments>http://www.cppblog.com/hannosogno/archive/2011/09/08/155388.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hannosogno/comments/commentRss/155388.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hannosogno/services/trackbacks/155388.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #283769; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>IplImage *src = 0;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>IplImage *histimg = 0;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>CvHistogram *hist = 0;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>int hdims = 50; &nbsp; &nbsp; // 划分HIST的个数，越高越精确</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>float hranges_arr[] = {0,255};</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>float* hranges = hranges_arr;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>int bin_w;&nbsp;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>float max_val;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>int i;</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>src=cvLoadImage( "d:\\_t\\Class Pic\\Lena.jpg", 0 );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvNamedWindow( "Histogram", 1 );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>hist = cvCreateHist( 1, &amp;hdims, CV_HIST_ARRAY, &amp;hranges, 1 ); &nbsp;// 计算直方图</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>histimg = cvCreateImage( cvSize(320,200), 8, 3 );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvZero( histimg );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvCalcHist( &amp;src, hist, 0, 0 ); // 计算直方图</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvGetMinMaxHistValue( hist, 0, &amp;max_val, 0, 0 ); &nbsp;// 只找最大值</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvConvertScale( hist-&gt;bins, hist-&gt;bins, max_val ? 255. / max_val : 0., 0 ); // 缩放 bin 到区间 [0,255]</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvZero( histimg );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>bin_w = histimg-&gt;width / hdims; &nbsp;// hdims: 条的个数，则 bin_w 为条的宽度</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>// 画直方图</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>for( i = 0; i &lt; hdims; i++ )</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>{</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>double val = ( cvGetReal1D(hist-&gt;bins,i)*histimg-&gt;height/255 );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>CvScalar color = CV_#ffff00; //(hsv2rgb(i*180.f/hdims);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvRectangle( histimg, cvPoint(i*bin_w,histimg-&gt;height),</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvPoint((i+1)*bin_w,(int)(histimg-&gt;height - val)),</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>color, 1, 8, 0 );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvShowImage( "Histogram", histimg );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvWaitKey(0);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvDestroyWindow("Histogram");</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvReleaseImage( &amp;src );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvReleaseImage( &amp;histimg );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: pre; "> </span>cvReleaseHist ( &amp;hist );</div></span><img src ="http://www.cppblog.com/hannosogno/aggbug/155388.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hannosogno/" target="_blank">hannosogno~</a> 2011-09-08 22:50 <a href="http://www.cppblog.com/hannosogno/archive/2011/09/08/155388.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>