November 25, 2011
On Fri, 25 Nov 2011 15:55:15 +0100, Max Samukha <maxter@spambox.com> wrote:

> On 11/25/2011 02:27 PM, Andrei Alexandrescu wrote:
>> On 11/25/11 2:13 AM, Jude Young wrote:
>>> Is there an easy way to turn D style (string[] args) into C style?
>>
>> Hm, I expected this would work:
>>
>> extern(C) int __argc;
>> extern(C) char** __argv;
>>
>> The symbols do exist at least on OSX (no linker error) but they are both
>> zero, so apparently druntime doesn't initialize them.
>>
>>
>> Andrei
>>
>
> If you want a reference to an externally defined variable, you need another "extern" and (if the variable is non-TLS) - __gshared:
>
> __gshared extern extern(C) int __argc;

It also took me terribly long to find out that one for getting environ.
We could probably get rid of setErrno and getErrno which add extra C sources
to druntime.

>
>
>
>
>
November 25, 2011
On 11/25/2011 09:15 AM, Martin Nowak wrote:
> On Fri, 25 Nov 2011 15:55:15 +0100, Max Samukha <maxter@spambox.com> wrote:
> 
>> On 11/25/2011 02:27 PM, Andrei Alexandrescu wrote:
>>> On 11/25/11 2:13 AM, Jude Young wrote:
>>>> Is there an easy way to turn D style (string[] args) into C
>>>> style?
>>> 
>>> Hm, I expected this would work:
>>> 
>>> extern(C) int __argc; extern(C) char** __argv;
>>> 
>>> The symbols do exist at least on OSX (no linker error) but they are both zero, so apparently druntime doesn't initialize them.
>>> 
>>> 
>>> Andrei
>>> 
>> 
>> If you want a reference to an externally defined variable, you need another "extern" and (if the variable is non-TLS) - __gshared:
>> 
>> __gshared extern extern(C) int __argc;
> 
> It also took me terribly long to find out that one for getting environ. We could probably get rid of setErrno and getErrno which add extra C sources to druntime.
> 
>> 
>> 
>> 
>> 
>> 
> 
I'll have to check these out.  In the meantime, More successes! =D

running hello_d with 5 threads
Hello, world, I am 1 of 5
Hello, world, I am 2 of 5
Hello, world, I am 5 of 5
Hello, world, I am 3 of 5
Hello, world, I am 4 of 5

running ring_d with 5 threads
Process 0 sending 15 to 1, tag 201 (5 processes in ring)
Process 0 sent to 1
Process 0 decremented value: 14
Process 0 decremented value: 13
Process 0 decremented value: 12
Process 0 decremented value: 11
Process 0 decremented value: 10
Process 0 decremented value: 9
Process 0 decremented value: 8
Process 0 decremented value: 7
Process 0 decremented value: 6
Process 0 decremented value: 5
Process 0 decremented value: 4
Process 0 decremented value: 3
Process 0 decremented value: 2
Process 0 decremented value: 1
Process 0 decremented value: 0
Process 0 exiting
Process 1 exiting
Process 2 exiting
Process 3 exiting
Process 4 exiting

running connectivity_d with 25 threads
Connectivity test on 25 processes PASSED.

All of the basic examples included now work.
I just need to find more examples and other things so that I can make
sure that everything else is working properly.
November 25, 2011
On 11/25/11 8:55 AM, Max Samukha wrote:
> __gshared extern extern(C) int __argc;

That doesn't link.

Andrei
November 25, 2011
On 11/25/2011 05:27 PM, Andrei Alexandrescu wrote:
> On 11/25/11 8:55 AM, Max Samukha wrote:
>> __gshared extern extern(C) int __argc;
>
> That doesn't link.
>
> Andrei

It links correctly on windows for me and linux doesn't have the variables defined. Are you sure they are not windows-specific?
November 25, 2011
On 2011-11-25 13:27, Andrei Alexandrescu wrote:
> On 11/25/11 2:13 AM, Jude Young wrote:
>> Is there an easy way to turn D style (string[] args) into C style?
>
> Hm, I expected this would work:
>
> extern(C) int __argc;
> extern(C) char** __argv;
>
> The symbols do exist at least on OSX (no linker error) but they are both
> zero, so apparently druntime doesn't initialize them.
>
>
> Andrei

As I wrote in another message in this thread, use these:

Mac OS X:

extern (C) char*** _NSGetEnviron();

Posix:

extern (C) extern char** environ;

-- 
/Jacob Carlborg
November 25, 2011
On 11/25/11 10:00 AM, Max Samukha wrote:
> On 11/25/2011 05:27 PM, Andrei Alexandrescu wrote:
>> On 11/25/11 8:55 AM, Max Samukha wrote:
>>> __gshared extern extern(C) int __argc;
>>
>> That doesn't link.
>>
>> Andrei
>
> It links correctly on windows for me and linux doesn't have the
> variables defined. Are you sure they are not windows-specific?

As I wrote, on OSX the definitions do link so they exist.

Andrei
November 25, 2011
On 11/25/2011 06:52 PM, Andrei Alexandrescu wrote:
>
> As I wrote, on OSX the definitions do link so they exist.
>
> Andrei

Are you defining the variables or referencing them?

extern(C) int x; means definition and of course it will link because it is you who defines the variable.

extern extern(C) int x; means declaration and won't link if there is no definition somewhere else.

I know that standard libs in MinGW gcc, DMC and MSVC do define those variables. I doubt they exist in non-MinGW gcc, including the one used on OSX.



November 25, 2011
There's an edge-case here to be aware of, if you don't reference the symbol there won't be linker errors (at least with optlink, and this makes sense when you think about it):

// no linker errors
extern extern(C) int x;
void main() { }

// linker errors
extern extern(C) int x;
void main() { int z = x; }
November 25, 2011
On Fri, 25 Nov 2011 08:22:34 +0100, Martin Nowak <dawg@dawgfoto.de> wrote:

> After going astray while doing C++ class bindings using smart_ptrs and ugly
> mixins I think I found a really neat way.
> It consists of a C++ header that defines a d_new template function.
> It will allocate it's types from the D GC while hooking the C++ type's
> destructor to a D finalizer. It even allows for nested scanning of C++
> classes and allocating other data, e.g. arrays.
>
> To use C++ class from D one only needs to define one factory function and
> add the class declaration to a D source file and link in the d_new module.
>
> Currently uses boost and offers no constructor arguments, due to the dreaded
> forwarding problem.
>
> https://gist.github.com/1391734
>
> martin

It should actually be also possible to fake the complete C++ calling conventions
using extern(C) functions. Then a mangler can be library implemented and allowing almost full access.

proof of concept
https://gist.github.com/1394145

martin
November 25, 2011
On 11/25/11 11:30 AM, Max Samukha wrote:
> On 11/25/2011 06:52 PM, Andrei Alexandrescu wrote:
>>
>> As I wrote, on OSX the definitions do link so they exist.
>>
>> Andrei
>
> Are you defining the variables or referencing them?
>
> extern(C) int x; means definition and of course it will link because it
> is you who defines the variable.
>
> extern extern(C) int x; means declaration and won't link if there is no
> definition somewhere else.
>
> I know that standard libs in MinGW gcc, DMC and MSVC do define those
> variables. I doubt they exist in non-MinGW gcc, including the one used
> on OSX.

Oh, I see. Thanks for enlightening me. So apparently I defined those variables...

Andrei