public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* need help on gfortran and C++
@ 2012-01-11  8:40 Mojtaba Seifi
  2012-01-11  8:57 ` Tobias Burnus
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Mojtaba Seifi @ 2012-01-11  8:40 UTC (permalink / raw)
  To: fortran, gcc-help

[-- Attachment #1: Type: text/plain, Size: 718 bytes --]

Dear Sir/Madam,

I'm trying to compile a simple fortran code using gfortran and then 
using this library in a C++ code (compiling by g++). The simple codes 
for fortran and C++ are attached. I use the following commands in 
terminal (ubuntu) for this purpose:

gfortran -c FSub.f90
ar -rcs libFsub.a FSub.o
g++ -c main.cpp
g++ -o main.exe main.o -L/`pwd` -lFsub


But I get the following error:
--------------
main.o: In function `main':
main.cpp:(.text+0x41): undefined reference to `MULTAB_'
collect2: ld returned 1 exit status
--------------
I couldn't find any solution on the Internet for this issue.
Would you please help me on this. I'm new in linux and have no idea how 
to fix this.

Thank you.

-- 
Mojtaba


[-- Attachment #2: FSub.f90 --]
[-- Type: text/x-fortran, Size: 123 bytes --]



INTEGER FUNCTION MULTAB(A, B)
    IMPLICIT NONE

    INTEGER, INTENT(IN) :: A,B

    MULTAB = A*B

END FUNCTION MULTAB



[-- Attachment #3: main.cpp --]
[-- Type: text/x-c++src, Size: 541 bytes --]

//============================================================================
// Name        : CPP_Calls_GF.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

extern "C" int MULTAB_(int &a, int &b);


int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

	int a=3, b=8;

	cout << MULTAB_(a, b) << endl;

	return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: need help on gfortran and C++
  2012-01-11  8:40 need help on gfortran and C++ Mojtaba Seifi
@ 2012-01-11  8:57 ` Tobias Burnus
  2012-01-11 12:53   ` N.M. Maclaren
  2012-01-11  9:37 ` Paul Richard Thomas
  2012-01-11 12:20 ` Jonathan Wakely
  2 siblings, 1 reply; 5+ messages in thread
From: Tobias Burnus @ 2012-01-11  8:57 UTC (permalink / raw)
  To: Mojtaba Seifi; +Cc: fortran, gcc-help


On 01/11/2012 08:33 AM, Mojtaba Seifi wrote:
> But I get the following error:
> main.cpp:(.text+0x41): undefined reference to `MULTAB_'
>
> extern "C" int MULTAB_(int&a, int&b);
>
> INTEGER FUNCTION MULTAB(A, B)

In gfortran, the "function multab" translates into "multab_" and not 
"MULTAB_". You have to change the 'extern "C"' declaration to lower case.

(It depends on the Fortran compiler and the used options whether the 
symbol becomes lower or upper case and whether it has one, two or no 
trailing underscores.)


I assume that it is sufficient for you to change the name in the C++ 
file. However, I want to point out that Fortran also provides means for 
better interoperability with C ["bind(C)"] - and thus with C++'s 'extern 
"C"', cf. 
http://gcc.gnu.org/onlinedocs/gfortran/Mixed_002dLanguage-Programming.html

Additionally, I want to point out that without using Bind(C) there might 
be additional issues - and differences between compilers; in particular, 
the way, "float"/"REAL" values and complex-number-returning functions 
are implemented, differs among compilers.

Tobias

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: need help on gfortran and C++
  2012-01-11  8:40 need help on gfortran and C++ Mojtaba Seifi
  2012-01-11  8:57 ` Tobias Burnus
@ 2012-01-11  9:37 ` Paul Richard Thomas
  2012-01-11 12:20 ` Jonathan Wakely
  2 siblings, 0 replies; 5+ messages in thread
From: Paul Richard Thomas @ 2012-01-11  9:37 UTC (permalink / raw)
  To: Mojtaba Seifi; +Cc: fortran, gcc-help

Dear Mojtaba,

Please note that this list is devoted to the development of gfortran;
go to the likes of comp.lang.fortran for help with the language
itself.  Better still read the book or google "C fortran
interoperability".  That said, this works:
(Note well the arguments in the prototype for 'multab' and the
provision of a C binding name in the fortran. Also, uppercase is
traditional for fortran but is by no means obligatory - )
it is simply ignored.)

#include <iostream>
using namespace std;
extern "C" int multab(int *a, int *b);
int main()
{
  int a=3, b=8;
  while (a > 0 && b > 0)
    {
      cout << "please input a and b" << endl;
      cin >> a >> b;
      cout << "a * b = " << multab(&a, &b) << endl;
    }
  return 0;
}

function multab(a, b) bind(c, name = "multab")
  use iso_c_binding
  implicit none
  integer(c_int), intent(in), bind(c) :: a, b
  integer(c_int) :: multab
  multab = a * b
end function multab

pault@paul-laptop:/tmp# g++ main.cpp FSub.f90
pault@paul-laptop:/tmp# ./a.out
please input a and b
2 2
a * b = 4
please input a and b

Good luck

Paul

On Wed, Jan 11, 2012 at 8:33 AM, Mojtaba Seifi <mojtaba.seifi@gmail.com> wrote:
> Dear Sir/Madam,
>
> I'm trying to compile a simple fortran code using gfortran and then using
> this library in a C++ code (compiling by g++). The simple codes for fortran
> and C++ are attached. I use the following commands in terminal (ubuntu) for
> this purpose:
>
> gfortran -c FSub.f90
> ar -rcs libFsub.a FSub.o
> g++ -c main.cpp
> g++ -o main.exe main.o -L/`pwd` -lFsub
>
>
> But I get the following error:
> --------------
> main.o: In function `main':
> main.cpp:(.text+0x41): undefined reference to `MULTAB_'
> collect2: ld returned 1 exit status
> --------------
> I couldn't find any solution on the Internet for this issue.
> Would you please help me on this. I'm new in linux and have no idea how to
> fix this.
>
> Thank you.
>
> --
> Mojtaba
>



-- 
The knack of flying is learning how to throw yourself at the ground and miss.
       --Hitchhikers Guide to the Galaxy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: need help on gfortran and C++
  2012-01-11  8:40 need help on gfortran and C++ Mojtaba Seifi
  2012-01-11  8:57 ` Tobias Burnus
  2012-01-11  9:37 ` Paul Richard Thomas
@ 2012-01-11 12:20 ` Jonathan Wakely
  2 siblings, 0 replies; 5+ messages in thread
