<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Approximatrix Forums — FORTRAN dll - Undefined reference]]></title>
	<link rel="self" href="https://forums.approximatrix.com/extern.php?action=feed&amp;tid=438&amp;type=atom" />
	<updated>2015-02-18T15:47:44Z</updated>
	<generator>PunBB</generator>
	<id>https://forums.approximatrix.com/viewtopic.php?id=438</id>
		<entry>
			<title type="html"><![CDATA[Re: FORTRAN dll - Undefined reference]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=1909#p1909" />
			<content type="html"><![CDATA[<p>If I understand correctly, basically something goes wrong when the Fortran routine in a Fortran-only DLL calls a C++ routine in a C++-only DLL.&nbsp; Is that correct?</p><p>So I have a few questions.&nbsp; &nbsp;Did you compile the C++ DLL with Simply Fortran&#039;s compiler or Microsoft&#039;s (or another)?&nbsp; If it was another compiler, do you know what calling standard it is using?&nbsp; Is it also explicitly declared to be STDCALL?</p><p>If so, does your Fortran code declare a proper INTERFACE block for the C++ routine?&nbsp; The Fortran calling routine would also need to know that the routine it is calling is a) a C-compatible routine and b) a procedure using STDCALL.&nbsp; If either isn&#039;t true, it could work but corrupt memory in the process.&nbsp; </p><p>Also, what type of variables are you passing?&nbsp; The Fortran declarations must be done properly in order to ensure reliability.&nbsp; For example, if your C routine is expecting two integers, you <em>must</em> have an appropriate INTERFACE in your Fortran code:</p><div class="codebox"><pre><code>INTERFACE
   FUNCTION TWOINT(X, Y) BIND(C)
   USE ISO_C_BINDING
      INTEGER(KIND=C_INT)::TWOINT
      INTEGER(KIND=C_INT), VALUE::X,Y
   END FUNCTION TWOINT
END INTERFACE</code></pre></div><p>The &quot;VALUE&quot; modifier is necessary because Fortran will, by default, pass by reference, whereas C expects passing by value.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2015-02-18T15:47:44Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=1909#p1909</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FORTRAN dll - Undefined reference]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=1907#p1907" />
			<content type="html"><![CDATA[<p>Jeff,</p><p>Thank you for your suggestions. I now have it all started but it is not all trouble free. :-). Here is my basic setup:</p><p>- C# calling a FORTRAN DLL<br />- FORTRAN DLL has a few subroutines which are exported. In the FORTRAN DLL one of the routines calls an external C++ DLL to perform some calculations.</p><p>With this setup, when I am running the whole code, the routines that does not have the C++ DLL interface works all the time returning correct values to C#. However, the routine with the C++ interface returned the right value the first time. After that the C# call to that routine is always crashing with the &quot;AccessViolationException is Unhandled&quot; error with following message:</p><p> &quot;Attempted to read or write protected memory. This is often an indication that other memory is corrupt.&quot;</p><p>I am unable to attach files to this otherwise I want to send you the whole code and the C++ dll so that you can check the issue.</p><p>Let me know if you have any suggestions.</p><p>Thanks<br />Sankar</p>]]></content>
			<author>
				<name><![CDATA[sankarnkp]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3473</uri>
			</author>
			<updated>2015-02-18T15:14:36Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=1907#p1907</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FORTRAN dll - Undefined reference]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=1902#p1902" />
			<content type="html"><![CDATA[<p>Sankar,</p><p>Building DLLs require considerably more details to be properly linked with C code.&nbsp; Are you working with 32- or 64-bit projects?</p><p>If 32-bit, you may run into complications due to &quot;name mangling&quot; by the Fortran compiler and assumed mangling by the C++ or C# compiler (I&#039;m not clear on what is calling what from your description).&nbsp; In 32-bit mode, you need to declare all exported functions properly.&nbsp; There is another post about this issue at: <a href="http://forums.approximatrix.com/viewtopic.php?id=380">http://forums.approximatrix.com/viewtopic.php?id=380</a>.</p><p>Using a Fortran static library to be linked in to the DLL is a bit unorthodox, but it should work.&nbsp; However, the declarations that are outlined in the <a href="http://forums.approximatrix.com/viewtopic.php?id=380">link above</a> would still be necessary in your Fortran code.&nbsp; When compiling your Fortran code, you may also need to add a flag to the Fortran Compiler flags:</p><div class="codebox"><pre><code>-fno-underscoring</code></pre></div><p>to make the function &quot;decorations&quot; in the object file conform to what C, C++, or C# would expect.</p><p>Let me know if the above gets you started.&nbsp; Some additional details about what project is calling what would be helpful.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2015-02-17T01:35:20Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=1902#p1902</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[FORTRAN dll - Undefined reference]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=1899#p1899" />
			<content type="html"><![CDATA[<p>I am trying to build a FORTRAN dll which will be used with a C# interface. The dll project contains multiple C++ dlls and one FORTRAN static library (.a). When I build the project I get &quot;Undefined reference to&quot; error for all the routines included in the FORTRAN static library. I also have an equivalent EXE version where the whole project is built as a standalone executable. The EXE is built and linked very well and I can also use that with a C# interface. </p><p>I am lost on to what is happening in the dll mode and why is there a Undefined reference error. I am using Simply Fortran 1.45.</p><p>Thank you for your help.</p><p>Regards<br />Sankar</p>]]></content>
			<author>
				<name><![CDATA[sankarnkp]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3473</uri>
			</author>
			<updated>2015-02-16T21:28:18Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=1899#p1899</id>
		</entry>
</feed>
