Search code examples
cstaticsymbolslibraries

What is a symbol in programmation as referred in ar command


I see this term a lot, 'symbol', and after searching on google I still can't find a definition that makes sense to me.

For example in the manual of ar command on Linux, it's said :

ar creates an index to the symbols defined in relocatable object modules in the archive when you specify the modifier s.

Are function declarations / variable declarations / defines / structure declarations etc, symbols ? Or is a symbol a term for .o files ?

In this context, what is a symbol exactly ? Act like I'm a complete beginner who knows nothing when you form your answer please !


Solution

  • A symbol is the name of things (functions, variables etc). Here is an example file:

    #include <stdlib.h>
    
    struct foo {
        char *a;
    };
    struct foo global_foo;
    static struct foo static_foo;
    
    struct foo *foo_create() {
         return malloc(sizeof(struct foo));
    }
    

    which I then compile and make an archive and then use nm to list its symbols:

    $ gcc -c 1.c && ar -rc 1.a 1.o && nm -s 1.a
    
    Archive index:
    global_foo in 1.o
    foo_create in 1.o
    
    1.o:
    0000000000000000 T foo_create
    0000000000000000 B global_foo
                     U _GLOBAL_OFFSET_TABLE_
                     U malloc
    0000000000000008 b static_foo