<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>aaron-kelley.net &#187; C++</title>
	<atom:link href="http://aaron-kelley.net/blog/tag/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaron-kelley.net</link>
	<description>My little corner of the Internet</description>
	<lastBuildDate>Tue, 31 Jan 2012 01:57:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Getting Started with JNI and C++ under Windows</title>
		<link>http://aaron-kelley.net/blog/2007/09/getting-started-with-jni-and-c-under-windows/</link>
		<comments>http://aaron-kelley.net/blog/2007/09/getting-started-with-jni-and-c-under-windows/#comments</comments>
		<pubDate>Sun, 02 Sep 2007 01:35:58 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://aaron-kelley.net/?p=65</guid>
		<description><![CDATA[This is a step-by-step guide to getting started at using JNI with C++ under Windows. JNI is pretty much a method to call C/C++ functions from a Java application. (Actually, calling native code written in other languages, including assembly, is supported as well, but here I am focusing on C++.) This guide should give you [...]]]></description>
			<content:encoded><![CDATA[<p>This is a step-by-step guide to getting started at using <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" target="_blank">JNI</a> with C++ under Windows.</p>
<p><span id="more-65"></span>JNI is pretty much a method to call C/C++ functions from a Java application.  (Actually, calling native code written in other languages, including assembly, is supported as well, but here I am focusing on C++.)  This guide should give you all you need to know to get JNI working quickly.</p>
<p>The JNI documentation for Java 1.6 is located <a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/" target="_blank">here</a>.  The JNI 6.0 Specification is a good place to look, in fact, you&#8217;re going to read some of that to know how to call Java functions from C/C++, which Java types correspond to which C/C++ types, and so on.  What the spec lacks is concrete information on how to get JNI working.  In Chapter 2, they discuss how to name your C function and give some sample code.  I could not get this to work, so I went off looking for better examples elsewhere.</p>
<p>Anyway, here&#8217;s what you do.</p>
<p>First, you must create a Java class with prototypes for the native functions that you want to use.  You declare a function native just by including the keyword &#8216;native&#8217; in its declaration, for instance, &#8216;<tt>static native double f();</tt>&#8216; is a static function that takes no parameters and returns a double.  Note that you just end the function declaration with a semi-colon, you do not write any code for the function here.</p>
<p>This class may contain any other Java code that you like.</p>
<p>Next, you generate a header file using the native function declarations in your Java class.  The JDK includes a program called <tt>javah</tt> that does this for you.  If you have a class named <tt>MyClass</tt>, then open a command prompt and go to the directory containing <tt>MyClass.class</tt> and run the command <tt>javah MyClass</tt>.  This will generate <tt>MyClass.h</tt>.</p>
<p>Now, you must create a <tt>.cpp</tt> file and implement at a minimum all of the functions declared in <tt>MyClass.h</tt>, and compile the results to a DLL.  For information on how to do this with Microsoft Visual C++ 2005 Express Edition, see my <a href="index.php?itemid=8">previous post</a>.  I also have had success using gcc with Cygwin, see below.*  Your <tt>.cpp</tt> file should include the <tt>.h</tt> file that was generated.  You may link this code to whatever other C++ code you like (I am writing a JNI wrapper for an existing C++ library).</p>
<p>Note that when you compile this, it&#8217;s going to be looking for files in the JDK&#8217;s include folder.  You&#8217;ll need to make sure that you add this to your include path.  For instance, for me, I had to add &#8220;C:\Program Files (x86)\Java\jdk1.6.0_02\include&#8221; and &#8220;C:\Program Files (x86)\Java\jdk1.6.0_02\include\win32&#8243; to the include path.  These paths should be similar for you.  (For information in how to do this in Microsoft Visual C++ 2005/2008, see below.**)</p>
<p>Now that you have your DLL, you&#8217;re ready to actually call these functions from Java.  You must load your library from Java.  This is done by calling the <tt>System.loadLibrary()</tt> function in Java.  The line <tt>System.loadLibrary("MyClass");</tt> will try to load <tt>MyClass.dll</tt>, looking for it in the current working directory and all directories in the system path.  (You may also use <tt>System.load();</tt> and provide a full path to the file, using &#8216;/&#8217; instead of &#8216;\&#8217; to mark directories, for example, <tt>System.load("c:/MyClass.dll");</tt>.)</p>
<p>As long as you call <tt>System.loadLibrary()</tt> before you make any native calls, and your DLL is in the right place, <em>and</em> you named all of the native C++ functions correctly (which should be easy, since that&#8217;s done for you in the generated <tt>.h</tt> file), it should be working now.  If you get an UnsatisfiedLinkError exception when you make a native call, check to make sure that your C++ function header matches what was generated in the <tt>.h</tt> file.  If you get it when calling <tt>System.loadLibrary()</tt>, check to make sure that your DLL is in the right place.</p>
<p>* I&#8217;m assuming you&#8217;re already familiar with gcc and Cygwin.  To compile a DLL with gcc running under Cygwin, first use the command <tt>gcc -c MyClass.c</tt> to generate an object file, and then <tt>gcc -mno-cygwin -shared -Wl,--kill-at -o MyClass.dll MyClass.o</tt> to generate the DLL.  The <tt>-mno-cygwin</tt> switch makes sure that the DLL generated is not dependent on the Cygwin runtime (i.e., you won&#8217;t have to have Cygwin installed to run code out of it).  Similar compilation methods should work with g++, I haven&#8217;t tried it yet though.  (<a href="http://forum.java.sun.com/thread.jspa?threadID=708047&amp;messageID=4100932" target="_blank">source</a>)</p>
<p>** In Visual C++ 2005/2008, go to &#8220;Tools -&gt; Options&#8221;, then expand &#8220;Projects and Solutions&#8221; and select &#8220;VC++ Directories.&#8221;  Click on the drop-down menu under &#8220;Show directories for:&#8221; and choose &#8220;Include files.&#8221;  You can add include directories here, I had to add &#8220;C:\Program Files (x86)\Java\jdk1.6.0_02\include&#8221; and &#8220;C:\Program Files (x86)\Java\jdk1.6.0_02\include\win32&#8243;.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaron-kelley.net/blog/2007/09/getting-started-with-jni-and-c-under-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a DLL in Microsoft Visual C++ 2005 Express Edition</title>
		<link>http://aaron-kelley.net/blog/2007/08/building-a-dll-in-microsoft-visual-c-2005-express-edition/</link>
		<comments>http://aaron-kelley.net/blog/2007/08/building-a-dll-in-microsoft-visual-c-2005-express-edition/#comments</comments>
		<pubDate>Wed, 29 Aug 2007 21:49:34 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://aaron-kelley.net/?p=69</guid>
		<description><![CDATA[For class, I need to take some C++ code and compile it to a .dll file to use with JNI. I have MS Visual C++ 2005 Express (hey, it was free!). Searching around online, I found plenty of things that implied that it was possible to build a DLL with this version of Visual C++, [...]]]></description>
			<content:encoded><![CDATA[<p>For class, I need to take some C++ code and compile it to a .dll file to use with <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" target="_blank">JNI</a>.  I have MS Visual C++ 2005 Express (hey, it was free!).  Searching around online, I found plenty of things that implied that it was <em>possible</em> to build a DLL with this version of Visual C++, but no specific instructions on how to do it.  I actually found some forum posts by other people who were confused like me.  Thus this post.</p>
<p><span id="more-69"></span>It&#8217;s actually pretty easy.  Here&#8217;s what you do.</p>
<ul>
<li>Install Visual C++ 2005 Express Edition, if you have not already.</li>
<li>If you are using Windows Vista, you should install SP1 for Visual C++ and the Vista compatibility update.  These are listed under step 3 on <a href="http://msdn2.microsoft.com/en-us/express/aa975050.aspx" target="_blank">this page</a>, and I believe they also come in through Microsoft Update if you have it enabled.</li>
<li>Install the Windows Platform SDK and get Visual C++ set up to work with it.  As of the time of this writing, instructions for doing this are available <a href="http://msdn2.microsoft.com/en-us/express/aa700755.aspx" target="_blank">here</a>.  If you are using Windows Vista, see my note at the bottom of this post.*</li>
<li>After that&#8217;s done, you should notice that you have a Win32 Console Application option available in the New Project dialog of Visual C++.  That&#8217;s great, but I want a DLL, not a console application.  *grumble*</li>
<li>Here&#8217;s the counter-intuitive part.  Select the Win32 Console Application option <em>anyway</em>.</li>
<li>In the Win32 Application Wizard, which shows up after you decide where to put your project, click on Application Settings and select the DLL option.  Set any other options here as you like.</li>
<li>And now you have a new Windows DLL project.  You should be set to go.</li>
</ul>
<p>* If you are running Windows Vista, when you are following the Platform SDK directions, you&#8217;ll notice that you need to modify some files in Visual C++&#8217;s Program Files directory.  Remember that under Windows Vista, if a program that is not elevated attempts to modify a file in the Program Files folder, it will actually not modify that file and create another file in your virtual store with the modification.  So, while this modification will always show up when <em>you</em> run (non-elevated) applications, if you want these changes to apply to all users, you should run Notepad or whatever elevated (&#8220;Run as Administrator&#8221;) to make the modifications.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaron-kelley.net/blog/2007/08/building-a-dll-in-microsoft-visual-c-2005-express-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

