Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1057523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:52:56+00:00 2026-05-16T17:52:56+00:00

It seems uninitialized global variable is treated as weak symbol in Gcc. What is

  • 0

It seems uninitialized global variable is treated as weak symbol in Gcc. What is the reason behind this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:52:56+00:00Added an answer on May 16, 2026 at 5:52 pm

    gcc, in C mode:

    Uninitialised globals which are not declared extern are treated as "common" symbols, not weak symbols.

    Common symbols are merged at link time so that they all refer to the same storage; if more than one object attempts to initialise such a symbol, you will get a link-time error. (If they aren’t explicitly initialised anywhere, they will be placed in the BSS, i.e. initialised to 0.)

    gcc, in C++ mode:

    Not the same – it doesn’t do the common symbols thing. "Uninitialised" globals which are not declared extern are implicitly initialised to a default value (0 for simple types, or default constructor).


    In either case, a weak symbol allows an initialised symbol to be overridden by a non-weak initialised symbol of the same name at link time.


    To illustrate (concentrating on the C case here), I’ll use 4 variants of a main program, which are all the same except for the way that global is declared:

    1. main_init.c:

      #include <stdio.h>
      
      int global = 999;
      
      int main(void) { printf("%d\n", global); return 0; }
      
    2. main_uninit.c, which omits the initialisation:

      #include <stdio.h>
      
      int global;
      
      int main(void) { printf("%d\n", global); return 0; }
      
    3. main_uninit_extern.c, which adds the extern keyword:

      #include <stdio.h>
      
      extern int global;
      
      int main(void) { printf("%d\n", global); return 0; }
      
    4. main_init_weak.c, which initialises global and declares it to be a weak symbol:

      #include <stdio.h>
      
      int global __attribute__((weak)) = 999;
      
      int main(void) { printf("%d\n", global); return 0; }
      

    and another_def.c which initialises the same global:

    int global = 1234;
    

    Using main_uninit.c on its own gives 0:

    $ gcc -o test main_uninit.c && ./test
    0
    

    but when another_def.c is included as well, global is explicitly initialised and we get the expected result:

    $ gcc -o test main_uninit.c another_def.c && ./test
    1234
    

    (Note that this case fails instead if you’re using C++.)

    If we try with both main_init.c and another.def.c instead, we have 2 initialisations of global, which won’t work:

    $ gcc -o test main_init.c another_def.c && ./test
    /tmp/cc5DQeaz.o:(.data+0x0): multiple definition of `global'
    /tmp/ccgyz6rL.o:(.data+0x0): first defined here
    collect2: ld returned 1 exit status
    

    main_uninit_extern.c on its own won’t work at all – the extern keyword causes the symbol to be an ordinary external reference rather than a common symbol, so the linker complains:

    $ gcc -o test main_uninit_extern.c && ./test
    /tmp/ccqdYUIr.o: In function `main':
    main_uninit_extern.c:(.text+0x12): undefined reference to `global'
    collect2: ld returned 1 exit status
    

    It works fine once the initialisation from another_def.c is included:

    $ gcc -o test main_uninit_extern.c another_def.c && ./test
    1234
    

    Using main_init_weak.c on its own gives the value we initialised the weak symbol to (999), as there is nothing to override it:

    $ gcc -o test main_init_weak.c && ./test
    999
    

    But pulling in the other definition from another_def.c does work in this case, because the strong definition there overrides the weak definition in main_init_weak.c:

    $ gcc -o test main_init_weak.c another_def.c && ./test
    1234
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems to me most of developers completely ignore this features. People prefer handling security
Seems so basic, I can't believe I don't know this! I just need a
valgrind is reporting uninitialized memory errors from code like this: unsigned char buf[100]; struct
Seems like a simple problem, but can't get this to work. In the example
This seems to have most started since I upgrade my DBIx::Class and I can't
Hye guys. Okay. I have done this coding. But it seems have error. Can
Seems like a simple enough question but I can't seem to find the answer.
Seems like a simple problem: I have an SVN repo inside our firewall. I
Seems like the subtraction is triggering some kind of issue and the resulting value
Seems likes it might be useful to have the assert display a message when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.