* * *

Author Topic: x64 Error while linking  (Read 462 times)

mewpokemon

  • New member
  • *
  • Posts: 22
x64 Error while linking
« on: August 20, 2017, 07:11:12 pm »
Heyas!

I'm having real trouble sorting out a problem.

I have a project I'm trying to port to Linux x64.  It works fine on Windows both 32 and 64 bits however it fails to compile with the error "Error while linking" on Linux.  It also works correctly on Linux x86.

I think the problem is that I am trying to link a C library.  I have compiled it using the switches -shared, -std=c++11 and -fPIC.  I am linking it in Pascal with {$LINKLIB ...}.

I've tried a few different combinations (compiling to .a, .so...  using $LINK instead of $LINKLIB...) but what I have settled on works in x86 but not x64.  I've also tried fixing the paths using various options (-Fl on the compiler, -rpath and -L on the linker).

It must be the compilation of the C library??  The problem is that g++ (its C wrapped) won't compile it without -fPIC and -std=c++11.  I'm assuming that -shared is required (it works in x86).

Can anyone help?


Daniel.

Phil

  • Hero Member
  • *****
  • Posts: 2278
Re: x64 Error while linking
« Reply #1 on: August 20, 2017, 07:28:05 pm »
It must be the compilation of the C library??  The problem is that g++ (its C wrapped) won't compile it without -fPIC and -std=c++11.  I'm assuming that -shared is required (it works in x86).

What's the exact error message?

Maybe you don't have a 64-bit version of the dynamic library?

mewpokemon

  • New member
  • *
  • Posts: 22
Re: x64 Error while linking
« Reply #2 on: August 20, 2017, 07:36:26 pm »
The exact error message is "Error: Error while linking".

I compiled the library myself.

Phil

  • Hero Member
  • *****
  • Posts: 2278
Re: x64 Error while linking
« Reply #3 on: August 20, 2017, 07:59:46 pm »
The exact error message is "Error: Error while linking".

I compiled the library myself.

If it can't find the library, ld will also output "/usr/bin/ld: cannot find -lnameoflib"


mewpokemon

  • New member
  • *
  • Posts: 22
Re: x64 Error while linking
« Reply #4 on: August 20, 2017, 08:38:51 pm »
Whoops...  I seem to have gotten the wrong area for this post.

I've proven its not the library now.

I'll keep trying to track down the cause and post again in the right area.

Sorry!


Daniel.

Phil

  • Hero Member
  • *****
  • Posts: 2278
Re: x64 Error while linking
« Reply #5 on: August 20, 2017, 08:42:46 pm »
Whoops...  I seem to have gotten the wrong area for this post.

I've proven its not the library now.

I'll keep trying to track down the cause and post again in the right area.

I wouldn't worry about that.

I just tested a 64-bit C++ dynamic library with a C interface. The library functions are declared like this:

function GDALVersionInfo_(param1: charPtr): charPtr; cdecl; external 'gdal';

libgdal.so is installed in /usr/lib

Compiled test app like this:

fpc -S2h testgdal.pas

That's all. Pretty straightforward.


dicepd

  • Jr. Member
  • **
  • Posts: 56
Re: x64 Error while linking
« Reply #6 on: August 20, 2017, 09:06:05 pm »
I had a similar problem see
https://forum.lazarus.freepascal.org/index.php/topic,37916.msg256217.html#msg256217

problem may lay with ld versions.

mewpokemon

  • New member
  • *
  • Posts: 22
Re: x64 Error while linking
« Reply #7 on: August 20, 2017, 09:10:32 pm »
I have the program working now...

I had another library that I'd forgotten about completely.  I am using OpenAL as well.  Unfortunately, I was getting no message to tell me that the library is missing.  I'm using the OpenAL units that come with FPC.

I wonder why I wasn't being told it couldn't find OpenAL?

Anyway, its solved now.  Thanks!

Oh!  BTW, I don't think you should specify _any_ calling convention on routines imported from 64 bit libraries.  The calling convention used is _not_ cdecl but ABI-something-or-other.  The cdecl is just being ignored.  gcc/g++ will tell you that the cdecl attributes are being ignored if you try to specify them.


Daniel.

Phil

  • Hero Member
  • *****
  • Posts: 2278
Re: x64 Error while linking
« Reply #8 on: August 20, 2017, 09:10:37 pm »
I had a similar problem see
https://forum.lazarus.freepascal.org/index.php/topic,37916.msg256217.html#msg256217

problem may lay with ld versions.

I don't see what Linux he's using.

On Ubuntu 14.04, ld is at version 2.24. That's what I used.

Phil

  • Hero Member
  • *****
  • Posts: 2278
Re: x64 Error while linking
« Reply #9 on: August 20, 2017, 09:16:10 pm »
Oh!  BTW, I don't think you should specify _any_ calling convention on routines imported from 64 bit libraries.  The calling convention used is _not_ cdecl but ABI-something-or-other.  The cdecl is just being ignored.  gcc/g++ will tell you that the cdecl attributes are being ignored if you try to specify them.

In this case, the cdecl is telling FPC how to make the call.

For example, with GDAL, some of the functions use cdecl on all platforms, other functions use cdecl on Unix and stdcall on Windows. You have to get that right conditionally for it to work across all platforms.

(And, just to make things even more fun, on Win32 the stdcall functions use decorated names.)


mewpokemon

  • New member
  • *
  • Posts: 22
Re: x64 Error while linking
« Reply #10 on: August 20, 2017, 11:16:45 pm »
Umm...  You missed what I said.  On x64 the calling convention is _not_ cdecl.  It is ABI-whatever and the cdecl you used is being ignored there.

Yes, there is name decoration on stdcall but only by the MSVC compiler on Windows, it seems.  So I do have three different declaration blocks for my imported routines in the code.


Daniel.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus