Thread: When I declare a variable, how will I know what type of linkage it is?

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    15

    When I declare a variable, how will I know what type of linkage it is?

    I am trying very hard to understand what is the linkage process in c language. I've read a lot about it but I still don't fully understand how it works.

    I understand that a C program can consist of multiple C files. We can declare variable in one function like local variable, declare it in single file like main.c, also declare in other file extern variable.

    There are three types of linker: external, internal and none

    When I declare a variable, how will I know what type of linkage it is?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,190
    > When I declare a variable, how will I know what type of linkage it is?
    Basically, everything at file scope is external by default unless you use the static keyword.

    Everything declared inside a function has no linkage by default, and only internal linkage if you use the static keyword.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    15
    I think it is a challenge for a compiler to know where the variable is declared. I think the linker can tell whether the variable is in the same scope, or it is in different scope or declared in another source file.

    What happens in internal linkage?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,190
    > I think it is a challenge for a compiler to know where the variable is declared.
    Not really.
    At the start of the .c file, the scope is global.
    After that, it's just a matter of keeping track of how many open and close braces it sees.
    Code:
    /* Starts in global scope */
    int global;
    void foo ( void )
    { /* now a local scope begins */
    
    }
    /* back to a global scope */
    Example, using objdump to display detailed information about each symbol.
    Code:
    $ cat foo.c
    #include <stdio.h>
    extern int other;
    int gvar = 2;
    static int lvar = 22;
    int main ( ) {
        int temp = 42;
        printf("%d %d %d %d\n", other, gvar, lvar, temp);
    }
    $ gcc -c foo.c
    $ objdump -t foo.o
    
    foo.o:     file format elf64-x86-64
    
    SYMBOL TABLE:
    0000000000000000 l    df *ABS*	0000000000000000 foo.c
    0000000000000000 l    d  .text	0000000000000000 .text
    0000000000000000 l    d  .data	0000000000000000 .data
    0000000000000000 l    d  .bss	0000000000000000 .bss
    0000000000000004 l     O .data	0000000000000004 lvar
    0000000000000000 l    d  .rodata	0000000000000000 .rodata
    0000000000000000 l    d  .note.GNU-stack	0000000000000000 .note.GNU-stack
    0000000000000000 l    d  .note.gnu.property	0000000000000000 .note.gnu.property
    0000000000000000 l    d  .eh_frame	0000000000000000 .eh_frame
    0000000000000000 l    d  .comment	0000000000000000 .comment
    0000000000000000 g     O .data	0000000000000004 gvar
    0000000000000000 g     F .text	0000000000000045 main
    0000000000000000         *UND*	0000000000000000 other
    0000000000000000         *UND*	0000000000000000 _GLOBAL_OFFSET_TABLE_
    0000000000000000         *UND*	0000000000000000 printf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linkage vs scope of a variable
    By MartinR in forum C Programming
    Replies: 5
    Last Post: 05-03-2018, 04:37 AM
  2. declare that a variable will be used later?
    By SandyMan in forum C Programming
    Replies: 6
    Last Post: 04-06-2015, 06:05 PM
  3. how to declare an external variable of type "enum"?
    By Adam Rinkleff in forum C Programming
    Replies: 28
    Last Post: 06-23-2011, 09:52 PM
  4. Compiler says I didn't declare a type, even though I have.
    By misterdanny in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2009, 11:12 PM
  5. Get sense of internal linkage and external linkage
    By gandalf_bar in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 05:57 AM

Tags for this Thread