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
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
>
> 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
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
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
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
Cheers,
phycho_dad
Ο/Η Joost έγραψε:
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
Cheers,
psycho_dad
Ο/Η Joost έγραψε:
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 ~
$
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 ~
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
$ 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