www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible to suppress standard lib and dlang symbols in dylib

reply David <dangermouseb forwarding.cc> writes:
I thought it would be fun to convert some old C++/C quant utils 
to D. I'm starting with a simple library that I call from vba in 
Excel on macos:


module xlutils;

import core.stdc.string : strlen, strcpy;
//import std.conv : to;
//import std.string : toStringz;
import core.stdc.stdlib : malloc, free;

extern (C) double addDD_D(double a, double b) {return a + b;}
...


which results in:

nm -gU libxlutils.dylib
0000000000003f10 S __D7xlutils12__ModuleInfoZ
0000000000003e44 T _addDD_D
0000000000003ebc T _addrC
0000000000003e84 T _freeP
0000000000003e9c T _strcpyCC
0000000000003e6c T _strlenC_L


If I import `to` and `toStringz` I get more symbols than will fit 
in my shell output. E.g.

00000000000318bc T 
__D2rt5minfo11ModuleGroup9sortCtorsMFAyaZ8findDepsMFmPmZ9__lambda5MFNbNiQBjZv
0000000000031534 T 
__D2rt5minfo11ModuleGroup9sortCtorsMFAyaZ8findDepsMFmPmZb
0000000000030dcc T __D2rt5minfo11ModuleGroup9sortCtorsMFAyaZv
0000000000031db4 T __D2rt5minfo11ModuleGroup9sortCtorsMFZv
0000000000048ef0 S __D2rt5minfo12__ModuleInfoZ
00000000000327e4 T 
__D2rt5minfo16rt_moduleTlsCtorUZ14__foreachbody1MFKSQBx19sections_osx_x86_6412SectionGroupZi
...
00000000000183ac T _thread_resumeAll
0000000000018660 T _thread_scanAll
0000000000018480 T _thread_scanAllType
0000000000019658 T _thread_suspendAll


Is there a way of not exposing the symbols that aren't mine? - I 
only need a simple C interface.

Thx

David
Mar 11
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote:
 I thought it would be fun to convert some old C++/C quant utils 
 to D. I'm starting with a simple library that I call from vba 
 in Excel on macos:

 [...]
*trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d
Mar 11
parent reply David <dangermouseb forwarding.cc> writes:
On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote:
 I thought it would be fun to convert some old C++/C quant 
 utils to D. I'm starting with a simple library that I call 
 from vba in Excel on macos:

 [...]
*trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d
 Btw, have you looked at excel-d?
Of course - but unless I've missed something I don't believe it works on macos.
Mar 11
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote:
 On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote:
 I thought it would be fun to convert some old C++/C quant 
 utils to D. I'm starting with a simple library that I call 
 from vba in Excel on macos:

 [...]
*trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d
 Btw, have you looked at excel-d?
Of course - but unless I've missed something I don't believe it works on macos.
Hmm, I'm not sure. Have you tried?
Mar 11
parent reply David <dangermouseb forwarding.cc> writes:
On Thursday, 11 March 2021 at 14:49:32 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote:
 On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote:
 I thought it would be fun to convert some old C++/C quant 
 utils to D. I'm starting with a simple library that I call 
 from vba in Excel on macos:

 [...]
*trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d
 Btw, have you looked at excel-d?
Of course - but unless I've missed something I don't believe it works on macos.
Hmm, I'm not sure. Have you tried?
Nope the documentation implies it only works on windows so I've ruled it out. But the thing I'm trying to solve is suppression of symbols in the library.
Mar 11
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 11 March 2021 at 17:00:06 UTC, David wrote:
 On Thursday, 11 March 2021 at 14:49:32 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote:
 On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote:
 I thought it would be fun to convert some old C++/C quant 
 utils to D. I'm starting with a simple library that I call 
 from vba in Excel on macos:

 [...]
*trigger warning* "vba in Excel on macos" ⚠️ Btw, have you looked at excel-d? https://code.dlang.org/packages/excel-d
 Btw, have you looked at excel-d?
