www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can D interface with Free Pascal?

reply Taylor Hillegeist <taylorh140 gmail.com> writes:
Just curious... I had a thought that perhaps since Objective C 
was a replacement for Pascal on the mac. that they might have the 
same interface. but I'm not savvy enough with fpc to figure out 
how to try it.
Jan 27
next sibling parent reply FreeSlave <freeslave93 gmail.com> writes:
On Thursday, 28 January 2016 at 04:26:26 UTC, Taylor Hillegeist 
wrote:
 Just curious... I had a thought that perhaps since Objective C 
 was a replacement for Pascal on the mac. that they might have 
 the same interface. but I'm not savvy enough with fpc to figure 
 out how to try it.
Not directly. You can declare cdecl function on Free Pascal side and call it as extern(C).
Jan 28
parent reply bearophile <bearophileHUGS lycos.com> writes:
FreeSlave:

On Thursday, 28 January 2016 at 08:15:38 UTC, FreeSlave wrote:
 Not directly. You can declare cdecl function on Free Pascal 
 side and call it as extern(C).
What about extern(Pascal)? https://dlang.org/spec/attribute.html#linkage Bye, bearophile
Jan 28
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 28 January 2016 at 19:33:22 UTC, bearophile wrote:
 FreeSlave:

 On Thursday, 28 January 2016 at 08:15:38 UTC, FreeSlave wrote:
 Not directly. You can declare cdecl function on Free Pascal 
 side and call it as extern(C).
What about extern(Pascal)? https://dlang.org/spec/attribute.html#linkage Bye, bearophile
Cool!, I did find this little gem. but still havent gotten a hello world to work. I need to figure out which libraries i need to include for fpc dependencies.
Jan 28
parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 28 January 2016 at 19:49:22 UTC, Taylor Hillegeist 
wrote:
 On Thursday, 28 January 2016 at 19:33:22 UTC, bearophile wrote:
 FreeSlave:

 On Thursday, 28 January 2016 at 08:15:38 UTC, FreeSlave wrote:
 Not directly. You can declare cdecl function on Free Pascal 
 side and call it as extern(C).
What about extern(Pascal)? https://dlang.org/spec/attribute.html#linkage Bye, bearophile
Cool!, I did find this little gem. but still havent gotten a hello world to work. I need to figure out which libraries i need to include for fpc dependencies.
AFAIK, FreePascal does not use the pascal calling convention by default. If this page [1] is correct, the default convention is 'register' and can be changed to something else (like pascal or cdecl) with a command-line switch [2]. [1] http://www.freepascal.org/docs-html/prog/progse22.html [2] http://www.freepascal.org/docs-html/prog/progsu87.html
Jan 28
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Friday, 29 January 2016 at 01:47:11 UTC, Mike Parker wrote:
 On Thursday, 28 January 2016 at 19:49:22 UTC, Taylor Hillegeist 
 wrote:
 On Thursday, 28 January 2016 at 19:33:22 UTC, bearophile wrote:
 FreeSlave:

 On Thursday, 28 January 2016 at 08:15:38 UTC, FreeSlave wrote:
 Not directly. You can declare cdecl function on Free Pascal 
 side and call it as extern(C).
What about extern(Pascal)? https://dlang.org/spec/attribute.html#linkage Bye, bearophile
Cool!, I did find this little gem. but still havent gotten a hello world to work. I need to figure out which libraries i need to include for fpc dependencies.
AFAIK, FreePascal does not use the pascal calling convention by default. If this page [1] is correct, the default convention is 'register' and can be changed to something else (like pascal or cdecl) with a command-line switch [2]. [1] http://www.freepascal.org/docs-html/prog/progse22.html [2] http://www.freepascal.org/docs-html/prog/progsu87.html
Working through a simple example. I tried the cdecl option but for some reason i can compile but when i run my Gethello it cant find the shared library in the same folder? ==================================================================== taylor taylor-NE510:~/Projects/PASCAL$ ls Gethello Gethello.d hello.pas libhello.so taylor taylor-NE510:~/Projects/PASCAL$ cat Gethello.d extern(C) void SubStr(); void main(){ SubStr(); } taylor taylor-NE510:~/Projects/PASCAL$ cat hello.pas library subs; procedure SubStr(); cdecl; begin write('hello World'); end; exports SubStr; end. taylor taylor-NE510:~/Projects/PASCAL$ nm libhello.so 0000000000003ac0 T SubStr taylor taylor-NE510:~/Projects/PASCAL$ ./Gethello ./Gethello: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory =======================================================================
Jan 29
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 30 January 2016 at 03:43:59 UTC, Taylor Hillegeist 
wrote:

 Working through a simple example. I tried the cdecl option but 
 for some reason i can compile but when i run my Gethello it 
 cant find the shared library in the same folder?
 
 taylor taylor-NE510:~/Projects/PASCAL$ nm libhello.so
 0000000000003ac0 T SubStr
 taylor taylor-NE510:~/Projects/PASCAL$ ./Gethello
 ./Gethello: error while loading shared libraries: libhello.so: 
 cannot open shared object file: No such file or directory
 
