Recent

Author Topic: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing  (Read 3858 times)

paule32

  • Sr. Member
  • ****
  • Posts: 277
FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« on: August 24, 2024, 11:06:09 pm »
Hello,
I would compile a Delphi Project with fpc 3.0, but I fail.
This is the source:
Code: Pascal  [Select][+][-]
  1. // ---------------------------------------------------------------------------
  2. // \file       misc.pas
  3. // \author     (c) 2024 Jens Kallup - paule32
  4. // \copyright  Alle Rechte vorbehalten.
  5. // ---------------------------------------------------------------------------
  6. {$ifdef FPC}
  7.     {$mode delphi}{$H+}
  8. {$endif}
  9. unit misc;
  10.  
  11. interface
  12. uses
  13.     {$ifdef win64}
  14.     Windows,
  15.     {$endif}
  16.     {$ifdef Unix}
  17.     DynLibs,
  18.     {$endif}
  19.     SysUtils;
  20.  
  21. const DLLname = 'fpc-qt.dll';
  22. type
  23.     TMainCallback = procedure(argc: Integer; argv: Array of String);
  24.  
  25.     function InitLibrary(Callback: TMainCallback): Boolean;
  26.     function ErrorMessage(s: AnsiString): Boolean; cdecl; external dllname;
  27.  
  28. implementation
  29. uses fpcmain;
  30.  
  31. function InitLibrary(Callback: TMainCallback): Boolean;
  32. begin
  33.   result := False;
  34.   DLLHandle := LoadLibrary('fpc-qt.dll');
  35.   if DLLHandle = 0 then
  36.   begin
  37.     ErrorMessage('Error: DLL could not be loaded.');
  38.     Halt(1);
  39.   end;
  40.   try
  41.     Callback(ParamCount, ParamStr(1));
  42.   finally
  43.       FreeLibrary(DLLHandle);
  44.       ReadLn;
  45.       Halt(0);
  46.   end;
  47. end;
  48.  
  49. end.

And this is the command + output:
Code: Bash  [Select][+][-]
  1. paule32@DESKTOP-FMHDK3M:/mnt/e/Projekte/AthensProjekte/1$ fpc fpcqt.pas
  2. Free Pascal Compiler version 3.0.4+dfsg-23 [2019/11/25] for x86_64
  3. Copyright (c) 1993-2017 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling fpcqt.pas
  6. Compiling pas/appsettings.pas
  7. Compiling pas/misc.pas
  8. Compiling pas/fpcmain.pas
  9. Compiling pas/QCharClass.pas
  10. QCharClass.pas(575) Warning: Implicit uses of Variants unit
  11. Linking fpcqt
  12. /usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
  13. /usr/bin/ld.bfd: cannot find -lfpc-qt.dll
  14. /usr/bin/ld.bfd: cannot find -lfpc-qt
  15. fpcqt.pas(36,1) Error: Error while linking
  16. fpcqt.pas(36,1) Fatal: There were 1 errors compiling module, stopping
  17. Fatal: Compilation aborted
  18. Error: /usr/bin/ppcx64 returned an error exitcode
  19. paule32@DESKTOP-FMHDK3M:/mnt/e/Projekte/AthensProjekte/1$

where comes the .dll stuff ?
I expect a .so
But at current time, the settings are made for create an Application....

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 545
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #1 on: August 25, 2024, 12:38:32 am »
Did you check project>Project Options > config and target?

Aruna

  • Sr. Member
  • ****
  • Posts: 494
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #2 on: August 25, 2024, 02:40:35 am »
It looks like your project is trying to use a DLL (Dynamic Link Library) named fpc-qt.dll, which is specific to Windows. Since you're compiling on a Linux system, this DLL won't be found or used. Linux uses shared libraries with the .so extension (e.g., libfpc-qt.so). You need the Linux equivalent of fpc-qt.dll

Warning About Implicit Uses:
Code: Pascal  [Select][+][-]
  1. QCharClass.pas(575) Warning: Implicit uses of Variants unit
This warning indicates that QCharClass.pas is using features from the Variants unit without explicitly declaring it in the uses clause. This can lead to potential issues or unintended behavior.

Linker Warnings and Errors:
Code: Pascal  [Select][+][-]
  1. [/b][/i]
  2. /usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
  3. /usr/bin/ld.bfd: cannot find -lfpc-qt.dll
  4. /usr/bin/ld.bfd: cannot find -lfpc-qt
link.res contains output sections; did you forget -T?: This warning suggests that the linker found some sections in the link.res file that might need specific handling, potentially requiring the -T option for custom linker scripts.

cannot find -lfpc-qt.dll:
The linker cannot find the library fpc-qt.dll. Note that .dll files are typically used on Windows, not Linux. This suggests that the project may be configured for Windows but is being compiled on Linux. (Which it is!)

cannot find -lfpc-qt: The linker also cannot find the fpc-qt library. This could be due to a missing library file or incorrect path settings.

