1

I'm trying to use a fortran module subroutine in c and cannot get through, here is a simplified version of my problem:

I have one fortran module that contains a subroutine, and the second subroutine uses the module.

!md.f90
module myadd
implicit none
contains

subroutine add1(a) bind(c)
implicit none
integer a
a=a+1

end subroutine add1
end module myadd


!sb.f90
subroutine sq(a) bind(c)
use myadd
implicit none
integer a
call add1(a)
a=a*a
end subroutine sq

Now I want to call the sb function in c:

//main.cpp
extern "C"{ void sb(int * a); }
int main(){
  int a=2;
  sb(&a);
}

how should I link them together?

I tried something like

ifort -c md.f90 sb.f90
icc sb.o main.cpp

but it gives error

sb.o: In function sq': sb.f90:(.text+0x6): undefined reference to add1' /tmp/icc40D9n7.o: In function main': main.cpp:(.text+0x2e): undefined reference tosb'

Does anyone know how to solve the problem?

1

2 Answers 2

3
int main(void){
  int a=2;
  sb(&a);
  return 0;
}

and

module myadd
use iso_c_binding
implicit none
contains

subroutine add1(a) bind(c)
implicit none
integer (c_int),intent (inout) :: a
a=a+1

end subroutine add1
end module myadd


!sb.f90
subroutine sq(a) bind(c, name="sb")
use iso_c_binding
use myadd
implicit none
integer (c_int), intent(inout) :: a
call add1(a)
a=a*a
end subroutine sq

with

gcc -c main.c
gfortran-debug fort_subs.f90 main.o

It is easier to link with the Fortran compiler because it brings in the Fortran libraries.

Sign up to request clarification or add additional context in comments.

4 Comments

It works. Thanks a lot! Do you know how to do the same using intel compilers? I always get error with intel : main.o: In function main': main.c:(.text+0x0): multiple definition of main' for_main.o:/export/users/nbtester/efi2linux_nightly/branch-13_0/20120801_000000/libdev/frtl/src/libfor/for_main.c:(.text+0x0): first defined here /home/compilers/Intel/composer_xe_2013.0.079/compiler/lib/intel64/for_main.o: In function main': /export/users/nbtester/efi2linux_nightly/branch-13_0/20120801_000000/libdev/frtl/src/libfor/for_main.c:(.text+0x38): undefined reference to MAIN__'
@xslittlegrass, if you use ifort to link Fortran code with a C/C++ main() function, you should give it the -nofor-main option, otherwise the compiler links the for_main.o object file, which contains a main function, which in turn calls the main Fortran program (usually named MAIN under the hood).
@HristoIliev It works! Thanks. Do you what is the compiler flag for icc to ask icc to bring in the fortran library?
@xslittlegrass, I'm not aware of such option. It's probably best to use ifort to link mixed-language applications.
1

The reason for your link error is twofold:

  • you omitted the object file for the source file that holds the module from the final command line (md.o).

  • you've called sq in the fortran sb in the C++ code.

Fix those and you'll be right to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.