D - struct sort
- Brian Hammond <Brian_member pathlink.com> Apr 21 2004
- Brian Hammond <d brianhammond.com> Apr 21 2004
- "Carlos Santander B." <carlos8294 msn.com> Apr 24 2004
- Brian Hammond <d brianhammond.com> Apr 24 2004
- Brian Hammond <d brianhammond.com> Apr 24 2004
- "Carlos Santander B." <carlos8294 msn.com> Apr 25 2004
From http://www.prowiki.org/wiki4d/wiki.cgi?HelmutLeitner/StructSort Attempting to compile the example with the latest compiler version fails. $ dmd|head -1 Digital Mars D Compiler v0.82 $ dmd ssort.d gcc ssort.o -o ssort -lphobos -lpthread -lm ssort.o(.gnu.linkonce.t_Dmain+0x1f): In function `_Dmain': : undefined reference to `_init_19TypeInfo_S5ssort2As' collect2: ld returned 1 exit status --- errorlevel 256 This is on Linux: $ uname -a Linux overdone 2.4.22-1.2115.nptl #1 Wed Oct 29 15:42:51 EST 2003 i686 i686 i386 GNU/Linux Thanks
Apr 21 2004
Can anyone try compiling this?
/*
** ssort.d (C) Helmut Leitner 2003
** struct sort example
*/
struct As // a struct
{
int x;
char [] name;
int cmp(As *p2)
{
int a = x;
int b = p2.x;
printf("compare %d %d\n",a,b);
return a < b ? -1 : a > b ? 1 : 0;
}
}
class TypeInfo_S8ssort_As : TypeInfo
{
int tsize()
{
return As.size;
}
int compare_name(void *p1, void *p2)
{
char [] a = ((As *) p1).name;
char [] b = ((As *) p2).name;
// printf("compare %d %d\n",a,b);
return a < b ? -1 : a > b ? 1 : 0;
}
int compare_x(void *p1, void *p2)
{
int a = ((As *) p1).x;
int b = ((As *) p2).x;
// printf("compare %d %d\n",a,b);
return a < b ? -1 : a > b ? 1 : 0;
}
int compare(void *p1, void *p2)
{
return compare_name(p1,p2);
}
}
void AsPrint(As a) {
printf("x=%d name=%.*s\n",a.x,a.name);
}
As sasar[3]= [ // static As array
{ 2, "Gamma" },
{ 1, "Beta" },
{ 3, "Alpha" }
];
void AsArrayPrintTitle(As [] array, char [] title)
{
if(title.length) {
printf("%.*s",title);
}
for(int i=0; i<array.length; i++)
{
AsPrint(array[i]);
}
}
int main()
{
AsArrayPrintTitle(sasar,"\nbefore sorting:\n");
sasar.sort;
AsArrayPrintTitle(sasar,"\nafter sorting:\n");
return (0);
}
In article <c669k9$1est$1 digitaldaemon.com>, Brian Hammond says...
From http://www.prowiki.org/wiki4d/wiki.cgi?HelmutLeitner/StructSort
Attempting to compile the example with the latest compiler version fails.
$ dmd|head -1
Digital Mars D Compiler v0.82
$ dmd ssort.d
gcc ssort.o -o ssort -lphobos -lpthread -lm
ssort.o(.gnu.linkonce.t_Dmain+0x1f): In function `_Dmain':
: undefined reference to `_init_19TypeInfo_S5ssort2As'
collect2: ld returned 1 exit status
--- errorlevel 256
This is on Linux:
$ uname -a
Linux overdone 2.4.22-1.2115.nptl #1 Wed Oct 29 15:42:51 EST 2003 i686 i686 i386
GNU/Linux
Thanks
Apr 21 2004
"Brian Hammond" <d brianhammond.com> wrote in message
news:c66o06$2amo$1 digitaldaemon.com
| Can anyone try compiling this?
|
| /*
| ** ssort.d (C) Helmut Leitner 2003
| ** struct sort example
| */
|
| struct As // a struct
| {
| int x;
| char [] name;
|
| int cmp(As *p2)
| {
| int a = x;
| int b = p2.x;
| printf("compare %d %d\n",a,b);
| return a < b ? -1 : a > b ? 1 : 0;
| }
| }
|
| class TypeInfo_S8ssort_As : TypeInfo
| {
| int tsize()
| {
| return As.size;
| }
|
| int compare_name(void *p1, void *p2)
| {
| char [] a = ((As *) p1).name;
| char [] b = ((As *) p2).name;
| // printf("compare %d %d\n",a,b);
| return a < b ? -1 : a > b ? 1 : 0;
| }
|
| int compare_x(void *p1, void *p2)
| {
| int a = ((As *) p1).x;
| int b = ((As *) p2).x;
| // printf("compare %d %d\n",a,b);
| return a < b ? -1 : a > b ? 1 : 0;
| }
|
| int compare(void *p1, void *p2)
| {
| return compare_name(p1,p2);
| }
| }
|
| void AsPrint(As a) {
| printf("x=%d name=%.*s\n",a.x,a.name);
| }
|
| As sasar[3]= [ // static As array
| { 2, "Gamma" },
| { 1, "Beta" },
| { 3, "Alpha" }
| ];
|
| void AsArrayPrintTitle(As [] array, char [] title)
| {
| if(title.length) {
| printf("%.*s",title);
| }
| for(int i=0; i<array.length; i++)
| {
| AsPrint(array[i]);
| }
| }
|
| int main()
| {
| AsArrayPrintTitle(sasar,"\nbefore sorting:\n");
| sasar.sort;
| AsArrayPrintTitle(sasar,"\nafter sorting:\n");
|
| return (0);
| }
|
I didn't try this on Linux, but I think name mangling works the same in both
compilers.
The key is this line:
class TypeInfo_S8ssort_As : TypeInfo
Instead of what follows to the "S" (8ssort_As) you have to write: the length
of your module's name, your module's name, the length of the struct name,
and the struct name.
So, if this file is in module foo, that line would be replaced by:
class TypeInfo_S3foo2As : TypeInfo
-----------------------
Carlos Santander Bernal
Apr 24 2004
In article <c6eo0s$297r$1 digitaldaemon.com>, Carlos Santander B. says..."Brian Hammond" <d brianhammond.com> wrote in message news:c66o06$2amo$1 digitaldaemon.com | Can anyone try compiling this? | | /* | ** ssort.d (C) Helmut Leitner 2003 | ** struct sort example | */ | | struct As // a struct | { | int x; | char [] name; | | int cmp(As *p2) | { | int a = x; | int b = p2.x; | printf("compare %d %d\n",a,b); | return a < b ? -1 : a > b ? 1 : 0; | } | } | | class TypeInfo_S8ssort_As : TypeInfo | { | int tsize() | { | return As.size; | } | | int compare_name(void *p1, void *p2) | { | char [] a = ((As *) p1).name; | char [] b = ((As *) p2).name; | // printf("compare %d %d\n",a,b); | return a < b ? -1 : a > b ? 1 : 0; | } | | int compare_x(void *p1, void *p2) | { | int a = ((As *) p1).x; | int b = ((As *) p2).x; | // printf("compare %d %d\n",a,b); | return a < b ? -1 : a > b ? 1 : 0; | } | | int compare(void *p1, void *p2) | { | return compare_name(p1,p2); | } | } | | void AsPrint(As a) { | printf("x=%d name=%.*s\n",a.x,a.name); | } | | As sasar[3]= [ // static As array | { 2, "Gamma" }, | { 1, "Beta" }, | { 3, "Alpha" } | ]; | | void AsArrayPrintTitle(As [] array, char [] title) | { | if(title.length) { | printf("%.*s",title); | } | for(int i=0; i<array.length; i++) | { | AsPrint(array[i]); | } | } | | int main() | { | AsArrayPrintTitle(sasar,"\nbefore sorting:\n"); | sasar.sort; | AsArrayPrintTitle(sasar,"\nafter sorting:\n"); | | return (0); | } | I didn't try this on Linux, but I think name mangling works the same in both compilers. The key is this line: class TypeInfo_S8ssort_As : TypeInfo Instead of what follows to the "S" (8ssort_As) you have to write: the length of your module's name, your module's name, the length of the struct name, and the struct name. So, if this file is in module foo, that line would be replaced by: class TypeInfo_S3foo2As : TypeInfo ----------------------- Carlos Santander Bernal
Apr 24 2004
[Sorry about empty message] Thanks that worked... From where did you learn about the name mangling? In article <c6eo0s$297r$1 digitaldaemon.com>, Carlos Santander B. says..."Brian Hammond" <d brianhammond.com> wrote in message news:c66o06$2amo$1 digitaldaemon.com | Can anyone try compiling this? | | /* | ** ssort.d (C) Helmut Leitner 2003 | ** struct sort example | */ | | struct As // a struct | { | int x; | char [] name; | | int cmp(As *p2) | { | int a = x; | int b = p2.x; | printf("compare %d %d\n",a,b); | return a < b ? -1 : a > b ? 1 : 0; | } | } | | class TypeInfo_S8ssort_As : TypeInfo | { | int tsize() | { | return As.size; | } | | int compare_name(void *p1, void *p2) | { | char [] a = ((As *) p1).name; | char [] b = ((As *) p2).name; | // printf("compare %d %d\n",a,b); | return a < b ? -1 : a > b ? 1 : 0; | } | | int compare_x(void *p1, void *p2) | { | int a = ((As *) p1).x; | int b = ((As *) p2).x; | // printf("compare %d %d\n",a,b); | return a < b ? -1 : a > b ? 1 : 0; | } | | int compare(void *p1, void *p2) | { | return compare_name(p1,p2); | } | } | | void AsPrint(As a) { | printf("x=%d name=%.*s\n",a.x,a.name); | } | | As sasar[3]= [ // static As array | { 2, "Gamma" }, | { 1, "Beta" }, | { 3, "Alpha" } | ]; | | void AsArrayPrintTitle(As [] array, char [] title) | { | if(title.length) { | printf("%.*s",title); | } | for(int i=0; i<array.length; i++) | { | AsPrint(array[i]); | } | } | | int main() | { | AsArrayPrintTitle(sasar,"\nbefore sorting:\n"); | sasar.sort; | AsArrayPrintTitle(sasar,"\nafter sorting:\n"); | | return (0); | } | I didn't try this on Linux, but I think name mangling works the same in both compilers. The key is this line: class TypeInfo_S8ssort_As : TypeInfo Instead of what follows to the "S" (8ssort_As) you have to write: the length of your module's name, your module's name, the length of the struct name, and the struct name. So, if this file is in module foo, that line would be replaced by: class TypeInfo_S3foo2As : TypeInfo ----------------------- Carlos Santander Bernal
Apr 24 2004
"Brian Hammond" <d brianhammond.com> wrote in message news:c6fihm$f3n$1 digitaldaemon.com | [Sorry about empty message] | | Thanks that worked... From where did you learn about the name mangling? | I wish I remembered. I guess from looking assembly output... Or maybe someone said once how it worked... I can't really remember. ----------------------- Carlos Santander Bernal
Apr 25 2004









Brian Hammond <d brianhammond.com> 