相关疑难解决方法(0)

1511
推荐指数
11
解决办法
72万
查看次数

使用gcc链接fortran和c ++二进制文件

我可以使用gcc分别使用g ++或gfortran在c和c ++之间或c和fortran之间进行调用.但是如果我尝试在c ++和fortran之间进行过程调用,那么在使用g ++或gfortran编译时会出现错误,因为它们都不知道其他所需的库.如何链接使用c ++和fortran编写的源代码的项目?

$ cat print_hi.f90
subroutine print_hi() bind(C)
  implicit none
  write(*,*) "Hello from Fortran."
end subroutine print_hi

$ cat main.cpp
#include <iostream>

extern "C" void print_hi(void);

using namespace std;

int main() {
  print_hi();
  cout << "Hello from C++" << endl;
  return 0;
}
$ gfortran -c print_hi.f90 -o print_hi.o
$ g++ -c main.cpp -o main.o
Run Code Online (Sandbox Code Playgroud)

我尝试用g ++链接:

$ g++ main.o print_hi.o -o main
print_hi.o: In function `print_hi':
print_hi.f90:(.text+0x3f): undefined reference to `_gfortran_st_write'
Run Code Online (Sandbox Code Playgroud)

以及有关未定义引用的更多错误.

并与gfortran:

$ gfortran …
Run Code Online (Sandbox Code Playgroud)

c++ gcc fortran

37
推荐指数
1
解决办法
3万
查看次数

标签 统计

c++ ×2

c ×1

extern-c ×1

fortran ×1

gcc ×1

linkage ×1

name-mangling ×1