This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Help with linking and symbol resolution
- From: Andrew Haley <aph at redhat dot com>
- To: Lakshay Garg <lakshaygarg1996+gcc at gmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Sat, 28 Jan 2017 14:43:27 +0000
- Subject: Re: Help with linking and symbol resolution
- Authentication-results: sourceware.org; auth=none
- References: <CAJq3jyziSS4ScBbz5b8szCyAdFHnUfkL0f=BxgZggQHyzzhgYw@mail.gmail.com>
On 28/01/17 11:35, Lakshay Garg wrote:
> /* f1.c */
> #include <stdio.h>
>
> double c;
> int main() {
> c = 1;
> printf("sizeof(c) = %lu\n", sizeof(c));
> return 0;
> }
>
> /* f2.c */
> int c = 0;
>
> I compile the program using gcc f1.c f2.c. From what I have learnt, I
> believe that the output of the file should be sizeof(c) = 4 since the
> declaration of c in f1.c is weak and would be resolved to the symbol c
> in file f2.c which is strong.
I do this:
$ gcc f1.c f2.c -Wall
/usr/local/bin/ld: Warning: alignment 4 of symbol `c' in f2.o is smaller than 8 in f1.o
/usr/local/bin/ld: Warning: size of symbol `c' changed from 8 in f1.o to 4 in f2.o
You should always use -Wall when gcc does anything unexpected.
Hint: make sure you declare the type of c in every source file. Feel free
to use a header file to do this.
Andrew.