x64 Error while linking lazarus / 1-Linux ---------------------------------------------------------------------- mewpokemon · 2017-08-20 17:11 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 · 2017-08-20 17:28 Quote from: mewpokemon on August 20, 2017, 07:11:12 pmIt 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 · 2017-08-20 17:36 The exact error message is "Error: Error while linking". I compiled the library myself. ---------------------------------------------------------------------- Phil · 2017-08-20 17:59 Quote from: mewpokemon on August 20, 2017, 07:36:26 pmThe 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 · 2017-08-20 18:38 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 · 2017-08-20 18:42 Quote from: mewpokemon on August 20, 2017, 08:38:51 pmWhoops...  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 · 2017-08-20 19:06 I had a similar problem see https://forum.lazarus.freepascal.org/index.php/topic,37916.msg256217.h tml#msg256217 problem may lay with ld versions. ---------------------------------------------------------------------- mewpokemon · 2017-08-20 19:10 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 · 2017-08-20 19:10 Quote from: dicepd on August 20, 2017, 09:06:05 pmI had a similar problem see https://forum.lazarus.freepascal.org/index.php/topic,37916.msg256217.h tml#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 · 2017-08-20 19:16 Quote from: mewpokemon on August 20, 2017, 09:10:32 pmOh!  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 · 2017-08-20 21:16 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.