我可以使用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) 我正在尝试在 INSEL 中创建一个用户定义的块,它需要 C++ 进行编程,并使用 gfortran 链接它。
我的程序中有以下代码
// Setting seed for random number generators
unsigned seed = static_cast<int> (std::chrono::system_clock::now().time_since_epoch().count());
Run Code Online (Sandbox Code Playgroud)
设置为我的随机数生成器的种子。当我使用 g++ (gcc v.5.1.0) 编译它时,它没有显示错误或警告。我的编译命令是
g++ -O0 -Wall -c -g3 -std=c++14 -fmessage-length=0 $(sourcesC)
Run Code Online (Sandbox Code Playgroud)
sourcecC 有我所有的 .cpp 程序。但是当我尝试使用 gfortran 将其链接到:
gfortran -shared -o C:\***\inselUB.dll -Wall -L./ -linselTools $(objects)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
C:***\resources/../src/constants.h:54: 对 `std::chrono::_V2::system_clock::now()' 的未定义引用
collect2.exe:错误:ld 返回 1 退出状态 makefile:11:目标“inselUB”的配方失败
make: *** [inselUB] 错误 1
我只是这种类型接口的初学者。可能是一个简单的错误。有人能给我建议一个解决方案吗?
操作系统:Win 7