Ensure that the fpc-qt library is available and correctly installed on your system. If it's a Windows-specific library, you may need to adjust your project settings for Linux or use the appropriate library for Linux.
« Last Edit: August 25, 2024, 02:45:26 am by Aruna »

paule32

  • Sr. Member
  • ****
  • Posts: 277
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #3 on: August 25, 2024, 09:42:04 am »
Thank you @all for reply.

I found out - something voodoo programming ...
when I change the Line: 21 in the posted misc.pas to:

const DLLname = 'fpc-qt.dll'; { I get the lib .dll error and the lib error as shown }
const DLLname = 'fpc-qt.so';  { I get no lib .dll error, and no .so error, only that lib is not found }
const DLLname = 'fpc-qt.sos';  { I get lib error twice }

So, I assume that FPC does not ensure which it has to do, when I found:

ccnst DLLname ...
{ and: }
function ErrorMessage(s: AnsiString): Boolean; cdecl; external dllname;

{ note, the "external dllname" directive }

BUT: What I don't understand: Why is it explicit neccassary to have the fpc-qt.so/dll, when I compile a application ?
I recall the compiling a Library - when "external" is used, I need the .so/dll, too ? - funny !

cdbc

  • Hero Member
  • *****
  • Posts: 1569
    • http://www.cdbc.dk
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #4 on: August 25, 2024, 09:58:04 am »
Hi
Quote
Why is it explicit neccassary to have the fpc-qt.so/dll, when I compile a application ?
I recall the compiling a Library - when "external" is used, I need the .so/dll, too ? - funny !
Nah, not so much... When you use the 'external' declaration, the compiler  __Implicitly__  loads the library for you and checks your declarations against those it finds in the library. If they don't match, it _can't_ compile, simple as that.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

TRon

  • Hero Member
  • *****
  • Posts: 3453
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #5 on: August 25, 2024, 10:38:06 am »
@paule32:
There is no gentle way to say this (I have seen your repository).

- Please learn about the difference between static linking, dynamic linking and dynamic loading. You current implementation uses all 3 (and only 1 is valid at a time). Just pick one and stick to it (if only for the time being or as a new example project).
- start a new project (if only for testing) and use a single library header unit.
- get rid of the windows unit, there is no need for it.

for example you can read up on different linking vs loading here and here.

Have for example a look here on the wiki and don't miss out on the link to the (externally linked) tutorial (it links to a Github repo that has a PDF with several examples/instructions).

I am aware that the above instructions mentioned still do not tell anything specific about dynamic loading with Pascal but for that have a look at the package directory of the FPC source-tree where you can find examples of well known (c-)libraries and their pascal bindings (that sometimes uses dynamic loading).

A small example of loading a library can be found in the documentation here

@aruna , the following is not meant to criticize your constructive answers but to illustrate to TS what is wrong.

Linker Warnings and Errors:
Code: Pascal  [Select][+][-]
  1. [/b][/i]
  2. /usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
  3. /usr/bin/ld.bfd: cannot find -lfpc-qt.dll
  4. /usr/bin/ld.bfd: cannot find -lfpc-qt
link.res contains output sections; did you forget -T?: This warning suggests that the linker found some sections in the link.res file that might need specific handling, potentially requiring the -T option for custom linker scripts.
You might not know this but it is compiler related. The FPC compiler used has this issue and the only 2 ways to get rid of it is a) to upgrade the compiler version or b) downgrade the (external) linker.

Quote
cannot find -lfpc-qt.dll:
The linker cannot find the library fpc-qt.dll. Note that .dll files are typically used on Windows, not Linux. This suggests that the project may be configured for Windows but is being compiled on Linux. (Which it is!)

cannot find -lfpc-qt: The linker also cannot find the fpc-qt library. This could be due to a missing library file or incorrect path settings.

Ensure that the fpc-qt library is available and correctly installed on your system. If it's a Windows-specific library, you may need to adjust your project settings for Linux or use the appropriate library for Linux.
Note that the presented code explicitly declares a library name with dll in it, as well as the loadlibrary routine that explicitly loads a dll (thereby mixing two different approaches).

Besides that, on linux machine when you link the library you either need to supply the correct compiler option(s) (or compiler directives) or set the ld_library path correctly (or install the library system wide).

@paule32: This reply is not meant to discourage you in any way but to show that you got a lot of things mixed up and that is catching up with you.
« Last Edit: August 25, 2024, 11:32:54 am by TRon »
This tagline is powered by AI

paule32

  • Sr. Member
  • ****
  • Posts: 277
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #6 on: August 25, 2024, 12:26:06 pm »
@Tron:

no problem, don't thing to worrie about my self.
I thank you for your reply, and the posted links.

I am a little bit out of date with my skills and programming.
It give a idom there in Germany: "Out of the Eye's. Out of the sense."

I don't know how to write it correctly translated to English sentences.
But, I know you can understand it so that, when you do a thing not often, you lost the skill's...

TRon

  • Hero Member
  • *****
  • Posts: 3453
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #7 on: August 25, 2024, 12:46:02 pm »
@paule32:
Yeah I understand (though I have no idea what the English translation of that saying is).

