Linker errors with Fortran to C library - /usr/lib

2019-02-24 22:28发布

问题:

So I'm having a little trouble with the fortran to C library. Now, before I get into the problem, I can tell you that I cannot use g2c as some forum sites have suggested.

Now, to the problem. When I try to compile a very large project, I get the following:

[from the makefile...] g++ -L [~200 dirs] -l [~200 libs] -lf2c  
/usr/lib/libf2c.so: undefined reference to 'MAIN__'  
collect2: ld returned 1 exit status  
make: *** [all] Error 1

Now, I have checked my /usr/lib directory and have come up with the following:

$ locate libf2c
/usr/lib/libf2c.so
/usr/lib/libf2c.so.0
/usr/lib/libf2c.so.0.22

So at the very least, it is there. I used the google, which told me that entering...

sudo rm /usr/lib/libf2c.so && sudo ln -s /usr/lib/libf2c.a /usr/lib/libf2c.so

...would help. Something about the oldness of the library meaning that gcc doesn't link to it right anymore, and tries to find a main method where there isn't one. So I did it. However, after I linked to the .a library, g++ now can't find the library at all.

g++ -L [~200 dirs] -l [~200 libs] -lf2c 
/usr/bin/ld: cannot find -lf2c
collect2: ld returned 1 exit status
make: *** [all] Error 1

So in effect I've gone backwards. Anyone have any ideas as to where I should go from here?

EDIT: So I think I figured it out. I followed this to reinstall the libs. Now it magically does not complain that the fortran main doesn't exist. Thanks for your help guys.

回答1:

You should have paid closer attention to the first error message. The linker is telling you that no symbol MAIN__ can be found, which is what f2c is expected that the fortran main will be compiled to in your C or C++ code somewhere. The f2c library itself was working just fine.

The solution to the original error would be to make sure the correct entry point is defined in your code. It might be that by using C++ to compile, you have name mangling problems and the correct entry point for the f2c library doesn't exist.

Now by messing around with the f2c libraries, you have totally broken your f2c installation. Reinstall it and start again...



回答2:

Well,

sudo rm /usr/lib/libf2c.so && sudo ln -s /usr/lib/libf2c.a /usr/lib/libf2c.so

will delete /usr/lib/libf2c.so, hence the new error you're getting.

So as a minimum you'll have to reinstall that library and link to the new version, and then see where you are.



回答3:

I know it's an old thread, but It was helpful for me when I faced the same problem.

I solved it by applying the commands:

sudo rm /usr/lib/libf2c.so && sudo ln -s /usr/lib/libf2c.a /usr/lib/libf2c.so

as the OP did. I found the same trick here:

http://byeworld.blogspot.com/2009/01/libf2cso-undefined-reference-to-main.html

What it does is in fact is to delete the dynamic library and create an alias to the static library. The reason why it didn't work for the OP was that he/she didn't have the static library installed. That was clear from the output of the LOCATE command.

I hope this helps someone else.