Calling F90 (compiled with g95) from c

53 views
Skip to first unread message

psycho_dad

unread,
Jan 31, 2007, 10:41:04 AM1/31/07
to gg95
Hi all,
I have a problem calling F90 (compiled with g95) from a program
written in C.

Specifically, there are some subroutines (doing numerics) and I want
to call them from C. When I use a fortran program as a driver (so as
to call the subroutines etc) everythink works fine. However, when I
use a C program to call the subroutines and link with gcc I get lots
of errors like:

subroutines.o:subroutines.f90:(.text+0x5c): undefined reference to
`__g95_temp_a
lloc'
subroutines.o:subroutines.f90:(.text+0x38f): undefined reference to
`__g95_temp_
free'
subroutines.o:subroutines.f90:(.text+0x6c1): undefined reference to
`__g95_get_i
oparm'
subroutines.o:subroutines.f90:(.text+0x6c7): undefined reference to
`__g95_filen
ame'

I am working on the cygwin enviroment with the latest version of g95
and gcc (v3.44). Sorry if the question is naive but I am not very good
at C.
Any pointers/advice are welcome.

psycho_dad

Joost

unread,
Jan 31, 2007, 1:59:04 PM1/31/07
to gg95

> However, when I
> use a C program to call the subroutines and link with gcc I get lots
> of errors like:
>
> subroutines.o:subroutines.f90:(.text+0x5c): undefined reference to
> `__g95_temp_a
> lloc'
> subroutines.o:subroutines.f90:(.text+0x38f): undefined reference to

you might want to read:

http://www.g95.org/docs.html#interface

and

http://ftp.g95.org/G95Manual.pdf

the particular problem you mention above can be solved by linking also
the fortran runtime library (which is something like: g95-install/lib/
gcc-lib/x86_64-unknown-linux-gnu/4.0.3/libf95.a)

Joost

psycho_dad

unread,
Feb 1, 2007, 5:36:57 AM2/1/07
to gg95
Hi again,

>
> the particular problem you mention above can be solved by linking also
> the fortran runtime library (which is something like: g95-install/lib/
> gcc-lib/x86_64-unknown-linux-gnu/4.0.3/libf95.a)
>

I linked directly to libf95.a but now I got this error:

C:/cygwin/usr/local/lib/gcc-lib/i686-pc-mingw32/4.0.3/libf95.a(main.o):
(.text+0x
32): undefined reference to `_MAIN_'
collect2: ld returned 1 exit status

The references:

http://www.g95.org/docs.html#interface
http://ftp.g95.org/G95Manual.pdf

mention that I should call: g95_runtime_start()/g95_runtime_stop(),
but are a bit vague on the details. An example on its use would
help...

Cheers,
psycho_dad

gsal

unread,
Feb 1, 2007, 9:35:11 AM2/1/07
to gg95

I am just starting to experiment with this too, but only calling C
from fortran. In order to avoid problems I am doing at least a couple
of things:

using the -fno-underscoring flag when compiling the fortran, and
naming all my C functions in lower case.

Also, as far as the "undefined reference" problem goes, that sometimes
just has to do with the order in which you list the libraries at
compile/link time.

Hope this helps.

gsal

Joost

unread,
Feb 1, 2007, 1:38:57 PM2/1/07
to gg95

On Feb 1, 3:35 pm, "gsal" <salger...@gmail.com> wrote:
> I am just starting to experiment with this too, but only calling C
> from fortran. In order to avoid problems I am doing at least a couple
> of things

the one thing one should really do in this case is to learn (in a
couple of hours) the Fortran2003 ISO_C_BINDING stuff. It is relatively
easy, standard/portable, and will soon be supported by all major
compiler (while g95 and a couple of others already have it now).

Joost

Joost

unread,
Feb 1, 2007, 1:47:50 PM2/1/07
to gg95

