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

how gcc8 process strong symbol and weak symbol for .c and .cpp?

$
0
0

I see these linking rules somewhere (including CSAPP), saying that:

  1. When there is multiple strong symbols, linking failed;
  2. When there is strong symbol and weak symbol with the same name, linker choose the strong one;
  3. When there is only multiple weak symbols, linker choose one randomly;

Supposing these rules are true, I did this:

// main.cpp#include <stdio.h>int gvar;int main(){    printf("shared var is %d\n",gvar);    return 0;}// aux.cppint gvar = 111;
gcc -o test main.cpp aux.cpp/usr/bin/ld: /tmp/ccQq1mwo.o:(.data+0x0): multiple definition of `gvar'; /tmp/ccgMcACl.o:(.bss+0x0): first defined herecollect2: error: ld returned 1 exit status

Why this error occur?

And I found that if I change "main.cpp" and "aux.cpp" to "main.c" and "aux.c", this error just disappears! Why?

By the way, my gcc version is 8.4.0.


Viewing all articles
Browse latest Browse all 22018

Trending Articles



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