Can I create static c callable library?
Steven Schveighoffer
schveiguy at gmail.com
Thu Sep 27 23:53:50 UTC 2018
On 9/27/18 8:16 AM, Atila Neves wrote:
> On Tuesday, 25 September 2018 at 14:13:50 UTC, Jacob Carlborg wrote:
>> On Tuesday, 25 September 2018 at 12:05:21 UTC, Jonathan M Davis wrote:
>>
>>> If you use -betterC, then it's trivial, because your D program is
>>> restricted to extern(C) functions and features which don't require
>>> druntime. It can also be done without -betterC (and thus with
>>> druntime), but it gets to be _way_ more of a pain, because it
>>> requires that you manually initialize druntime - either by forcing
>>> whatever is using your "C" library to call a specific function to
>>> initialize druntime before using any of its normal functions or by
>>> having every function in the library check whether druntime has been
>>> initialized yet and initialize it if it hasn't been before it does
>>> whatever it's supposed to do.
>>
>> Shouldn't it be possible to use a C initialization function, i.e.
>> pragma(crt_constructor) to initialize druntime? Then it only needs to
>> be initialized once and it's not required to check if it's initialized
>> all the time.
>>
>> --
>> /Jacob Carlborg
>
> Even easier, compile this C file and add the resulting object file to
> your (now mostly) D static library:
>
> -----------------------
> extern int rt_init(void);
> extern int rt_term(void);
>
> __attribute__((__constructor__)) void dinit(void) {
> rt_init();
> }
> __attribute__((__destructor__)) void dterm(void) {
> rt_term();
> }
> -----------------------
>
> The C runtime will initialise the D runtime for you.
>
I will point out that this is EXACTLY what pragma(crt_constructor) does.
And my comments still aren't answered -- I'm not sure whether this works
correctly or not, as we don't test initializing druntime before C main runs.
-Steve
More information about the Digitalmars-d-learn
mailing list