<?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>Fellinghaug Blog &#187; fortran</title>
	<atom:link href="http://asbjorn.fellinghaug.com/blog/tag/fortran/feed/" rel="self" type="application/rss+xml" />
	<link>http://asbjorn.fellinghaug.com/blog</link>
	<description>&#62;&#62;&#62; from fellinghaug import asbjorn; asbjorn.play()</description>
	<lastBuildDate>Thu, 19 Nov 2009 21:22:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating Python extensions in C</title>
		<link>http://asbjorn.fellinghaug.com/blog/2009/11/creating-python-extensions-in-c/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2009/11/creating-python-extensions-in-c/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 21:22:01 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C extension]]></category>
		<category><![CDATA[fortran]]></category>
		<category><![CDATA[HPC]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=210</guid>
		<description><![CDATA[Hi there!
In my current work I&#8217;m working on optimizing some parallel software, and basically tries to make programs run faster. Within this work there are focus areas such as I/O and memory utilization, which are key areas when trying to optimize software. Generally when people think of highly optimized software, they think of C/C++ and [...]]]></description>
			<content:encoded><![CDATA[<p>Hi there!</p>
<p>In my current work I&#8217;m working on optimizing some parallel software, and basically tries to make programs run faster. Within this work there are focus areas such as I/O and memory utilization, which are key areas when trying to optimize software. Generally when people think of highly optimized software, they think of C/C++ and possible assembly.</p>
<p>Python is very simple in its syntax, and its speed is not at all bad. From a HPC (<em>High Performance Computing</em>) perspective Python may not look that interesting, but combining Python&#8217;s simplicity with C&#8217;s speed, you&#8217;ll get the best from both worlds.</p>
<p>Since many of the core modules, and Python itself is written in C, it is possible to further extend Python in C. Even the official Python documentation (<a title="Python official documentation" href="http://docs.python.org">http://docs.python.org</a>) have a whole section covering the Python C API. By downloading the Python development files (and of course the GNU compiler; <em>apt-get install build-essential</em>), then you&#8217;re ready to create C extensions for Python. The overall goal for your C extensions should be to only do the very compute intensive tasks, and keep the other stuff in Python. A key observation here is to identify which parts of your program you may need to create a C extension for &#8211; and for this to be found you can use a profiler. Included in Python (<em>&#8220;batteries included&#8221;</em>) there are a couple of profilers:</p>
<ul>
<li>import profile</li>
<li>import cProfile</li>
<li>import hotshot</li>
</ul>
<p>I will not cover  these python profilers here, but rather inspire you to try them out. They might save you a lot of work and give you valuable knowledge of your software. Do some reading of python profiling <a title="Python profiling" href="http://docs.python.org/library/profile.html">here</a>.</p>
<p>Now lets create a very simple (and extremely stupid) python extension in C. We want it to contain two methods:</p>
<ol>
<li>a method which allows you to run shell commands by passing a string</li>
<li>a method which returns some text based on your input</li>
</ol>
<p>To create this extension we follow these steps: a) decide the python module name b) create the C file with the same name as the python module name c) start programming C! Let&#8217;s name our module for &#8220;cool&#8221; (not the best name)..</p>
<p>This is our <strong>cool.c</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;Python.h&gt;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
 * Takes a string argument, which is the shell command, and runs it.
 * Returns the return code of the system(cmd) call.
 * */</span>
<span style="color: #993333;">static</span> PyObject <span style="color: #339933;">*</span>cool_command<span style="color: #009900;">&#40;</span>PyObject <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> PyObject <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>cmd;
    <span style="color: #993333;">int</span> retval;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyArg_ParseTuple<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    retval <span style="color: #339933;">=</span> system<span style="color: #009900;">&#40;</span> cmd <span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">return</span> Py_BuildValue<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;i&quot;</span><span style="color: #339933;">,</span> retval<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
 * Takes a string argument, and assembles it into a new python string
 * object and returns it.
 * */</span>
<span style="color: #993333;">static</span> PyObject <span style="color: #339933;">*</span>cool_greet<span style="color: #009900;">&#40;</span>PyObject <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> PyObject <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>input;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>resp <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Hi there: &quot;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyArg_ParseTuple<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>input<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>retstr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>strlen<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>strlen<span style="color: #009900;">&#40;</span>resp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// +1 &amp;lt;- null termination</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>retstr <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    PyObject <span style="color: #339933;">*</span>retString;
&nbsp;
    strcpy<span style="color: #009900;">&#40;</span> retstr<span style="color: #339933;">,</span> resp <span style="color: #009900;">&#41;</span>;
    strcat<span style="color: #009900;">&#40;</span>retstr<span style="color: #339933;">,</span> input<span style="color: #009900;">&#41;</span>;
&nbsp;
    retString <span style="color: #339933;">=</span> Py_BuildValue<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> retstr<span style="color: #009900;">&#41;</span>;
    Py_INCREF<span style="color: #009900;">&#40;</span>retString<span style="color: #009900;">&#41;</span>;
    free<span style="color: #009900;">&#40;</span>retstr<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> retString;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* an array of PyMethodDef structure. A PyMethodDef structure is
used to describe a method of an extension type. */</span>
<span style="color: #993333;">static</span> PyMethodDef CoolMethods<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;command&quot;</span><span style="color: #339933;">,</span> cool_command<span style="color: #339933;">,</span> METH_VARARGS<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Execute a shell command.&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;greet&quot;</span><span style="color: #339933;">,</span> cool_greet<span style="color: #339933;">,</span> METH_VARARGS<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Returns a greeting to the caller.&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
PyMODINIT_FUNC initcool<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> Py_InitModule<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;cool&quot;</span><span style="color: #339933;">,</span> CoolMethods <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This C extension file is built up with the following key sections:</p>
<ul>
<li>Inclusion of headers.</li>
<li>static PyObject methods which constitutes the visible python functions of our extension module.</li>
<li>An array of PyMethodDef structures. A PyMethodDef structure describes a method of an extension type.</li>
<li>A initialization method which is named &#8220;<em>init&lt;modulename&gt;</em>&#8220;. This method has the sole purpose of calling the Py_InitModule function which takes the name and the PyMethodDef array.</li>
</ul>
<p>So there you go, this is our Python extension implemented in C with the help of the Python development headers. Now we&#8217;ll have a look at our Distutils script (<em>setup.py</em>) that will assist us with the compilation and creation of the actual python module.</p>
<p>This is our <strong>setup.py</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup, Extension
&nbsp;
module1 = Extension<span style="color: black;">&#40;</span><span style="color: #483d8b;">'cool'</span>,
        sources = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'cool.c'</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>
&nbsp;
setup <span style="color: black;">&#40;</span> name = <span style="color: #483d8b;">'CoolPackage'</span>,
        version = <span style="color: #483d8b;">'0.1'</span>,
        description = <span style="color: #483d8b;">'A descriptive and informal C extension to Python'</span>,
        ext_modules = <span style="color: black;">&#91;</span>module1<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span></pre></div></div>

<p>jada..</p>
<p>This file is much self-explained. We have basically one method named &#8220;setup&#8221; which takes a bunch of arguments. These arguments is the package name, version, description and a set of <em>Extension</em> objects. These <em>Extension</em> objects describes our C extension files. By simply running this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">python setup.py build</pre></div></div>

<p>When the compilation is done, you&#8217;ll have a build folder in the same directory as the setup.py. Go into the &#8220;<em>build/lib.linux-i686-2.6/</em>&#8220;. Then type &#8220;python&#8221; or &#8220;ipython&#8221;, and then &#8220;import cool&#8221;. Now you actually have loaded the &#8220;cool&#8221; module, and you may call &#8220;cool.greet(&#8217;Alex&#8217;)&#8221; and/or &#8220;cool.command(&#8217;ls /&#8217;)&#8221; and the actual computing happens in the C world instead of the Python world.</p>
<p>Now, keep in mind that this C extension isn&#8217;t actual doing anything useful. But, given that you have some algorithm or other problem to solve, and the time is of the essence, then utilizing the power which lays in this C extensions can give you significant time savings.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2009/11/creating-python-extensions-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integrating Fortran and C</title>
		<link>http://asbjorn.fellinghaug.com/blog/2009/07/integrating-fortran-and-c/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2009/07/integrating-fortran-and-c/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 12:45:00 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[fortran]]></category>
		<category><![CDATA[integration]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=250</guid>
		<description><![CDATA[Hi there!
&#8220;Wow&#8221; you may think. Are people still using the Fortran programming language?? I know, I was shocked too. But, apparently, Fortran is very much used within large mathematical problems / scientific work, and can therefor be seen within HPC communities. As I&#8217;m currently an IT-consultant which is working within this kind of environment, I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Hi there!</p>
<p>&#8220;Wow&#8221; you may think. Are people still using the Fortran programming language?? I know, I was shocked too. But, apparently, Fortran is very much used within large mathematical problems / scientific work, and can therefor be seen within HPC communities. As I&#8217;m currently an IT-consultant which is working within this kind of environment, I&#8217;ve got some hands-on experience with C and Fortran.</p>
<p>One of the applications I&#8217;m working on is a mix of Fortran and some C. Since I&#8217;m a software developer at heart and I have almost never ever touched Fortran, I prefer C code over Fortran. However, rewriting all the Fortran code is not an option as it is very time consuming, and that one should respect the saying &#8220;if it works, don&#8217;t change it&#8221;. But, one of my tasks is to try to optimize the code, and running the whole application through the GNU gprof tools reveals some bad code. I&#8217;m not going to dive into exactly what it was, but to solve it I wanted to use C instead of Fortran. So, then I discovered that I could simply reimplement the subroutine (basically just a function in C terms) in C and append a &#8220;_&#8221; at the end of the function name.</p>
<p>This fortran program for example calls the C function &#8220;my_c_func&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="fortran fortran" style="font-family:monospace;"><span style="color: #b1b100;">program</span> testme
    <span style="color: #000066;">implicit</span> <span style="color: #000066;">none</span>
    <span style="color: #000066;">integer</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">4</span> dx, dy, direction, i, j
    <span style="color: #666666; font-style: italic;">!real*4 dimension(:,:), allocateable :: data</span>
    <span style="color: #000066;">real</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">4</span> <span style="color: #000066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>
    dx <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span>
    dy <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span>
    direction <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
    <span style="color: #b1b100;">do</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, dy
      <span style="color: #b1b100;">do</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, dx
          <span style="color: #000066;">data</span><span style="color: #009900;">&#40;</span>i,j<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">+</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">**</span><span style="color: #cc66cc;">2</span>
      <span style="color: #b1b100;">end</span> <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">end</span> <span style="color: #b1b100;">do</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">! call our C library function</span>
    <span style="color: #b1b100;">call</span> my_func<span style="color: #009900;">&#40;</span>dx, dy, <span style="color: #000066;">data</span>, direction<span style="color: #009900;">&#41;</span>
&nbsp;
    write <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">&quot;Hello world!&quot;</span>
<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">program</span> testme</pre></div></div>

<p>And then this is the C function &#8220;my_c_func&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> my_func_<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>dx<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>dy<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> <span style="color: #339933;">*</span>data<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>direction<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span>j;
&nbsp;
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;You are now inside the C function.<span style="color: #000099; font-weight: bold;">\n</span>Awesome, right?<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;dx=%d, dy=%d, direction=%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>dx<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>dy<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>direction<span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Lets go through our data matrix..<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color:#800080;">0</span>;i<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>dy<span style="color: #009900;">&#41;</span>;i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color:#800080;">0</span>;j<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>dx<span style="color: #009900;">&#41;</span>;j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;data[%d][%d] = %.2f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>i<span style="color: #339933;">+</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Lets call the fortran file for test.f90 and the C file for c_lib.c, and lets create a <a title="Scons build system" href="http://www.scons.org">Scons</a> file instead of a Makefile to compile this example. Also, I have only tested this with the &#8220;gfortran v4.2&#8243; compiler, and gcc v4.3.3.</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
env = Environment<span style="color: black;">&#40;</span> env = <span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span> <span style="color: black;">&#41;</span>
&nbsp;
env.<span style="color: black;">Program</span><span style="color: black;">&#40;</span> target = <span style="color: #483d8b;">&quot;program1&quot;</span>, source = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;for.f90&quot;</span>, <span style="color: #483d8b;">&quot;c_lib.c&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And now for the grand finally. Type <strong>scons</strong>, and the compilation should run. There should now be a binary named &#8220;program1&#8243;. Run it, and have a closer look at the info.</p>
<p>So, to summarize, the only requirement for Fortran files to call C functions is that the respective C function has a underscore right after its function name. Also, before the fortran compilation, the C file containing the function needs to be compiled into a object file and be reachable.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2009/07/integrating-fortran-and-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
