fortran 调用 c 没有提示出错
而c、cpp 调用fortran 编译时 如果没有添加相应的函数库 会提示错误
使用 c 使用gcc\g++ 或 icc ifort 使用 gfortran 和ifort
使用http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
中的例子
testC.cpp
#include <iostream>
using namespace std;
extern "C"
{
void fortfunc_(int *ii,float *ff);
}
main()
{
int ii=5;
float ff=5.5;
fortfunc_(&ii,&ff);
return 0;
}
testF.f
subroutine fortfunc(ii,ff)
integer ii
real*4 ff
write(6,100) ii,ff
100 format('ii=',i2,'ff=',f6.3)
return
end
编译方法
一:
ifort -c testF.f
icc -c testC.cpp
icc -o test testF.o testC.o -lifcore
如果少了 -lficore的话提示
testF.o: In function `fortfunc_':
testF.f:(.text+0x3f): undefined reference to `for_write_seq_fmt'
testF.f:(.text+0x5c): undefined reference to `for_write_seq_fmt
这样的错误。
可以用nm 查看 在安装ifort的根目录中lib中libifcore.so
找到 for_write_seq_fmt 和for_write_seq_fmt
二:
gfortran -c testF.f
g++ -c testC.cpp
g++ -o test testF.o testC.o -lgfortran
三:
ifort -c testF.f
g++ -c testC.cpp
g++ -o test testF.o testC.o -lifcore
提示
libifcore.so.5: undefined reference to `dlsym'
错误
虽然nm libifocre.so.5 是可以看到dlsym,但提示这样的错误
说明还是缺少一个含dlsysm的库。
g++ -o test testF.o testC.o -lifcore -ldl 即可
libdl.so 在 /usr/lib 中
调试
使用idb 可能会比较适合习惯了windows 调试的使用者
编译时只有加上 -g即可
icc -g -c testC.cpp
icc -c testC.cpp
/usr/include/c++/4.8/iostream(38): catastrophic error: could not open source file "bits/c++config.h"
#include <bits/c++config.h>
ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h /usr/include/c++/4.8/bits/c++config.h
/usr/include/c++/4.8/bits/c++config.h(426): catastrophic error: could not open source file "bits/os_defines.h"
#include <bits/os_defines.h>
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h /usr/include/c++/4.8/bits/os_defines.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/cpu_defines.h /usr/include/c++/4.8/bits/cpu_defines.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++locale.h /usr/include/c++/4.8/bits/c++locale.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/gthr.h /usr/include/c++/4.8/bits/gthr.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/gthr-default.h /usr/include/c++/4.8/bits/gthr-default.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/atomic_word.h /usr/include/c++/4.8/bits/atomic_word.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++allocator.h /usr/include/c++/4.8/bits/c++allocator.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/ctype_base.h /usr/include/c++/4.8/bits/ctype_base.h
sudo ln -s /usr/include/x86_64-linux-gnu/c++/4.8/bits/ctype_inline.h /usr/include/c++/4.8/bits/ctype_inline.h
icc -c testC.cpp
/usr/include/c++/4.8/ext/atomicity.h(49): error: identifier "__ATOMIC_ACQ_REL" is undefined
{ return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
查找一下 有人提到这事icc 和gcc 版本兼容性问题,安装2013版的icc
安装最新的ifort 和 icc (版本2013)
原来icc 11版的问题消失,
直接启动idb 进行调试即可
ifort -g -c testF.f
icc -g -o test testF.o testC.o