> I linked directly to libf95.a but now I got this error:
> C:/cygwin/usr/local/lib/gcc-lib/i686-pc-mingw32/4.0.3/libf95.a(main.o):
> (.text+0x
> 32): undefined reference to `_MAIN_'
> collect2: ld returned 1 exit status

not sure if this is mingw specific. An example that works here (and
you could try out) is:

~/g95> cat fortran.f90
SUBROUTINE dostuff()
INTEGER :: I,J
I=1 ; J=1
write(6,*) I+J
END SUBROUTINE
~/g95> cat c.c
int main(int argc, char* argv[])
{
g95_runtime_start(argc,argv);
dostuff_();
g95_runtime_stop();
return 0;
}
~/g95> g95 -c fortran.f90
~/g95> gcc c.c fortran.o -Lg95-install/lib/gcc-lib/x86_64-unknown-
linux-gnu/4.0.3/ -lf95
~/g95> ./a.out
2

Cheers,

Joost

psycho_dad

unread,
Feb 2, 2007, 4:54:25 AM2/2/07
to gg95
Thanks for your help. I really appreciate it.

Cheers,
phycho_dad

Ο/Η Joost έγραψε:

psycho_dad

unread,
Feb 2, 2007, 5:10:49 PM2/2/07
to gg95
Hi again,
I tried the example and it works fine but when I compile (note that I
work in the cygwin enviroment) with the options:
-mno-cygwin -mwindows
while there are no errors, the executable does not work. All I tried
the option -Wall to check for any strange warnings but in vain.

The full set of commands is:

g95 -c -mno-cygwin -mwindows fortran.f90
gcc -mno-cygwin -mwindows c.c fortran.o -LC:/cygwin/usr/local/lib/gcc-
lib/i686-pc-mingw32/4.0.3 -lf95 -o test

Is it a problem with mingw?

Cheers,
psycho_dad

Joost

unread,
Feb 3, 2007, 3:06:11 AM2/3/07
to gg95
It is likely something cygwin related, but you'll need to post the
error message that you get trying to execute the binary. 'The
executable doesn't work' is a bit vague.

Joost

psycho_dad

unread,
Feb 3, 2007, 5:59:37 AM2/3/07
to gg95
Actually, that's exactly the problem! It doesn't work. When I run it,
it produces nothing (no results, no errors) but it doesn't hung
either!

Cheers,
psycho_dad

Ο/Η Joost έγραψε:

Doug

unread,
Feb 3, 2007, 9:16:27 AM2/3/07
to gg95
It seems to work ok for me.

I also tried using the options -mno-cygwin and -mwindows with the same
results.

Oh, I noticed you have linked i686-pc-mingw32 instead of the Cygwin
library. That will not work, of course.

Doug

dougc@trueblue ~
$ cat > fortran.f90


SUBROUTINE dostuff()
INTEGER :: I,J
I=1 ; J=1
write(6,*) I+J
END SUBROUTINE

dougc@trueblue ~
$ cat > c.c


int main(int argc, char* argv[])
{
g95_runtime_start(argc,argv);
dostuff_();
g95_runtime_stop();
return 0;
}

dougc@trueblue ~
$ g95 -c fortran.f90

dougc@trueblue ~
$ gcc c.c fortran.o -L/usr/local/lib/gcc-lib/i686-pc-cygwin/4.1.1 -
lf95

dougc@trueblue ~
$ ./a
2

dougc@trueblue ~
$

Doug

unread,
Feb 3, 2007, 9:42:29 AM2/3/07
to gg95

On Feb 3, 9:16 am, "Doug" <t...@sentex.net> wrote:
> It seems to work ok for me.
>
> I also tried using the options -mno-cygwin and -mwindows with the same
> results.
>
> Oh, I noticed you have linked i686-pc-mingw32 instead of the Cygwin
> library. That will not work, of course.

Well, that was incorrect, it should have worked. Sorry. But when I
tried, I get some errors. So there seems to be a problem with the
MinGW libraries.

Doug

dougc@trueblue ~
$ g95 -c -mno-cygwin -mwindows fortran.f90

dougc@trueblue ~
$ gcc c.c fortran.o -L/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1 -
lf95
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x207):
undefined reference to `__stricmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x22d):
undefined reference to `__stricmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x5b8):
undefined reference to `__stricmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x713):
undefined reference to `__strnicmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x852):
undefined reference to `___p__environ'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x8b0):
undefined reference to `___p__environ'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(environ.o):(.text
+0x900):
undefined reference to `___p__environ'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(unit.o):(.text
+0x38c): und
efined reference to `__stricmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(unit.o):(.text
+0x3df): und
efined reference to `__stricmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(string.o):(.text
+0x20d): u
ndefined reference to `__strnicmp'
/usr/local/lib/gcc-lib/i686-pc-mingw32/4.1.1/libf95.a(namelist.o):
(.text+0x8d):
undefined reference to `__stricmp'


collect2: ld returned 1 exit status

dougc@trueblue ~

Doug

unread,
Feb 3, 2007, 10:06:19 AM2/3/07
to gg95
This example also failed in MinGW with the default gcc-3.4.5, but it
works with the version of gcc included with gfortran.
In a Msys shell:

dougc@TRUEBLUE ~
$ g95 -c fortran.f90

dougc@TRUEBLUE ~
$ /mingw/gfortran/bin/gcc c.c fortran.o -L/mingw/lib/gcc-lib/i686-pc-
mingw32/4.
1.1 -lf95

dougc@TRUEBLUE ~
$ a
2


Doug

psycho_dad

unread,
Feb 3, 2007, 1:31:15 PM2/3/07
to gg95
The thing is that for my project the options "-mno-cygwin -mwindows"
must be present during the linking. Hence the linking to the mingw32
version of libf95.a
So the full set of commands that I (need to) use are:

$ g95 -c -mno-cygwin -mwindows fortran.f90

$ gcc -mno-cygwin -mwindows c.c fortran.o -LC:/cygwin/usr/local/lib/
gcc-lib/i686-pc-mingw32/4.0.3 -lf95 -o test

and the result is:
$ ./test

$ (here should be the result but I get nothing, neither errors nor a
result... )


Cheers,
psycho_dad

Reply all
Reply to author
Forward
0 new messages