Re: C++ and Fortran - Help linking files
Re: C++ and Fortran - Help linking files
- Subject: Re: C++ and Fortran - Help linking files
- From: Roland King <email@hidden>
- Date: Sat, 12 Nov 2016 09:34:38 +0800
>
> Undefined symbols for architecture x86_64:
> "_FF1", referenced from:
> _main in main.o
> "_FR1", referenced from:
> _main in main.o
> ld: symbol(s) not found for architecture x86_64
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
>
> Adding underscores doesn’t help. It just adds the underscores to error message. If the subroutine and functions names don’t match the editor complains.
>
> (BTW I havdn’t found where to add the -v)
>
> I’ve looked everywhere I can think of without success. I was thinking it was a fortran problem but it now looks like a linker issue.
>
> It looks like a linker problem. From some reading it might need to use gfortran or gcc to link the c++ and fortran object files??
>
> Any suggestions?
>
> Thanks, David
>
Start by finding the .o files generated from gfortran and run “nm -a” on them, that will show you the symbols which are actually exported and you should see FF1 and FF2 in there, with or without underscores and possibly listed as “T”, that’s the symbol for exported text symbols.
The C compiler is using the (standard) convention that externals are exported with a leading underscore, the Fortran compiler probably isn’t, so first of all, work out what the names of the functions it is exporting are. My guess would be “FF1” and “FF2” but I really have no idea. Either way, that’s what you have to match.
Assuming that’s the case, declaring the functions in the extern “C” block as you have means the linker is looking for _FF1 and _FF2, because it expects the symbol in the .o file to be the name prepended by an underscore. If fortran allows you to name the routines _FF1 and _FF2, that would work and would be the easiest. Use _FF1 in the fortran, use FF1 in the C++.
If you can’t do that, you have to tell the compiler you know better than it does what the name of the function should be, for which you can use __asm__. Instead of
extern “C” {
void FF1( int*, int*);
}
write
extern void FF1(int*, int*) __asm__(“”FF1”);
I don’t believe that needs to go in an extern-C block as well. That should force the C++/C code to look for ‘FF1’ instead of “_FF1” when linking the code.
As to whether the fortran code respects the same ABI for argument passing as C is a whole another question ..
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden