devopsfun
devopsfun

Reputation: 1418

Symbols in shared libraries

I have done this tutorial here to learn how to generate shared libraries. I added some smaller things because I wanted to test something out. Here are ma source codes:

strong text

#ifndef foo_h__
#define foo_h__

void foo(void);

class CFoo
{
public:
    void fooing1();
    void fooing2(int a);
    void fooing3(int a, int b = 0);
};

#endif      // foo_h__

foo.cpp

#include <stdio.h>
#include "foo.h"

void foo(void)
{
    puts("Hello, I'm a shared library");
}

void CFoo::fooing1()
{
    puts("CFoo::fooing1()");
}

void CFoo::fooing2(int a)
{
    printf("CFoo::fooing2(%d)\n", a);
}

void CFoo::fooing3(int a, int b)
{
    printf("CFoo::fooing3(%d, %d)\n", a, b);
}

main.c

#include <stdio.h>
#include "foo.h"

int main(void)
{
    puts("This is a shared library test...");
    foo();

    CFoo *foo1 = new CFoo();
    foo1->fooing1();
    foo1->fooing2(12);
    foo1->fooing3(1);
    foo1->fooing3(1, 2);
    delete foo1;


    return 0;
}

And the build script

g++ -c -Wall -Werror -fpic foo.cpp
g++ -shared -o libfoo.so foo.o
g++ -Wall -o test main.c -lfoo -L.

When typing the command nm -D libfoo.so | c++filt, I get this output:

 0000000000201040 B __bss_start
                  w __cxa_finalize
 0000000000201040 D _edata
 0000000000201048 B _end
 0000000000000818 T _fini
                  w __gmon_start__
 0000000000000640 T _init
                  w _ITM_deregisterTMCloneTable
                  w _ITM_registerTMCloneTable
                  w _Jv_RegisterClasses
                  U printf
                  U puts
 0000000000000796 T foo()
 00000000000007a8 T CFoo::fooing1()
 00000000000007c2 T CFoo::fooing2(int)
 00000000000007ea T CFoo::fooing3(int, int)

I have two questions:

1) Why do printf and puts have an U before their function names? And the functions foo(), CFoo::fooing1(), CFoo::fooing2(int) and CFoo::fooing3(int, int) does have a T before their function names?

2) What to do to get a U before the CFoo functions?

Upvotes: 1

Views: 2067

Answers (1)

Christophe
Christophe

Reputation: 73540

The letter U means that the symbols printf and puts are undefined in the library code you have produced. This is normal, as they are defined in another shared library.

The letter T means that the code is defined in the library file (the text section corresponds in fact to code; it shall not be confused with the data section).

If CFoo functions are meant to be the core content of your shared library, you'd better avoid makeing them U in your library. If you nevertheless want to achieve this :

  • keep this CFoo function declared in the class header
  • remove completely its in foo.cpp: the code will compile but the function is missing (i.e. it's not defined at all in the library file)
  • but call this function from another CFoo function in the foo.cpp: the code will compile, and this time the library file will have this function defined as U: a symbol whic is regularly declared , is needed, but was not found yet.

Upvotes: 3

Related Questions