www.digitalmars.com         C & C++   DMDScript  

D - C to D struct with static array linker problem

reply Damon Gray <Damon_member pathlink.com> writes:
I'm making a simple sockets lib for D on linux to get used to D. When I compile
it with sin_zero in struct sockaddr_in as "ubyte sin_zero[8];" I get the error:

dmd testsocket.d 
gcc testsocket.o -o testsocket -lphobos -lpthread -lm 
testsocket.o: In function `_Dmain':
testsocket.o(.gnu.linkonce.t_Dmain+0x8): undefined reference to
`_init_7sockets11sockaddr_in'
collect2: ld returned 1 exit status
--- errorlevel 256

But when I use the sockaddr_in struct below (with sin_zero1 thru sin_zero8) it
compiles and runs fine. The same thing happens if I reference just sockaddr as
well. The sin_zero is used to pad sockaddr_in to the size of sockaddr which is
16 bytes.

Here is the related code to the problem:

alias ushort sa_family_t;
alias ushort in_port_t;
alias uint u_int32_t;

struct sockaddr {
sa_family_t sa_family;
byte sa_data[14];
};  

struct in_addr {
u_int32_t s_addr;
};  

struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port;
in_addr sin_addr;
ubyte sin_zero[8];
};
struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port;
in_addr sin_addr;
ubyte sin_zero1;
ubyte sin_zero2;
ubyte sin_zero3;
ubyte sin_zero4;
ubyte sin_zero5;
ubyte sin_zero6;
ubyte sin_zero7;
ubyte sin_zero8;
};


Thanks for your help.
Jan 06 2004
parent "Walter" <newshound digitalmars.com> writes:
The trouble is that the linker is looking for the initializer for
sockaddr_in. That is provided when socket.d is compiled. I suspect you are
not compiling socket.d and linking in socket.obj.

"Damon Gray" <Damon_member pathlink.com> wrote in message
news:btf7h4$1n7e$1 digitaldaemon.com...
 I'm making a simple sockets lib for D on linux to get used to D. When I
compile
 it with sin_zero in struct sockaddr_in as "ubyte sin_zero[8];" I get the
error:
 dmd testsocket.d
 gcc testsocket.o -o testsocket -lphobos -lpthread -lm
 testsocket.o: In function `_Dmain':
 testsocket.o(.gnu.linkonce.t_Dmain+0x8): undefined reference to
 `_init_7sockets11sockaddr_in'
 collect2: ld returned 1 exit status
 --- errorlevel 256

 But when I use the sockaddr_in struct below (with sin_zero1 thru
sin_zero8) it
 compiles and runs fine. The same thing happens if I reference just
sockaddr as
 well. The sin_zero is used to pad sockaddr_in to the size of sockaddr
which is
 16 bytes.

 Here is the related code to the problem:

 alias ushort sa_family_t;
 alias ushort in_port_t;
 alias uint u_int32_t;

 struct sockaddr {
 sa_family_t sa_family;
 byte sa_data[14];
 };

 struct in_addr {
 u_int32_t s_addr;
 };

 struct sockaddr_in {
 sa_family_t sin_family;
 in_port_t sin_port;
 in_addr sin_addr;
 ubyte sin_zero[8];
 };
 struct sockaddr_in {
 sa_family_t sin_family;
 in_port_t sin_port;
 in_addr sin_addr;
 ubyte sin_zero1;
 ubyte sin_zero2;
 ubyte sin_zero3;
 ubyte sin_zero4;
 ubyte sin_zero5;
 ubyte sin_zero6;
 ubyte sin_zero7;
 ubyte sin_zero8;
 };


 Thanks for your help.
May 15 2004