结合FORTRAN和C ++,链接错误 [英] Combining FORTRAN and C++, linking error
问题描述
我应该在 linux
中结合一个c ++代码和旧的 fortran
代码,其中 fortan
是主要代码.我不是这方面的专家,我尝试从简单的测试开始,但是仍然无法编译它.也许我很愚蠢,但是我找不到任何可行的例子.当链接可以通过ifort完成时,我设法编译了fortran和c(需要稍后使用intel编译器和实际的fortran代码一起使用).但是,如果我没错理解的话,使用 c ++
进行的链接必须由 c ++
编译器( g ++
)完成.
I should couple in linux
one c++ code with old fortran
code, where fortan
is the main code. Im not expert in this area and I try to start with simple test, but still I cannot compile it. Maybe I'm stupid, but I cannot find a working example anywhere. I managed to compile fortran and c, when the linking can be done by ifort (need to use intel compiler later with the actual fortran code). But If I've understood right, with c++
, the linking must be done by c++
compiler (g++
).
那我该怎么办:
我的FORTRAN测试代码"ftest.f":
My FORTRAN test code "ftest.f":
PROGRAM MAIN
IMPLICIT NONE
INTEGER I
write(*,*) "hello fortran1"
CALL ctest()
write(*,*) "hello fortran2"
END PROGRAM
和C ++代码"ctest.cpp"
And C++ code "ctest.cpp"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
extern "C" void ctest_();
void ctest_(){
int i;
// std::cout << "hello c \n";
printf("hello c\n");
}
我尝试使用以下内容进行编译:
I try to compile with the following:
ifort -c ftest.f
g++ -c ctest.cpp
g++ -ldl -lm -limf -L -l -lifcore ctest.o ftest.o
我得到一个错误:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
那么如何成功链接此程序?
So what should I do to success with linking this program?
推荐答案
您的主要(输入项)在Fortran部分中,因此解决此问题的一种方法是使用
Your main (entry) is in Fortran part, so one way to solve it is to to use ifort linker instead of g++ (that would also link ifcore automatically)
ifort ctest.o ftest.o ... -lstdc++
这篇关于结合FORTRAN和C ++,链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!