﻿<?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++博客-花钱的年华</title><link>http://www.cppblog.com/calvin/</link><description>C++分站</description><language>zh-cn</language><lastBuildDate>Fri, 03 Apr 2026 22:43:29 GMT</lastBuildDate><pubDate>Fri, 03 Apr 2026 22:43:29 GMT</pubDate><ttl>60</ttl><item><title>SWIG，把C/C++ API 导出成Java,C#,Python,Ruby,Php的类库</title><link>http://www.cppblog.com/calvin/archive/2005/10/24/833.html</link><dc:creator>江南白衣</dc:creator><author>江南白衣</author><pubDate>Mon, 24 Oct 2005 09:41:00 GMT</pubDate><guid>http://www.cppblog.com/calvin/archive/2005/10/24/833.html</guid><wfw:comment>http://www.cppblog.com/calvin/comments/833.html</wfw:comment><comments>http://www.cppblog.com/calvin/archive/2005/10/24/833.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/calvin/comments/commentRss/833.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/calvin/services/trackbacks/833.html</trackback:ping><description><![CDATA[<P>&nbsp;&nbsp;&nbsp; 文艺一点总结，<A href="http://www.swig.org/">SWIG</A>就是C/C++类库与泛高阶语言的万能粘合剂。<BR>&nbsp;&nbsp; 按照Swig的思路，<STRONG>C/C++是很优秀的library级代码语言，而写界面等方面则是Java、C#、Python们占优。</STRONG>所以<A href="http://www.swig.org/">SWIG(Simplified Wrapper and Interface Generator)</A> 就把C/C++写的类库，编译为上述高阶语言的Class来使用。<BR>&nbsp;&nbsp;&nbsp;&nbsp;原本担心这个项目有点边缘化，一看原来Ruby和Python早就靠它从C/C++里溜东西出来了。对于python, ruby这些家底不厚的新生代来说，C/C++的类库很有魅惑力。<BR>&nbsp;&nbsp; &nbsp;<A HREF="/cyt/">cyt</A>偶像说，"做产品的时候好呀：有丰富的API接口，支持java、python、php、perl、ruby…… 多好吹…… "<BR>&nbsp;&nbsp;&nbsp;<BR><STRONG>1. 实现原理：</STRONG><BR>&nbsp;&nbsp;&nbsp; 比如Java吧，有一个JNI机制可以直接调用C代码，但要求C代码需如是：<BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">JNIEXPORT&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;JNICALL&nbsp;Java_HelloWorld_displayHelloWorld(JNIEnv&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">env,&nbsp;jobject&nbsp;obj)&nbsp;<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Hello&nbsp;world!\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">;<BR>}</SPAN></DIV>
<P><BR>&nbsp;&nbsp; SWIG 就为你自动生成了这个冗长的wrapper文件，其中还妥善完成了类型转换，函数指针、模板这些C++功能在其他语言中的转换等诸般工作，善举阿。<BR><BR>&nbsp;<STRONG>2.操作过程：</STRONG>&nbsp;<BR>&nbsp;&nbsp;&nbsp; 很简单，特别在VC里dsp文件都写好了，把选项设为Release，rebuild一下就搞定。<BR>&nbsp;&nbsp;&nbsp; 如果要拆成慢动作，在一个Java项目里是这样的：<BR>&nbsp;&nbsp; 1.编写interface文件example.i<BR>&nbsp;&nbsp;&nbsp;&nbsp; 如果api有头文件就更简单了，下面的代码定义一个example类<BR>&nbsp;&nbsp; </P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000">module&nbsp;example<BR></SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000">{<BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">example.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000">}<BR><BR></SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000">include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">example.h</SPAN><SPAN style="COLOR: #000000">"</SPAN></DIV>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp; <FONT face="Courier New">swig -java </FONT><A href="file:///E:/ITO无敌套装/C++/swigwin-1.3.27/Examples/java/simple/example.i"><FONT face="Courier New">example.i</FONT></A>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;生成符合JNI语法(见上)的C文件： example_wrap.c<BR>&nbsp;&nbsp;&nbsp;&nbsp; 生成Java类文件：exampleJNI.java，example.java<BR><BR>&nbsp;&nbsp;&nbsp; 3.VC把example_wrap.c 和example.c 打包成dll.&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp; 4.在普通java程序里使用example.java<BR>&nbsp; </P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.loadLibrary(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">example</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;g&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Example().add(</SPAN><SPAN style="COLOR: #000000">42</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">105</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN></DIV>
<P><BR><STRONG>&nbsp; 3.总结</STRONG><BR>&nbsp;&nbsp;&nbsp; <STRONG>当你山穷水尽，准备要重新发明轮子的时候，总该想起有个</STRONG><A href="http://www.swig.org/"><STRONG>swig</STRONG></A><STRONG>，可以把C/C++的轮子，换装到你的语言上。</STRONG></P><img src ="http://www.cppblog.com/calvin/aggbug/833.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/calvin/" target="_blank">江南白衣</a> 2005-10-24 17:41 <a href="http://www.cppblog.com/calvin/archive/2005/10/24/833.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Javaer 双修C++的key point </title><link>http://www.cppblog.com/calvin/archive/2005/10/14/677.html</link><dc:creator>江南白衣</dc:creator><author>江南白衣</author><pubDate>Fri, 14 Oct 2005 03:28:00 GMT</pubDate><guid>http://www.cppblog.com/calvin/archive/2005/10/14/677.html</guid><wfw:comment>http://www.cppblog.com/calvin/comments/677.html</wfw:comment><comments>http://www.cppblog.com/calvin/archive/2005/10/14/677.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/calvin/comments/commentRss/677.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/calvin/services/trackbacks/677.html</trackback:ping><description><![CDATA[请看BlogJava上的最新版 : <A id=viewpost1_TitleUrl href="http://www.blogjava.net/calvin/archive/2005/10/10/15132.html">Javaer 双修C++的key point</A><img src ="http://www.cppblog.com/calvin/aggbug/677.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/calvin/" target="_blank">江南白衣</a> 2005-10-14 11:28 <a href="http://www.cppblog.com/calvin/archive/2005/10/14/677.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++ 完全不完全资源导引--书籍篇</title><link>http://www.cppblog.com/calvin/archive/2005/10/11/628.html</link><dc:creator>江南白衣</dc:creator><author>江南白衣</author><pubDate>Tue, 11 Oct 2005 03:55:00 GMT</pubDate><guid>http://www.cppblog.com/calvin/archive/2005/10/11/628.html</guid><wfw:comment>http://www.cppblog.com/calvin/comments/628.html</wfw:comment><comments>http://www.cppblog.com/calvin/archive/2005/10/11/628.html#Feedback</comments><slash:comments>21</slash:comments><wfw:commentRss>http://www.cppblog.com/calvin/comments/commentRss/628.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/calvin/services/trackbacks/628.html</trackback:ping><description><![CDATA[三军未动，资料先行。书山碟海，只列其中最好的。统统都可以emule找到。<BR><BR><STRONG>1.快速入门<BR></STRONG>&nbsp;&nbsp;&nbsp;<STRONG>〈Accelerated.C++〉</STRONG>2000&nbsp;<BR>&nbsp;&nbsp;&nbsp;<STRONG>〈Essential C++〉</STRONG>2002 Lipman<BR>&nbsp;&nbsp;&nbsp;&nbsp; 两本都是开篇就讲C++/STL，绝口不提C，而且都有中文版。<BR><BR><STRONG>2.枕头参考</STRONG><BR>&nbsp;&nbsp;<STRONG>〈C++.Primer.4th〉</STRONG>2005 Lippman，第3版有中文版，当入门书看也无不可。<BR>&nbsp;&nbsp;<STRONG>〈The.C++.Programming.Language.3rd〉</STRONG>Bjarne Stroustrup ，简称TCPL。<BR><BR><STRONG>3.专门书籍<BR></STRONG>&nbsp;&nbsp;&nbsp; STL参考：<STRONG>〈The.C++.Standard.Library-A.Tutorial.And.Reference〉</STRONG>1999<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号称最好的STL参考，但我觉得很像JDK文档。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Boost&nbsp;参考：<STRONG>〈Beyond the C++ Standard Library - An Introduction to Boost〉</STRONG>2005<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;介绍了Boost的一些重要类库，但其余的还是要看Boost自带文档。<BR><BR>&nbsp;&nbsp;&nbsp; C++ Template参考：<STRONG>〈C++ Templates - The Complete Guide〉</STRONG>2002&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; 对template讲得相当深，无愧于The Complete Guide的书名，ytam说可以作为MCD的前传和续集。<BR><BR><STRONG>〈C++ Common Knowledge〉</STRONG> 中文版, Stephen&nbsp;C.&nbsp;Dewhurst&nbsp;<BR>&nbsp;&nbsp;&nbsp;跑杯茶怡然的看看C++里面那些值得吹嘘的知识点是个不错的场景。<BR><BR><STRONG>4.Effective&nbsp;类</STRONG><BR>&nbsp;&nbsp; Herb Sutter, Andrei Alexandrescu合著一本：<BR>&nbsp;<STRONG>〈C++.Coding.Standards - 101.Rules.Guidelines〉</STRONG>2004<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Meyers的三本Effecive：<BR>&nbsp;<STRONG>〈Effective C++ 3rd〉</STRONG> 2005<BR>&nbsp;<STRONG>〈More Effective C++〉</STRONG><BR>&nbsp;<STRONG>〈Effective.STL.50.Specific.Ways.to.Improve.Your.Use.of.STL〉</STRONG><BR><BR>&nbsp;<STRONG>〈C++ Gotchas〉</STRONG>2002，Stephen C. Dewhurst<BR><BR>&nbsp;&nbsp;&nbsp; 对于后四本，我老觉得是在语言规范下绕来绕去，从工作角度来看是实用，从个人角度看来是无聊。<BR><BR><STRONG>5.精力过剩类</STRONG><BR>&nbsp; <STRONG>〈Modern C++ Design - Generic Programming and Design Patterns 〉</STRONG>2001 Andrei Alexandrescu&nbsp;，推荐，C++里最值得一读的书。<BR><BR>&nbsp;&nbsp; <STRONG>〈C++ Template Metaprogramming Concepts,Tools and Techniques from Boost and Beyond 〉</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; Herb Sutter的三本Exceptional，rayman说是打击信心用的:<BR>&nbsp;&nbsp; <STRONG>〈Exceptional C++ - 47 Engineering Puzzles, Programming Problems, and Solutions 〉</STRONG>1999<BR>&nbsp;&nbsp;&nbsp;<STRONG>〈More Exceptional C++〉</STRONG>2001<BR>&nbsp;&nbsp;&nbsp;<STRONG>〈Exceptional C++ Style - 40.New.Engineering.Puzzles.Programming.Problems.and.Solutions〉</STRONG>2004<BR><img src ="http://www.cppblog.com/calvin/aggbug/628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/calvin/" target="_blank">江南白衣</a> 2005-10-11 11:55 <a href="http://www.cppblog.com/calvin/archive/2005/10/11/628.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>