[英]Trying to link my C++ executable with Fortran library (Cygwin environment)
All my fortran sources compiled fine with 所有我的fortran来源编译得很好
gfortran -g -c fortran_source.f
and archived in a single library called "mylibrary.a" In there, there exists a function of interest called "myfunction" 并存档在一个名为“mylibrary.a”的库中。在那里,存在一个名为“myfunction”的兴趣函数
In my C++ file, I have: 在我的C ++文件中,我有:
extern "C" void myfunction_(/* all pointers */);
int main(){
cerr << "Mark 1" << endl;
myfunction_(/* all pointers or address_of my variables */);
cerr << "Mark 2" << endl;
}
I compile my c++ executable, linking the library with 我编译我的c ++可执行文件,链接库
g++ mainfile.cpp -L./ -lmylibrary -lgfortran
No errors or warnings... 没有错误或警告......
However, when I run my program it hangs at the first point where myfunction is called (prints "Mark1" but not "Mark 2")... 但是,当我运行我的程序时,它会在调用myfunction的第一个点挂起(打印“Mark1”而不是“Mark 2”)...
Note that this program builds and runs correctly on a Linux machine with ifort (linking -lifcore). 请注意,此程序在具有ifort(链接-lifcore)的Linux计算机上正确构建和运行。
Thank you very much! 非常感谢你!
You need to name your library libMyLibrary.a and put it in your current directory then you can link it using 您需要将库命名为libMyLibrary.a并将其放在当前目录中,然后您可以使用它来链接它
g++ mainfile.cpp -L. -lMyLibrary
or 要么
g++ mainfile.cpp ./libMyLibrary.a
You can put the library somewhere else. 您可以将库放在其他位置。 In the first case you'd change the
-L.
在第一种情况下,您将更改
-L.
to -L/path/to/the/lib
, in the second ./libMyLibrary.a
to /path/to/the/lib/libMyLibrary.a
到
-L/path/to/the/lib
,在第二个./libMyLibrary.a
到/path/to/the/lib/libMyLibrary.a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.