The binary's directory is not on the system search path by default on Linux. This page [1] shows three possible solutions. [1] http://www.aimlesslygoingforward.com/2014/01/19/bundling-shared-libraries-on-linux/
Jan 29
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Saturday, 30 January 2016 at 04:11:07 UTC, Mike Parker wrote:
 On Saturday, 30 January 2016 at 03:43:59 UTC, Taylor Hillegeist 
 wrote:

 Working through a simple example. I tried the cdecl option but 
 for some reason i can compile but when i run my Gethello it 
 cant find the shared library in the same folder?
 
 taylor taylor-NE510:~/Projects/PASCAL$ nm libhello.so
 0000000000003ac0 T SubStr
 taylor taylor-NE510:~/Projects/PASCAL$ ./Gethello
 ./Gethello: error while loading shared libraries: libhello.so: 
 cannot open shared object file: No such file or directory
 
The binary's directory is not on the system search path by default on Linux. This page [1] shows three possible solutions. [1] http://www.aimlesslygoingforward.com/2014/01/19/bundling-shared-libraries-on-linux/
Now I'm wishing that was the problem. Interestingly enough when i link to a C shared library it works... but also it isn't show in the needed shared library sections like below. taylor taylor-NE510:~/Projects/PASCAL$ readelf -d Gethello Dynamic section at offset 0x26dd8 contains 29 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libhello.so] 0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0] 0x0000000000000001 (NEEDED) Shared library: [librt.so.1] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x0000000000000001 (NEEDED) Shared library: [ld-linux-x86-64.so.2] 0x000000000000000f (RPATH) Library rpath: [--export-dynamic] 0x000000000000000c (INIT) 0x4017f8 0x000000000000000d (FINI) 0x424444 0x0000000000000019 (INIT_ARRAY) 0x626db0 0x000000000000001b (INIT_ARRAYSZ) 16 (bytes) 0x000000000000001a (FINI_ARRAY) 0x626dc0 0x000000000000001c (FINI_ARRAYSZ) 16 (bytes) 0x000000006ffffef5 (GNU_HASH) 0x4002d0 0x0000000000000005 (STRTAB) 0x400aa8 0x0000000000000006 (SYMTAB) 0x4002f8 0x000000000000000a (STRSZ) 1202 (bytes) 0x000000000000000b (SYMENT) 24 (bytes) 0x0000000000000015 (DEBUG) 0x0 0x0000000000000003 (PLTGOT) 0x627000 0x0000000000000002 (PLTRELSZ) 1824 (bytes) 0x0000000000000014 (PLTREL) RELA 0x0000000000000017 (JMPREL) 0x4010d8 0x0000000000000007 (RELA) 0x401090 0x0000000000000008 (RELASZ) 72 (bytes) 0x0000000000000009 (RELAENT) 24 (bytes) 0x000000006ffffffe (VERNEED) 0x401000 0x000000006fffffff (VERNEEDNUM) 4 0x000000006ffffff0 (VERSYM) 0x400f5a 0x0000000000000000 (NULL) 0x0 taylor taylor-NE510:~/Projects/PASCAL$ ./Gethello ./Gethello: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
Jan 29
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Saturday, 30 January 2016 at 04:35:29 UTC, Taylor Hillegeist 
wrote:
 On Saturday, 30 January 2016 at 04:11:07 UTC, Mike Parker wrote:
 [...]
Now I'm wishing that was the problem. Interestingly enough when i link to a C shared library it works... but also it isn't show in the needed shared library sections like below. [...]
Acctually I made multiple mistakes. 0x000000000000000f (RPATH) Library rpath:[--export-dynamic] should be [lib] Ill fix that and test again.
Jan 29
parent Taylor Hillegeist <taylorh140 gmail.com> writes:
On Saturday, 30 January 2016 at 04:49:39 UTC, Taylor Hillegeist 
wrote:
 On Saturday, 30 January 2016 at 04:35:29 UTC, Taylor Hillegeist 
 wrote:
 On Saturday, 30 January 2016 at 04:11:07 UTC, Mike Parker 
 wrote:
 [...]
Now I'm wishing that was the problem. Interestingly enough when i link to a C shared library it works... but also it isn't show in the needed shared library sections like below. [...]
Acctually I made multiple mistakes. 0x000000000000000f (RPATH) Library rpath:[--export-dynamic] should be [lib] Ill fix that and test again.
dmd Gethello.d -Llibhello.so -L"-rpath=./" made it use the CWD able to load libraries! cool!
Jan 29
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 28 January 2016 at 04:26:26 UTC, Taylor Hillegeist 
wrote:
 Just curious... I had a thought that perhaps since Objective C 
 was a replacement for Pascal on the mac. that they might have 
 the same interface. but I'm not savvy enough with fpc to figure 
 out how to try it.
As said in the other posts you can link dlls or even object files produced by a D compiler. There is the D runtime to initialize and finalize, a particular attention must be put on te calling conventions, also some stuff like strings must be passed using toStringz() or received with fromStringz() (and Pchar() in pascal)... One thing that's interesting is to develop let's say the GUI with Lazarus and use a core made in D. When you'll debug your application in Lazarus, and if an exception is raised within the object or the dll made in D, the integrated debugger will break and display the D source directly in Lazarus. The first time it happend to me, I was **totally mesmerized**... (I even made a post here: https://forum.dlang.org/thread/aeiwsnmsrchgmkbllqid forum.dlang.org "//debugger breaks here": it was the Lazarus debugger who displayed a D source). But actually it's totally logical, it's just the DWARF debug info that indicates the source name...
Feb 08