Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22113

the result of linking two strong function symbol with the same function signature use g++ and why?

$
0
0

I am confused about the linking procedure when linking two same function symbol.

point.h:

#ifndef _POINT_H_#define _POINT_H_struct dpoint_t{    /* data */    double x, y;};struct ipoint_t{    /* data */    int x, y;};#ifdef DOUBLE_POINT    typedef struct dpoint_t data;#else    typedef struct ipoint_t data;#endifstruct Point{    data p;    int idx;};/*#ifndef DOUBLE_POINT__attribute__ ((weak)) #endif*/void * get_y(struct Point &x);#endif

point.cpp:

#include "point.h"void * get_y(struct Point &pt){    int a = 1;    return &(pt.p.y);}

test.cpp:

#include <stdio.h>#include "point.h"int main(){    struct Point x;    x.p.x = 10.0;    x.p.y = 5.0;    void *p = get_y(x);    printf("double: %lf\nint: %d\n", *(double *)p, *(int *)p);    return 0;}

I get two objects by

g++ -o double_point -DDOUBLE_POINT -c point.cppg++ -o int_point -c point.cpp

and let use g++ to link them together with test.cpp

My question is:

why I can link them successfully, I mean there are 2 same symbol, why ld doesn't get error

I think if I use weak symbol on one of the functions, the linking result will always be the strong function symbol, but the result doesn't change, it always be the symbol that come first, I want to know why

my compiler:

GNU C++ version 3.4.5 20051201 (Red Hat 3.4.5-2) (x86_64-redhat-linux) compiled by GNU C version 3.4.5 20051201 (Red Hat 3.4.5-2).

GNU assembler version 2.15.92.0.2 (x86_64-redhat-linux) using BFD version 2.15.92.0.2 20040927


Viewing all articles
Browse latest Browse all 22113

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>