Of course - but unless I've missed something I don't believe it works on macos.
Hmm, I'm not sure. Have you tried?
Nope the documentation implies it only works on windows so I've ruled it out. But the thing I'm trying to solve is suppression of symbols in the library.
I see, there might be something similar to .def? https://docs.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files?redirectedfrom=MSDN&view=msvc-160 Or I guess you could strip it? https://www.linux.org/docs/man1/strip.html But I guess you already thought of that and want to not even generate them in the first place?
Mar 11
parent reply David <dangermouseb forwarding.cc> writes:
On Thursday, 11 March 2021 at 18:35:37 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 17:00:06 UTC, David wrote:
 On Thursday, 11 March 2021 at 14:49:32 UTC, Imperatorn wrote:
 On Thursday, 11 March 2021 at 10:29:55 UTC, David wrote:
 On Thursday, 11 March 2021 at 08:40:58 UTC, Imperatorn wrote:
 [...]
 [...]
Of course - but unless I've missed something I don't believe it works on macos.
Hmm, I'm not sure. Have you tried?
Nope the documentation implies it only works on windows so I've ruled it out. But the thing I'm trying to solve is suppression of symbols in the library.
I see, there might be something similar to .def? https://docs.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files?redirectedfrom=MSDN&view=msvc-160 Or I guess you could strip it? https://www.linux.org/docs/man1/strip.html But I guess you already thought of that and want to not even generate them in the first place?
I was wondering if there was something similar to def - my next attempt will be a combination of ldc and export. I wasn't aware that object files could be manipulated like the strip manual page - thx for the heads up.
Mar 11
parent reply David Skluzacek <david.skluzacek gmail.com> writes:
On Thursday, 11 March 2021 at 22:10:04 UTC, David wrote:
 I wasn't aware that object files could be manipulated like the 
 strip manual page - thx for the heads up.
With the caveats that the linked post is almost 14 years old, I can't try this command myself, and the ldc solution is probably preferable if you can get it to work - if you want to compile with dmd and use the strip command, this might help you (the OP answered his own question at the bottom): https://forum.juce.com/t/how-to-build-dylib-exporting-only-wanted-symbols/2180 He suggests doing: strip -u -r -s FILE_CONTAINING_EXPORTS_LIST MY_DYLIB.dylib
Mar 13
parent David <dangermouseb forwarding.cc> writes:
On Sunday, 14 March 2021 at 01:38:23 UTC, David Skluzacek wrote:
 On Thursday, 11 March 2021 at 22:10:04 UTC, David wrote:
 I wasn't aware that object files could be manipulated like the 
 strip manual page - thx for the heads up.
With the caveats that the linked post is almost 14 years old, I can't try this command myself, and the ldc solution is probably preferable if you can get it to work - if you want to compile with dmd and use the strip command, this might help you (the OP answered his own question at the bottom): https://forum.juce.com/t/how-to-build-dylib-exporting-only-wanted-symbols/2180 He suggests doing: strip -u -r -s FILE_CONTAINING_EXPORTS_LIST MY_DYLIB.dylib
It looks straight forward that way - thx
Mar 14
prev sibling next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
Pipe it to grep should work

| grep -v "__D2"
Mar 11
parent David <dangermouseb forwarding.cc> writes:
On Thursday, 11 March 2021 at 14:35:45 UTC, rikki cattermole 
wrote:
 Pipe it to grep should work

 | grep -v "__D2"
Thanks - though I'm trying to suppress the symbols being generated in the library. A colleague says it can be done in ldc but not dmd. I'll think I'll try that out.
Mar 11
prev sibling parent reply Guillaume Piolat <first.name spam.org> writes:
On Thursday, 11 March 2021 at 08:34:48 UTC, David wrote:
 I thought it would be fun to convert some old C++/C quant utils 
 to D. I'm starting with a simple library that I call from vba 
 in Excel on macos:


 module xlutils;

 import core.stdc.string : strlen, strcpy;
 //import std.conv : to;
 //import std.string : toStringz;
 import core.stdc.stdlib : malloc, free;

 extern (C) double addDD_D(double a, double b) {return a + b;}
 ...



 Is there a way of not exposing the symbols that aren't mine? - 
 I only need a simple C interface.

 Thx

 David
Create a exports.lst file with: _addDD_D as the only line there. Build with: "lflags-osx-ldc": [ "-exported_symbols_list", "exports.lst", "-dead_strip" ],
Mar 11
parent reply David <dangermouseb forwarding.cc> writes:
On Friday, 12 March 2021 at 00:12:37 UTC, Guillaume Piolat wrote:
 Create a exports.lst file with:

 _addDD_D


 as the only line there.
 Build with:

 "lflags-osx-ldc": [ "-exported_symbols_list", "exports.lst", 
 "-dead_strip" ],
