Re: C.h to D conversion (structs)
On Tuesday, 15 March 2016 at 18:47:22 UTC, Adam D. Ruppe wrote: Like you would another D library. Now I get it! Yes, that works as expected. The problem isn't the struct itself, but the D initializer. Structs in C don't have initalizers but do in D: struct Foo { int a = 10; /* illegal in C, legal in D */ }; Note that extern(C) doesn't actually do anything on a struct, it really only applies to functions and global variables. That's where I got it wrong! D realizes this by creating a variable called _DStructName__init which has the initial value of the struct. When you make a variable of that type, it copies the value of this hidden init thing over to your new variable to initialize it. C does nothing of the sort, so the C library will not provide this - you need to link in the one from D. You might be thinking "but I don't use = anything in this struct", but there's a few places where D will put one in automatically: float and double = NaN by default char, wchar, and dchar = -1 (well, 0xff) by default And everything else =0 (or null) by default. If *everything* in a struct is initialized to 0, the __init variable is left out and memset(, struct.sizeof, 0) is called instead. But if *anything* in there is non-zero, the __init variable is created and referenced when you declare the variable (unless you =void it at the usage point). Which causes the undefined reference problem you're seeing - the usage code is trying to initialize, but the C library doesn't provide that (since it isn't a C feature) and the D library isn't linked in. Thanks a million! Now I understand why it didn't work with my implementation. [snip]
Re: Clearing associative array.
On 3/12/16 8:09 AM, Adam D. Ruppe wrote: On Saturday, 12 March 2016 at 12:59:02 UTC, ciechowoj wrote: Nice article :), thanks. But still, what about clear()? In the documentation https://dlang.org/spec/hash-map.html#properties there is written that associative arrays have clear property. I don't think that actually works... might be out of date documentation, I'm not sure. Opposite. The next version of dmd will have a clear property. Apparently, we don't have a good system to merge doc changes in concert with future master releases. Whatever gets pulled apparently gets built as the current docs. See PR: https://github.com/D-Programming-Language/dlang.org/pull/1217 -Steve
Re: Is there a sorted map?
On 3/13/16 4:33 AM, Jonathan M Davis via Digitalmars-d-learn wrote: On Sunday, March 13, 2016 02:35:27 stunaep via Digitalmars-d-learn wrote: Is there any sorted map in D? I need a map and I need to be able to get the highest key in the map. In java I would use a TreeMap and use map.lastKey(), but since associative arrays are not sorted that would be O(n). I know about RedBlackTree, but that's a set and it must be a map. The closest that we have in Phobos at the moment is RedBlackTree in std.container. Its API is geared towards sets, not maps, but you can get it to work as a map if you define the comparison functions appropriately. Red-black trees are typically used for both sets and maps, so using RedBlackTree in that manner is pretty normal from an implementation perspective, but there's no question that what we really need is a wrapper around it that provides a map API, since it's not terribly user-friendly to use the set API for a map, much as it works. But unfortunately, the container situation in Phobos has been stalled for a while. It was decided that it would get a major overhaul once allocators were added to the standard library, so they didn't get much love for quite a while, and now that we finally have std.experimental.allocator, Andrei is working on a major redesign of our container solution that's supposed to replace what we have now, but in the interim, we're stuck with what we've had for a while. An alternative would be dcollections: https://github.com/schveiguy/dcollections And in fact, the implementation of RBTree in dcollections should be nearly identical to that in std.container.RedBlackTree (as the latter is based on the former). I use the same implementation for both TreeMap and TreeSet, proving Jonathan's point (all you need is a proper wrapper). It does have a HashMap, but it doesn't look like it's been updated in a while, so I don't know quite what its state is. It was solid, and as long as it still compiles, it should be fine, but it looks like Steven hasn't made any updates to it in a while (though it's my understanding that he intends to). I haven't compiled it in a long time, or even thought about how I would update it recently. I do want to freshen it up, and I did have some ideas to make the library more modular and composable. But free time is always limited ;) -Steve
Re: C.h to D conversion (structs)
On Tuesday, 15 March 2016 at 18:04:00 UTC, Chris wrote: I'm not 100% sure what you mean with compile+link in the modules. Like you would another D library. The structs are all defined in the original (third party) C header file. It's nothing I added (in which case I would have to recompile the C library). The C structs in the C library should be visible to the linker, shouldn't they? The problem isn't the struct itself, but the D initializer. Structs in C don't have initalizers but do in D: struct Foo { int a = 10; /* illegal in C, legal in D */ }; Note that extern(C) doesn't actually do anything on a struct, it really only applies to functions and global variables. D realizes this by creating a variable called _DStructName__init which has the initial value of the struct. When you make a variable of that type, it copies the value of this hidden init thing over to your new variable to initialize it. C does nothing of the sort, so the C library will not provide this - you need to link in the one from D. You might be thinking "but I don't use = anything in this struct", but there's a few places where D will put one in automatically: float and double = NaN by default char, wchar, and dchar = -1 (well, 0xff) by default And everything else =0 (or null) by default. If *everything* in a struct is initialized to 0, the __init variable is left out and memset(, struct.sizeof, 0) is called instead. But if *anything* in there is non-zero, the __init variable is created and referenced when you declare the variable (unless you =void it at the usage point). Which causes the undefined reference problem you're seeing - the usage code is trying to initialize, but the C library doesn't provide that (since it isn't a C feature) and the D library isn't linked in. I betcha if you have any float/double or char variables in those structs and set them as =0 in the definition, you will actually be able to make this problem go away.
Re: Gdmd compiling error
On 03/15/2016 02:45 AM, Orkhan wrote: > output of the gdc is : > > root@ubuntu:/home/alikoza/Downloads/i686-pc-linux-gnu# gdc > gdc: fatal error: no input files > compilation terminated. That makes sense. It should produce an executable if you give it a .d file: gdc foo.d Since you installed gdmd as well, you can get back to the Xcom server files. Have you tried building it now? If that still fails, please open another thread because I don't think I can help you any further with that project and I don't think anybody else is following this thread. :) Ali
Re: Is there anybody who used FireBird DB?
On 03/15/2016 07:08 AM, Suliman wrote: For my regret I need way to work with FireBird. I have found only one driver for D https://github.com/jiorhub/fired Before I did not work with C-bindigs and D. So I can't understand how to use this files. Could anybody help and explain how to work with it? I'm not familiar with fired or that module (e.g. whether its complete or not) but it seems like only the function declaration that are not commented-out in this extern(C) block are made available: https://github.com/jiorhub/fired/blob/master/ibase.d#L266 For example, in order to use the following function you would have to have arguments to pass to it: ISC_STATUS isc_attach_database(ref ISC_STATUS_ARRAY, short, const ISC_SCHAR*, isc_db_handle*, short, const ISC_SCHAR*); ISC_STATUS_ARRAY arr = /* I don't know how to set it up */; short s = /* What is this 'short' for? */ ISC_SCHAR c = /* What does this argument mean? */; // etc. And then you make the call: ISC_STATUS status = isc_attach_database(arr, s, , /* etc. */); You really have to know fired and apply that knowledge to this module similar to the call above. :-/ Ali
Re: C.h to D conversion (structs)
On Tuesday, 15 March 2016 at 17:10:03 UTC, Adam D. Ruppe wrote: On Tuesday, 15 March 2016 at 16:56:00 UTC, Chris wrote: Do you mean I need to void initialize them in the C code or in D? And if in D, how would I do that, with `static this`? in D, at the usage point with =void where you declare the variable of that type. So in your code: struct C { A a = void; B b = void; } though I'm pretty sure it wouldn't matter in this specific instance because they would be all zeroes anyway... your real code probably has a char or a float in it, right? I do not recommend trying that though, it is better to actually compile+link in the modules. I'm not 100% sure what you mean with compile+link in the modules. The structs are all defined in the original (third party) C header file. It's nothing I added (in which case I would have to recompile the C library). The C structs in the C library should be visible to the linker, shouldn't they? Just as when you define: extern (C): size_t strlen(const char *str); and the linker will find it automatically. The error I get is as if the structs were not defined (or as if the lib weren't linked to).
Re: Tracing InvalidMemoryOperationError
On 03/15/2016 12:27 AM, stunaep wrote: > I need to find the source of this InvalidMemoryOperationError. I tried > loading the project in visuald but it wont break on the error. Just to make sure, you tried to break inside the constructor of InvalidMemoryOperationError, right? Right where it does super( "Invalid memory operation", file, line, next ); In any case, a common reason for that error is when you have an expression (e.g. explicit 'new', some array operations, etc.) that uses the GC inside a destructor. Ali
Re: How to list all version identifiers?
would be easy with compiler as a library... also i thought 'dmd -v -version=foo -c -o- bar.d' would show -version identifiers used on the command line but doesn't seem to On Tue, Mar 15, 2016 at 8:51 AM, Iakh via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > On Sunday, 13 March 2016 at 20:16:36 UTC, Basile B. wrote: > >> On Sunday, 13 March 2016 at 16:28:50 UTC, Iakh wrote: >> >>> On Sunday, 13 March 2016 at 15:50:47 UTC, Basile B. wrote: >>> trivial answer, let's say you have dcd-server running in the background: dcd-client -c8 <<< "version(" >>> >>> Thanks. Will try. >>> >> >> But it was a joke actually. It works but this is not very >> straightforward. And it needs a bit of post processing since the output >> you'll get is normally made for the completion menu of D editors. >> > > Looks like it shows all version identifiers listed in > https://dlang.org/spec/version.html#version > and it's not what I want. I need just actual ones under some > compiler/circumstances >
Re: Is there anybody who used FireBird DB?
On Tuesday, 15 March 2016 at 15:01:09 UTC, Kagamin wrote: The same as you would do it in C. I do not know C :( Please explain me what i should to do with this binding
Re: C.h to D conversion (structs)
On Tuesday, 15 March 2016 at 16:56:00 UTC, Chris wrote: Do you mean I need to void initialize them in the C code or in D? And if in D, how would I do that, with `static this`? in D, at the usage point with =void where you declare the variable of that type. So in your code: struct C { A a = void; B b = void; } though I'm pretty sure it wouldn't matter in this specific instance because they would be all zeroes anyway... your real code probably has a char or a float in it, right? I do not recommend trying that though, it is better to actually compile+link in the modules.
Re: C.h to D conversion (structs)
On Tuesday, 15 March 2016 at 16:44:10 UTC, Adam D. Ruppe wrote: On Tuesday, 15 March 2016 at 16:32:56 UTC, Chris wrote: The error I get is something like undefined reference to `_D3test7testmodule13A6__initZ' undefined reference to `_D3test7testmodule13B6__initZ' You still need to compile/link in the module (or in this specific case, void initialize the structs) so any little functions or initializers are present. In C, structs need to be initialized manually, but in D they are automatically set to some initial value for each field. That initial value is the referenced __init symbol and still comes out of the .o file, like a function would. Do you mean I need to void initialize them in the C code or in D? And if in D, how would I do that, with `static this`?
Re: C.h to D conversion (structs)
On Tuesday, 15 March 2016 at 16:32:56 UTC, Chris wrote: The error I get is something like undefined reference to `_D3test7testmodule13A6__initZ' undefined reference to `_D3test7testmodule13B6__initZ' You still need to compile/link in the module (or in this specific case, void initialize the structs) so any little functions or initializers are present. In C, structs need to be initialized manually, but in D they are automatically set to some initial value for each field. That initial value is the referenced __init symbol and still comes out of the .o file, like a function would.
C.h to D conversion (structs)
I've converted a C.h file to D according to this guide: http://wiki.dlang.org/Converting_C_.h_Files_to_D_Modules and examples in deimos: https://github.com/D-Programming-Deimos/ However, I get an error when trying to use a struct that uses structs. struct A { size_t i; } struct B { size_t x; } struct C { A a; B b; } The error I get is something like undefined reference to `_D3test7testmodule13A6__initZ' undefined reference to `_D3test7testmodule13B6__initZ' // The C header would look like this: typedef struct _A { size_t i; } A; typedef struct _B { size_t x; } B; typedef struct _C { A a; B b; } C; Also, what do I do with C structs that contain the following: typedef struct _A { struct _A *next; struct _A *prev; } A; Thanks.
Re: How to list all version identifiers?
On Sunday, 13 March 2016 at 20:16:36 UTC, Basile B. wrote: On Sunday, 13 March 2016 at 16:28:50 UTC, Iakh wrote: On Sunday, 13 March 2016 at 15:50:47 UTC, Basile B. wrote: trivial answer, let's say you have dcd-server running in the background: dcd-client -c8 <<< "version(" Thanks. Will try. But it was a joke actually. It works but this is not very straightforward. And it needs a bit of post processing since the output you'll get is normally made for the completion menu of D editors. Looks like it shows all version identifiers listed in https://dlang.org/spec/version.html#version and it's not what I want. I need just actual ones under some compiler/circumstances
Re: Not sure how to translate this C++ variable to D.
On 03/15/2016 07:29 AM, WhatMeWorry wrote: >> SpriteRenderer Renderer; // Although, I would name it 'renderer' > Ok. I was trying something more D like, by doing: > > SpriteRenderer Renderer = new SpriteRenderer(); That would work if the variable were const or immutable and if everything needed to execute the expression were available at compile time: const(SpriteRenderer) renderer = new SpriteRenderer(); immutable(SpriteRenderer) renderer = new SpriteRenderer(); (Note: Associative arrays literals still cannot be initialized with that syntax and arrays may have performance implications.) For mutable variables and for anything in general, module variables can be initialized in 'statit this()' and 'shared static this()' scopes: static this() { renderer = new SpriteRenderer(); } The difference is that 'static this()' is executed at runtime before any code in the module gets executed. It is like the constructor of the whole module. (You can have many disjoint 'static this()' blocks, which would all get executed.) > In large projects with 1000s of > line of code and many many modules, classes, structs, and functions; is > it probably true that most of the time the vast majority of variables > are going to be inside one of above constructs? In D, everything will be inside one of those constructs because you included "modules". :) Like many modern languages, D lacks a global scope. Although we are advised to use the global scope much less in general, it is less applicable to D. In other words, it is fine to have module variables. For example, if a module consists of a single class, there wouldn't be any difference between making a variable class-static or module-scope. (Others, please correct me if you know any differences.) > And is there a name for the variables that fall outside of the above > constructs? I think module-scope or module would work. > I see so many tiny code snippets in books and docs, that when I do look > at large dub/github projects, I'm not sure how to organize the "stuff" > that slops over the edges. That's always hard :) but "cohesion" seems to be a winning goal: program constructs should have little responsibilities. Ali
Re: Is there anybody who used FireBird DB?
The same as you would do it in C.
Re: Not sure how to translate this C++ variable to D.
On Monday, 14 March 2016 at 22:19:50 UTC, Ali Çehreli wrote: On 03/14/2016 03:14 PM, WhatMeWorry wrote: > > sprite_renderer.h -- > > class SpriteRenderer > { > ... > }; Same thing in D without the semicolon. :) > game.cpp > > #include "sprite_renderer.h" > > SpriteRenderer *Renderer; Like in Java and C#, class variables are object references in D. So, the following is sufficient: SpriteRenderer Renderer; // Although, I would name it 'renderer' However, unlike C++, that variable is thread-local, meaning that if you have more than one thread, each will have their own variable. If you really need it, in multithreaded code you may want to define it shared: shared(SpriteRenderer) renderer; But then you will have to deal with thread synchronization. > I tried taking due diligence with the documentation. There is something here: http://ddili.org/ders/d.en/class.html#ix_class.variable,%20class Ali Ok. I was trying something more D like, by doing: SpriteRenderer Renderer = new SpriteRenderer(); I believe this won't work because I'm trying to allocate memory outside of any class, structure, or function? May I ask sort of an aside question. In large projects with 1000s of line of code and many many modules, classes, structs, and functions; is it probably true that most of the time the vast majority of variables are going to be inside one of above constructs? And is there a name for the variables that fall outside of the above constructs? I see so many tiny code snippets in books and docs, that when I do look at large dub/github projects, I'm not sure how to organize the "stuff" that slops over the edges.
Is there anybody who used FireBird DB?
For my regret I need way to work with FireBird. I have found only one driver for D https://github.com/jiorhub/fired Before I did not work with C-bindigs and D. So I can't understand how to use this files. Could anybody help and explain how to work with it?
Re: Is D a good choice for embedding python/octave/julia
On Sunday, 13 March 2016 at 13:02:16 UTC, Bastien wrote: Hi, apologies for what may be a fairly obvious question to some. ## The background: I have been tasked with building software to process data output by scientific instruments for non-experts - basically with GUI, menus, easy config files (JSON or similar) - and the ability to do some serious number crunching. [...] If the other language has some C api that can be called to interpret code then you can do so from D as well. See also e.g. https://github.com/ariovistus/pyd to make this easier for python.
Re: Using tango or other static lib in static lib
On Tuesday, 15 March 2016 at 08:40:31 UTC, Jaocb Carlborg wrote: On Tuesday, 15 March 2016 at 06:54:45 UTC, Zardoz wrote: Not would be more easy to simply add a dependency to tango on dub.SDL ? I ask... Yes. Mike gave a very long explanation that can be summed up by saying: add Tango as a dependency to your dub.json/sdl. -- /Jacob Carlborg The OP said nothing about DUB and demonstrated a lack of understanding of the difference between importing and linking, a very important concept to understand.
Re: Gdmd compiling error
On Monday, 14 March 2016 at 17:08:24 UTC, Marc Schütz wrote: On Monday, 14 March 2016 at 14:46:06 UTC, Orkhan wrote: [...] What does `which gdc` print? If it says something like "which: no gdc in ...", there is a problem with the installation of GDC. Otherwise, you can use the following as a quick workaround: sudo ln -s `which gdc` /usr/local/bin/gdc The proper solution is to find out why gdmd insists on using that non-existing path instead of relying on $PATH lookup, and fix it. I am sorry for multiple message , the output of the which gdc is : root@ubuntu:/usr/local/bin# which gdc /usr/local/bin/gdc Actually it is because I created symbolic link I guess.
Re: Gdmd compiling error
On Monday, 14 March 2016 at 18:13:33 UTC, Ali Çehreli wrote: On 03/14/2016 10:08 AM, Marc Schütz wrote: > What does `which gdc` print? If it says something like "which: no gdc in > ...", there is a problem with the installation of GDC. Otherwise, you > can use the following as a quick workaround: > > sudo ln -s `which gdc` /usr/local/bin/gdc > > The proper solution is to find out why gdmd insists on using that > non-existing path instead of relying on $PATH lookup, and fix it. Apparently, gdmd expects gdc to be in the same directory as itself. Once Orkhan finds out where gdc is on the system, the following should work: $ sudo ln -s /home/acehreli/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-gdc /usr/local/bin/gdc Orkhan, replace /home/acehreli/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-gdc with the gdc binary that you have. e.g. if it's under /usr/bin, then do the following: $ sudo ln -s /usr/bin/gdc /usr/local/bin/gdc Ali P.S. I've just installed gdc after downloading it from its download site ( http://gdcproject.org/downloads ) with the following two commands: unxz the_file_you_downloaded tar xvf the_file_you_unxzipped Those commands created the following directory in my case. /home/acehreli/x86_64-pc-linux-gnu That's why my gdc is named /home/acehreli/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-gdc above. After Marc's message I created symbolic link and now it is in the usr/local/bin . please look the output : root@ubuntu:/opt/xcomm# cd /usr/local/bin root@ubuntu:/usr/local/bin# ls ddemangle dman dmd dmd.conf dumpobj gdc gdmd obj2asm rdmd You see there is gdc after the command : sudo ln -s /usr/bin/gdc /usr/local/bin/gdc still when I run command Make it returns the same error. could you please test the make file as well if you already installed the gdc and gdmd . I have put the link it is not too big , I just need the xcomm being installed . Kind Regards
Re: Gdmd compiling error
On Monday, 14 March 2016 at 17:08:24 UTC, Marc Schütz wrote: On Monday, 14 March 2016 at 14:46:06 UTC, Orkhan wrote: On Monday, 14 March 2016 at 11:11:28 UTC, Ali Çehreli wrote: On 03/14/2016 02:56 AM, Orkhan wrote: > THe output like that : > root@ubuntu:/opt/xcomm# gdmd > Can't exec "/usr/local/bin/gdc": No such file or directory > at Ok, now you need to install gdc: http://gdcproject.org/downloads In short, the project that you are trying to build depends on gdmd, which is a dmd-like interface to gdc, which needs to be installed on your system. Ali Dear Ali , Thanks for your answer and time. I have tried to install and the output was like that. I gues it is already installed in my linux. isnt it ? root@ubuntu:/opt/xcomm# sudo apt-get install gdc Reading package lists... Done Building dependency tree Reading state information... Done gdc is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 180 not upgraded. root@ubuntu:/opt/xcomm# It is very urgent for me to install the project , I hope will do with your help . Thanks. What does `which gdc` print? If it says something like "which: no gdc in ...", there is a problem with the installation of GDC. Otherwise, you can use the following as a quick workaround: sudo ln -s `which gdc` /usr/local/bin/gdc The proper solution is to find out why gdmd insists on using that non-existing path instead of relying on $PATH lookup, and fix it. output of the gdc is : root@ubuntu:/home/alikoza/Downloads/i686-pc-linux-gnu# gdc gdc: fatal error: no input files compilation terminated. the output of the - sudo ln -s `which gdc` /usr/local/bin/gdc : root@ubuntu:/home/alikoza/Downloads/i686-pc-linux-gnu# sudo ln -s `which gdc` /usr/local/bin/gdc root@ubuntu:/home/alikoza/Downloads/i686-pc-linux-gnu# if you have already installed the gdmd could you please try to compile make file which I put a download link in my previous posts. Thanks.
Re: Is D a good choice for embedding python/octave/julia
On Tuesday, 15 March 2016 at 05:56:36 UTC, Ellery Newcomer wrote: On 03/13/2016 02:36 PM, Laeeth Isharc wrote: InterpContext context = new InterpContext(); context.py_stmts(outdent(" import numpy a = numpy.eye(2, dtype='complex128') ")); context.a.to_d!(Complex!double[][] )(); nitpicking, but the outdent is unnecessary, py_stmts calls it. hm. maybe that should be documented.. I was in a hurry so just copy and pasted from the unit test... I never do that myself.
Re: Using tango or other static lib in static lib
On Tuesday, 15 March 2016 at 06:54:45 UTC, Zardoz wrote: Not would be more easy to simply add a dependency to tango on dub.SDL ? I ask... Yes. Mike gave a very long explanation that can be summed up by saying: add Tango as a dependency to your dub.json/sdl. -- /Jacob Carlborg
Tracing InvalidMemoryOperationError
I need to find the source of this InvalidMemoryOperationError. I tried loading the project in visuald but it wont break on the error. I also tried adding extern(C) void onInvalidMemoryOperationError(void*) { asm { int 3; } } building with dub build --build=debug --arch=x86_64 and then using gdb, but it says "(no debugging symbols found)" and does this (gdb) r Starting program: program.exe [New Thread 8144.0xf00] Program received signal SIGTRAP, Trace/breakpoint trap. 0x00013f7a5f99 in ?? () (gdb) where #0 0x00013f7a5f99 in ?? () Backtrace stopped: previous frame identical to this frame (corrupt stack?)
Re: Using tango or other static lib in static lib
On Sunday, 13 March 2016 at 01:08:29 UTC, Mike Parker wrote: On Sunday, 13 March 2016 at 01:06:33 UTC, Mike Parker wrote: it. Assuming both files live in the same directory, they can be compiled with this command: Somehow I deleted that line: dmd main.d something.d Not would be more easy to simply add a dependency to tango on dub.SDL ? I ask...
Re: Is D a good choice for embedding python/octave/julia
On 03/13/2016 02:36 PM, Laeeth Isharc wrote: InterpContext context = new InterpContext(); context.py_stmts(outdent(" import numpy a = numpy.eye(2, dtype='complex128') ")); context.a.to_d!(Complex!double[][] )(); nitpicking, but the outdent is unnecessary, py_stmts calls it. hm. maybe that should be documented..