Chris
Chris

Reputation: 33

Trying to link my C++ executable with Fortran library (Cygwin environment)

All my fortran sources compiled fine with

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"

In my C++ file, I have:

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

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")...

Note that this program builds and runs correctly on a Linux machine with ifort (linking -lifcore).

Thank you very much!

Upvotes: 3

Views: 1351

Answers (1)

Michael Anderson
Michael Anderson

Reputation: 73520

You need to name your library libMyLibrary.a and put it in your current directory then you can link it using

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. to -L/path/to/the/lib, in the second ./libMyLibrary.a to /path/to/the/lib/libMyLibrary.a

Upvotes: 2

Related Questions