From: Jonathan Wakely @ 2012-01-11 12:20 UTC (permalink / raw)
  To: Mojtaba Seifi; +Cc: fortran, gcc-help

On 11 January 2012 07:33, Mojtaba Seifi wrote:
> Dear Sir/Madam,
>
> I'm trying to compile a simple fortran code using gfortran and then using
> this library in a C++ code (compiling by g++). The simple codes for fortran
> and C++ are attached. I use the following commands in terminal (ubuntu) for
> this purpose:
>
> gfortran -c FSub.f90
> ar -rcs libFsub.a FSub.o
> g++ -c main.cpp
> g++ -o main.exe main.o -L/`pwd` -lFsub
>
>
> But I get the following error:
> --------------
> main.o: In function `main':
> main.cpp:(.text+0x41): undefined reference to `MULTAB_'
> collect2: ld returned 1 exit status
> --------------
> I couldn't find any solution on the Internet for this issue.
> Would you please help me on this. I'm new in linux and have no idea how to
> fix this.

Although Tobias and Paul have given you the best ways to solve your
problem, I just want to point out you could have run 'nm' to see the
symbol in the FSub.o object:

$ gfortran -c FSub.f90
$ nm FSub.o
0000000000000000 T multab_

That would have shown you the symbol was called multab_ not MULTAB_

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: need help on gfortran and C++
  2012-01-11  8:57 ` Tobias Burnus
@ 2012-01-11 12:53   ` N.M. Maclaren
  0 siblings, 0 replies; 5+ messages in thread
From: N.M. Maclaren @ 2012-01-11 12:53 UTC (permalink / raw)
  To: fortran, gcc-help

On Jan 11 2012, Tobias Burnus wrote:
>
>I assume that it is sufficient for you to change the name in the C++ 
>file. However, I want to point out that Fortran also provides means for 
>better interoperability with C ["bind(C)"] - and thus with C++'s 'extern 
>"C"', cf. 
>http://gcc.gnu.org/onlinedocs/gfortran/Mixed_002dLanguage-Programming.html

A slight correction, which is more for us geeks than the OP :-)

Fortran does not formally support interoperability with C++ via that
route, and it isn't absolutely clear what C++ means by C linkage.

Both Fortran and C++ need to use a lot of created external names, and
have (moderately) advanced memory management and some error handling;
it isn't unlikely that those will conflict.  It is also unclear
exactly which Fortran interoperability features will map to C enough
to interoperate with the ways that C++ maps its equivalent to C.
I raised this at WG5 and got the answer that I expected (i.e. that
Fortran does not formally support interoperability with C++).

The latter is easier - 7.5 [dcl.link] p.3 says "Every implementation
shall provide for linkage to functions written in the C programming
language".  So far, so good.  But there are some potential semantic
discrepancies that are swept under the carpet by the C++ standard,
such as IEEE 754 'support'.  Anyone who relies on those and uses
Fortran/C++ interoperability had better prepare to be surprised!

I don't know of a clear and complete specification of how much of
this the GCC suite supports, but should be happy to cooperate with
any attempt to write one.  If there IS one, I apologise, and should
appreciate a reference!

>Additionally, I want to point out that without using Bind(C) there might 
>be additional issues - and differences between compilers; in particular, 
>the way, "float"/"REAL" values and complex-number-returning functions 
>are implemented, differs among compilers.

Yes.  Very much so.  In particular, complex-number-returning functions
are a minefield even with BIND(C) or between C++ and C and are hopeless
without it or 'extern "C"' :-(


Regards,
Nick Maclaren.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-01-11  9:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-11  8:40 need help on gfortran and C++ Mojtaba Seifi
2012-01-11  8:57 ` Tobias Burnus
2012-01-11 12:53   ` N.M. Maclaren
2012-01-11  9:37 ` Paul Richard Thomas
2012-01-11 12:20 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).