testC.cpp:
#include <iostream>
using namespace std;
extern "C"
{
void fr1_(int*,int *);
int ff1_(int *);
}
int main()
{
int n=10,nSq,nCube;
fr1_(&n, &nSq);
cout << "The square is:"<< nSq << "\n";
nCube=ff1_(&n);
cout << "The Cube is:"<< nCube << "\n";
return 0;
}
bak.c:
#include <stdio.h>
extern void fr1_(int*,int *);
extern int ff1_(int *);
int main()
{
int n=10,nSq,nCube;
fr1_(&n, &nSq);
printf( "The square is: %d\n", nSq);
nCube=ff1_(&n);
printf("The Cube is: %d\n", nCube);
return 0;
}
testF.f
SUBROUTINE FR1(N,M)
M=N*N
RETURN
END
INTEGER FUNCTION FF1(N)
FF1=N*N*N
RETURN
END
编译:
g77 -c testF.f
gcc -c bak.c
g++ -c testC.cpp
g++ -o testplus testC.o testF.o
gcc -o test bak.o testF.o
两个可执行文件testplus和test得到的结果是一样的。同样也可以写成Makefile简化编译过程,例如:
CXX=g++
F77=g77
OBJS=testF.o testC.o
testplus: $(OBJS)
$(CXX) -o testplus $(OBJS)
.cpp.o:
$(CXX) -c $<
.f.o:
$(F77) -c $<
.PHONY: clean
clean:
-rm testplus *.o
没有评论:
发表评论