And no problem. Like suggested try to start a new project using a simple example and pick a way how you want to use the library (loading, linking static or dynamic) and when you run into issue then feel free to ask.

Normally I would have made some suggestions about fixing your current project but there seems to be so many things mixed up that I do not even know where to begin (my shortcoming, not yours). Hence the suggestion to make a new project with a simple example in order to familiarize yourself with the concept.
This tagline is powered by AI

cdbc

  • Hero Member
  • *****
  • Posts: 1569
    • http://www.cdbc.dk
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #8 on: August 25, 2024, 12:52:21 pm »
Hi
Google to the rescue: "out of sight, out of mind"
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

TRon

  • Hero Member
  • *****
  • Posts: 3453
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #9 on: August 25, 2024, 01:10:08 pm »
cdbc to the rescue  :)

Indeed that is the one, thank you for that.

I was looking for something with "out of heart" but got nowhere...
This tagline is powered by AI

paule32

  • Sr. Member
  • ****
  • Posts: 277
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #10 on: August 25, 2024, 02:14:52 pm »
I get it working (the .so shared object file will be create by Makefile)
I have change the Makefile, so it is possible to create .so file.
I add a extra command on the command line interface: -Cn
this omit linkage.

then I tried to link with gcc -o test *.o -L./obj -lfpcqt.so
but then, I get the following output (take a look to: cannot find)

Code: Bash  [Select][+][-]
  1. paule32@DESKTOP-FMHDK3M:/mnt/e/Projekte/AthensProjekte/1/obj$ ls
  2. QCharClass.o    appsettings.o    dialogs.o  fpc-qt.so     fpcmain.o    fpcqt.o   misc.o    ppas.sh
  3. QCharClass.ppu  appsettings.ppu  fpc-qt.o   fpc-qtchar.o  fpcmain.ppu  link.res  misc.ppu
  4. paule32@DESKTOP-FMHDK3M:/mnt/e/Projekte/AthensProjekte/1/obj$ mv fpc-qt.so fpcqt.so
  5. paule32@DESKTOP-FMHDK3M:/mnt/e/Projekte/AthensProjekte/1/obj$ gcc -o test *.o -L./ -lfpcqt
  6. /usr/bin/ld: cannot find -lfpcqt
  7. collect2: error: ld returned 1 exit status
  8. paule32@DESKTOP-FMHDK3M:/mnt/e/Projekte/AthensProjekte/1/obj$

cdbc

  • Hero Member
  • *****
  • Posts: 1569
    • http://www.cdbc.dk
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #11 on: August 25, 2024, 02:22:59 pm »
Hi
Did you change the library's name?!? Before it was '-lfpc-qt'...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

paule32

  • Sr. Member
  • ****
  • Posts: 277
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #12 on: August 25, 2024, 02:26:15 pm »
I tried both:

-L./ -lfpc-qt
-L./ -lfpc-qt.so

and:

-L./ -lfpcqt.
-L./ -lfpcqt.so

In both (4) I fail...

TRon

  • Hero Member
  • *****
  • Posts: 3453
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #13 on: August 25, 2024, 02:31:15 pm »
@paule32:
uhm, sorry but I seem to miss what exactly it is that you are trying to accomplish with that command line. Could you explain in layman's terms what parts (objects)  is c and what parts (objects) are pascal and what you'd expect to happen ?

As far as I understood your project you created a c-library (actually cpp with a wrapper) and you want to make use of that library in Pascal. Thus FPC to either link against the library or load the library manually.

To link against the library in FPC you use linklib directive and link against the shared object or you manually load the library using the dynlibs functionality (in which case you have to obtain all the library entries manually and store them at variables so that your pascal program can invoke them using the variable name)



This tagline is powered by AI

paule32

  • Sr. Member
  • ****
  • Posts: 277
Re: FPC 3.0 on Ubuntu: 20 - DLL under Linux - very confusing
« Reply #14 on: August 25, 2024, 02:47:50 pm »
@TRon:

Like you have worked out:
The goal is it, to provide Pascal Code, that represent Qt5 classes with members, so it is not neccassary to use the procedural coding style, but have real OOP - object oriented programming.

For this, I have Pascal code, that is in the current state only an Application / Program (see: fpcqt.dpr - it is a Delphi extension under Windows, but I would like use the final result under Linux, too).

The Pascal files lay in root dir + pas directory.

The C++ parts are stored in the cpp sub directory.
This files, I use to compile the .dll/.so which help me to create "unmangled" function (because with C++ is it possible to create "mangled", and "unmangled" function names with  extern "C".
So, I can hijack the Qt5 Libraries (and any other libs) with the function name:

extern "C" void foo() {
  auto * foo = new fooclass();
  ...
}

Then, I can the "exported" function from the dll in FPC by "external" or LoadLibrary ...

Under Windows, I use mingw64 (msys2), and there, it work all done.

 

TinyPortal © 2005-2018