Fw: Linker erotisi

Giorgos Keramidas keramidi at otenet.gr
Mon Apr 30 14:04:01 EEST 2001


% To arxiko mail to exo skisei sto editing.
% Athlio formatting.
% Mallon apla delete tou aksize etsi opws htan, ki oxi apanthsh.

On Mon, Apr 30, 2001 at 01:05:20PM +0300, mikean1 linux greek - security wrote:
> 
> 
> Kalimera! exo tin exis erotisi gia ton GNU linker exo auta ta 2 c programata:
>
>     /* subfct.c */
>     int subfct (void)
>     {
>         printf ("message from subroutine version 1 \n") ;
>         return 0 ;
>     } /* end version 1 */
>
> kai
>
>     /* "main file" basic.c */
>     /* multiple defined subroutines test of compiler result */
>
>     #include <stddef.h>
>     #include <stdio.h>
>    
>     int subfct (void);
>     int main(void)
>     {
>         printf ("message from main part \n") ;
>         subfct () ;
>         return 0 ;
>     } /* end main */
>
>     int subfct (void)
>     {
>         printf ("message from subroutine version 2 \n") ;
>         return 0 ;
>     } /* end version 2 */
>
> kano loipon gcc -c subfct.c
> kai ftiaxno to subfct.o
>
> meta pao na kano link to basic.c kai to subfct.o
> gcc -o a.out basic.c subfct.o
>
> fysika o linker petaei error:
>
>     subfct.o: In function `subfct': subfct.o(.text+0x0): multiple definition of `subfct'
>     /tmp/ccyW6bjr.o(.text+0x24): first defined here
>      collect2: ld returned 1 exit status
>
> Yparxei kapoios tropos na po ston compiler na min xrisimopoihsei tin
> subroutina subfct pou brisketa sto basic.c alla tin routina apo to .o
> file i genika apo kapoia alli library.????

Nai fusika.  H lush einai aplh.
*Den* tha ftiajeis duo sumbola me to idio onoma se glossa opws h C [1].

To problhma den einai me to linker, alla me thn glossa pou einai to source.
O C-compiler sou otan brei duo object me to idio onoma, sto *.o arxeio
ejodou grafei akribos auto.  Duo object me to idio onoma.  O linker den
prepei na brei *pote* kati tetoio otan milame gia functions.

An uparxei hdh mia tetoia function, sto susthma kai esu thes na ftiajeis
kapoia pou kanei diaforetiko (version 2, sto paradeigma sou) tha prepei na
dialejeis ena monadiko onoma gia thn dikh sou routina.  Gia paradeigma,
an thes na kaneis override thn syslog(3) function, pou to prototype ths
einai to ejhs:

    void syslog (int priority, char *format, ...);

Den mporeis na kaneis dikia sou routina pou na legetai syslog().  Tha
prepei na tin onomaseis alliws.  my_syslog() ? new_syslog() ? Den exo
pragmatika idea, alla tha prepei na einai diaforetiko to onoma.

H allh lush einai, na exeis ontws duo arxeia pou exoun mia routina me to
idio onoma defined, opws fainetai parakato, alla na kaneis link mono me to
ena kathe fora.

    % cat koko.c

    int function (int arg)
    {
	printf("function: arg = %d\n", arg);
    }

    % cat lala.c

    int function (int arg)
    {
    	printf("function: arg = %d\n", arg == 0);
    }

    % cat main.c

    extern int function ();

    int main (void)
    {
    	function(0);
    }

Opote kaneis link me otidhpote theleis to main.c kai *ena* apo ta duo alla
arxeia, opws p.x.:

    % cc main.c koko.c
    % ./aout

*Kai* ta duo arxeia (koko.c || lala.c) den mporeis na ta kaneis link me to
main.c -- opote an auto thes na kaneis apla ksexase to :P

--giorgos

[1] Se glosses opws h C++ tha deis oti mporeis na exeis duo functions se
    diaforetika arxeia me to idio onoma kai na paiksei, alla auto einai
    kati pou douleuei diaforetika.  Kathe function se C++ 'metonomazetai'
    apo ton g++ se kati pou exei plhroforia toso gia to poio antikeimeno
    anhkei h function, oso gia to poia einai, kai poia arguments pairnei.

    Auth einai mia allh istoria omws, giati o linker blepei to `mangled'
    opws to leei onoma, *afou* ginei metonomasia.  Pali o linker blepei ena
    monadiko onoma gia kathe function.




More information about the Linux-greek-users mailing list