<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Approximatrix Forums — c and c++ project]]></title>
	<link rel="self" href="https://forums.approximatrix.com/extern.php?action=feed&amp;tid=764&amp;type=atom" />
	<updated>2024-02-15T12:52:15Z</updated>
	<generator>PunBB</generator>
	<id>https://forums.approximatrix.com/viewtopic.php?id=764</id>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=4280#p4280" />
			<content type="html"><![CDATA[<p>You only need to link to the standard C++ library if you or your external C/C++ code is using C++.&nbsp; If you&#039;re trying to link to a pure C library, there&#039;s no need to add the flag to link to the standard C++ library.&nbsp; The original post specifically asked about linking to C++, which is why I mentioned it.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2024-02-15T12:52:15Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=4280#p4280</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=4279#p4279" />
			<content type="html"><![CDATA[<p>Thank you for the overview.</p><p>I can appreciate the difficulties, although the example in this post was relatively simple.</p><p>When you say that there is a good chance that I’m not adding the standard C++ library flag for linking, what would that be?</p>]]></content>
			<author>
				<name><![CDATA[david.jeong]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3802</uri>
			</author>
			<updated>2024-02-15T01:52:06Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=4279#p4279</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=4278#p4278" />
			<content type="html"><![CDATA[<p>A C header file usually only declares prototypes for C procedures and provides constants.&nbsp; The availability of the procedures in a header file really depends on what specifically you&#039;re talking about.&nbsp; If you have an accompanying library that corresponds to the C file, then you would just need to create Fortran interfaces based on what the C header declares and link with the corresponding library.</p><p>If you&#039;re attempting to use functions in the C standard library that are mentioned in C header files, then you don&#039;t need to explicitly link to the library.&nbsp; The Fortran compiler will link to the C standard library regardless.</p><p>You only need to add the standard C++ library flag for linking if you&#039;re linking with the standard C++ library.&nbsp; There&#039;s a good chance you&#039;re not, though.</p><p>If you have the accompanying C source files for your header file, you can just add them to your project in Simply Fortran, and they should also be compiled.</p><p>To be clear, though, interfacing C routines isn&#039;t particularly simple.&nbsp; I&#039;m providing a very high level overview of what you need.&nbsp; It could get substantially more complicated quickly if you need to link multiple libraries (or you have to build a C library yourself first...).</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2024-02-14T21:12:02Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=4278#p4278</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=4277#p4277" />
			<content type="html"><![CDATA[<p>Are the interoperability procedures in this post the same if one only has a *.h file rather than *.c file?&nbsp; All I know about C language is that *.h is called a header file.&nbsp; </p><p>What I mean by the interoperability procedures is (1) to use iso_c_binding, (2) including the interface, and (3) adding -lstdc++ in the linker for compiler flags in the project options.</p><p>Thank you for any assistance you can provide.</p>]]></content>
			<author>
				<name><![CDATA[david.jeong]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3802</uri>
			</author>
			<updated>2024-02-13T23:26:48Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=4277#p4277</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3977#p3977" />
			<content type="html"><![CDATA[<p>I&#039;m guessing that the output is in the internal <em>* Console *</em> tab.&nbsp; Is that correct?&nbsp; I&#039;m surprised it&#039;s working that way on Windows, but you can probably fix it by adding a carriage return as well:</p><div class="codebox"><pre><code>    printf(&quot;aaa \r\n&quot;);</code></pre></div><p>I&#039;m wondering, does the code work as expected in an External Console without the carriage return?</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2022-09-20T12:18:10Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3977#p3977</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3970#p3970" />
			<content type="html"><![CDATA[<p>In your FORTRAN with c code, I replaced timestwo with this</p><p>float timestwo(float x)<br />{<br />&nbsp; &nbsp; printf(&quot;aaa \n&quot;);<br />&nbsp; &nbsp; printf(&quot;%f \n&quot;, x);<br />&nbsp; &nbsp; printf(&quot;bbb\n&quot;);<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; return x * 2.0;<br />}</p><p>The output ignored the newlines and gave this</p><p>aaa 3.000000 bbb</p><p>Any thoughts?<br />K</p>]]></content>
			<author>
				<name><![CDATA[kmutiny]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3620</uri>
			</author>
			<updated>2022-09-15T22:35:21Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3970#p3970</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3549#p3549" />
			<content type="html"><![CDATA[<p>Thanks.<br />Anything more you can provide would be most helpful.</p>]]></content>
			<author>
				<name><![CDATA[kmutiny]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3620</uri>
			</author>
			<updated>2020-07-28T16:04:36Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3549#p3549</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3547#p3547" />
			<content type="html"><![CDATA[<p>I&#039;ll have to work up some more complex examples, but you&#039;ll want to look into using the <em>iso_c_binding</em> module, which allows interfacing Fortran with C (and, hence, a whole collection of C-compatible languages).&nbsp; For example, I can write a very simple Fortran program that calls a C floating-point routine:</p><div class="codebox"><pre><code>program main
use iso_c_binding
implicit none

    real::timesval

    interface
        function timestwo(x) bind(c)
        use iso_c_binding
        real(kind=c_float)::timestwo
        real(kind=c_float), value::x
        end function timestwo
    end interface

    print *, &quot;Times two via C&quot;

    write(*, &#039;(1X, A21)&#039;, advance=&#039;no&#039;) &quot;Enter a real number: &quot;
    read(*, *) timesval

    write(*, &#039;(1X, A43, F8.3, A4, F8.3)&#039;) &quot;The C language tells me that two times &quot;, &amp;
                                          real(timesval, kind=c_float), &amp;
                                          &quot; is &quot;, &amp;
                                          timestwo(timesval)

end program main</code></pre></div><p>You&#039;ll notice I&#039;ve written a Fortran interface defining a function, <em>timestwo</em> that is a C function (hence the <em>bind(c)</em> appearing in the function&#039;s definition.&nbsp; Also, I&#039;ve marked the passed argument <em>x</em> as <em>value</em> because Fortran always passes by reference, while C always passes by value.&nbsp; I can then create a C file that contains:</p><div class="codebox"><pre><code>float timestwo(float x)
{
    return x * 2.0;
}</code></pre></div><p>If I add both files to a Simply Fortran project, it will just work, and the output is what you&#039;d expect:</p><div class="codebox"><pre><code> Times two via C
 Enter a real number: 6.0
     The C language tells me that two times    6.000 is   12.000</code></pre></div><p>I&#039;ll have to find some more complicated examples to display.&nbsp; But the key is using the <em>iso_c_binding</em> module.&nbsp; Simply Fortran will just accept a C file added to a project, though, so there is no complication from a development environment perspective.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2020-07-27T17:35:47Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3547#p3547</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3544#p3544" />
			<content type="html"><![CDATA[<p>Do your mixed language projects have C/C++ calling Fortran functions and vice versa?<br />Is there any extra interoperability code that is needed?<br />Can you make a mixed language project available for us to look at?<br />Or direct us to a web source to learn from that is consistent with SF?</p>]]></content>
			<author>
				<name><![CDATA[kmutiny]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3620</uri>
			</author>
			<updated>2020-07-24T17:55:54Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3544#p3544</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3539#p3539" />
			<content type="html"><![CDATA[<p>Simply Fortran has reasonably good support for the C language (in terms of the File Outline and searching for procedure definitions) and somewhat basic support for C++. It also includes the C and C++ compiler on Windows and macOS.&nbsp; You can just add the C or C++ files to a project just like any Fortran file.&nbsp; When you build the project, it should have no problem with those files.&nbsp; </p><p>A project containing C++ files does need an explicit library linking command, though, because of our default linking step.&nbsp; In Project Options under Compiler flags, one would need to add:<br /></p><div class="codebox"><pre><code>-lstdc++</code></pre></div><p>to the Linker box to ensure that the C++ standard library is also linked.&nbsp; </p><p>There&#039;s really no additional steps.&nbsp; You can mix C and Fortran source in the same project (I do this regularly), and Simply Fortran will compile it all.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2020-07-14T11:21:14Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3539#p3539</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[c and c++ project]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=3537#p3537" />
			<content type="html"><![CDATA[<p>Can SF be used as a c and c++ ide?<br />If so, can you explain how?</p>]]></content>
			<author>
				<name><![CDATA[kmutiny]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3620</uri>
			</author>
			<updated>2020-07-13T18:47:00Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=3537#p3537</id>
		</entry>
</feed>
