Re: C++ and Fortran - Help linking files
Re: C++ and Fortran - Help linking files
- Subject: Re: C++ and Fortran - Help linking files
- From: David Bourne <email@hidden>
- Date: Fri, 11 Nov 2016 19:28:59 -0700
Roland
> On Nov 11, 2016, at 6:34 PM, Roland King <email@hidden> wrote:
>
>
>>
>> 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
Strangely it didn’t seem to create .o files unless they are hiding deep in a ~/Library/ folder.
> 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.
Adding the _ in the C++ code didn’t seem to help
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)
> 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++.
Interesting idea but didn’t seem to work.
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)
> 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”);
Using
extern void FR1(int*, int*) __asm__("FR1");
extern int FF1(int*) __asm__("FF1”);
got me
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)
> 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 ..
Finally, I realize I may be on a wild goose chase with regard the linker issue.
Swift is going to require the LLVM linker whereas fortran will require gfortran or gcc.
Oh well. I was hoping to put a GUI on my command line program.
I may need to go the f2c route. Convert the fortran code to C and then link it with a bridging file to Swift.
Thanks for your help, David
_______________________________________________
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