Thx that's really helpful. I've hit a snag with LDC. After serveral hours of searching and reading I think it's coming down to sandboxing and needing to deploy libraries in a place that excel can find them. The library DMD creates just references /usr/lib/libSystem.B.dylib and Excel can load my dylib. LDC on the other hand additionally references: rpath/libphobos2-ldc-shared.95.dylib (compatibility version 95.0.0, current version 2.0.95) rpath/libdruntime-ldc-shared.95.dylib (compatibility version 95.0.0, current version 2.0.95) So Excel complains that it can't load my library - presumably because libphobos2 and libdruntime are not in the sandbox.ly Can anyone (either tell me how to fix this) or give me pointers pls? (I'm current reading up on DYLD_LIBRARY_PATH (shouldn't use that), sandboxing on Mac, and rpaths) I suppose a solution that allows the additional libraries to be put in a subdirectory of my library would be idea as then I can deploy that within the sandbox. Thx for the help
Mar 13
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 13 March 2021 at 23:41:28 UTC, David wrote:
 So Excel complains that it can't load my library - presumably 
 because libphobos2 and libdruntime are not in the sandbox.ly
You *might* be able to compile with --link-defaultlib-shared=false to use the static phobos+druntime... but with shared libs it prefers shared and might not allow this. Probably worth a try. Otherwise I'd try to just add the phobos so to your sandbox somehow. or the rpath set to current directory and putting them in your correct directory may also work. I don't know how that works on Mac but on Linux it is passing... `-L-rpath -L.` or something like that when compiling... I can't find my notes on this but something like that.
Mar 13
parent reply David <dangermouseb forwarding.cc> writes:
On Sunday, 14 March 2021 at 00:00:59 UTC, Adam D. Ruppe wrote:
 On Saturday, 13 March 2021 at 23:41:28 UTC, David wrote:
 So Excel complains that it can't load my library - presumably 
 because libphobos2 and libdruntime are not in the sandbox.ly
You *might* be able to compile with --link-defaultlib-shared=false to use the static phobos+druntime... but with shared libs it prefers shared and might not allow this. Probably worth a try. Otherwise I'd try to just add the phobos so to your sandbox somehow. or the rpath set to current directory and putting them in your correct directory may also work. I don't know how that works on Mac but on Linux it is passing... `-L-rpath -L.` or something like that when compiling... I can't find my notes on this but something like that.
I think figuring out how to add phobos to the sandbox is probably the best option as I'm sure it won't be the last dylib I need to load. Anyone else done this? Pointers welcome.
Mar 14
parent reply Guillaume Piolat <first.name spam.org> writes:
On Sunday, 14 March 2021 at 11:33:00 UTC, David wrote:
 Anyone else done this? Pointers welcome.
Sorry for delay. Just add "dflags-osx-ldc": ["-static"],
Mar 17
parent reply Jacob Carlborg <doob me.com> writes:
On Wednesday, 17 March 2021 at 13:52:48 UTC, Guillaume Piolat 
wrote:
 On Sunday, 14 March 2021 at 11:33:00 UTC, David wrote:
 Anyone else done this? Pointers welcome.
Sorry for delay. Just add "dflags-osx-ldc": ["-static"],
macOS doesn't support static linking. -- /Jacob Carlborg
Mar 17
next sibling parent Jacob Carlborg <doob me.com> writes:
On Wednesday, 17 March 2021 at 15:16:36 UTC, Jacob Carlborg wrote:

 macOS doesn't support static linking.
The proper way to solve this is to bundle the dynamic libraries with the application. If it's a GUI application it can be located in the application bundle. It seems like David already figured this out [1]. [1] https://forum.dlang.org/post/wsvlwdgzswxprtfjzqik forum.dlang.org -- /Jacob Carlborg
Mar 17
prev sibling parent David <dangermouseb forwarding.cc> writes:
On Wednesday, 17 March 2021 at 15:16:36 UTC, Jacob Carlborg wrote:
 On Wednesday, 17 March 2021 at 13:52:48 UTC, Guillaume Piolat 
 wrote:
 On Sunday, 14 March 2021 at 11:33:00 UTC, David wrote:
 Anyone else done this? Pointers welcome.
Sorry for delay. Just add "dflags-osx-ldc": ["-static"],
macOS doesn't support static linking. -- /Jacob Carlborg
Ah that's really useful to know, thanks - this is my first bit of macOS dev (other than high level stuff like R, Python, kdb etc) and I'm having to learn more about internals than I really care to - ho hum.
Mar 17