I just want to test mixed programming with C/F, but i failed.
F subroutine subf.f90
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode Integer Function Add(A,B) Bind(C,Name="Add")!//要返回的话,请用 Function
use , Intrinsic :: ISO_C_Binding
implicit none
integer , value :: A , B !/c#默认是传值,而fortran默认传址。因此要用value修饰
Add = a + b !// Add(函数名)用于返回,而不能写 return a+b
end Function Add
C main test.cpp
[C] syntaxhighlighter_viewsource syntaxhighlighter_copycode #include <stdio.h>
extern "C" int Add(int A,int B);
int main()
{
int c = Add(1,2);
printf("c=%d\n",c);
return 0;
}
makfile
[Make] syntaxhighlighter_viewsource syntaxhighlighter_copycode test: test.o subf.o
icc -O2 -o test *.o
test.o:
icc -c test.c
subf.o:
ifort -c subf.f90
ERR
[Make] syntaxhighlighter_viewsource syntaxhighlighter_copycode test.o: In function `main':
test.cpp:(.text+0x35): undefined reference to `Add(int, int)'
Makefile:5: recipe for target 'test' failed
where did it all go wrong?
|