This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiling a library file?

Guru 15580 points

I am including (linking) a very large library file in my project, but have temporarily commented out any references (calls) to the library functions. However, the compiler/linker appears to include all library functions during a build (seen in the .map file). Is this correct? Is there a way to exclude any non-referenced functions so that I can cut down on memory usage?

  • MikeH said:
    I am including (linking) a very large library file in my project, but have temporarily commented out any references (calls) to the library functions. However, the compiler/linker appears to include all library functions during a build (seen in the .map file). Is this correct?

    No.  Are you sure it is an object file library?

    Thanks and regards,

    -George

  • George,

    Georgem said:
    Are you sure it is an object file library?

    I'm not sure. I am compiling it as a static library in CCSv5.1. Should I be doing something different?

  • It is probably a library.  But let's make sure. Run the object file display (OFD) utility on it.  I don't know which toolset you are using.  Here is how it looks when running ofd470 from the ARM toolset on one of the RTS libraries ...

    % ofd470 rtsv5_A_le_eabi.lib
    ARCHIVE: rtsv5_A_le_eabi.lib
    OBJECT FILE: decode.obj
    ... 

    When you run OFD on your library, you should see similar output.

    Thanks and regards,

    -George

  • Duh. It's a library....since it's called file.lib. Sorry, should have known that.

  • The compiler put all functions in a .c file into a code section. If any function is called, the whole section is linked.

    search following options in you compiler user's guide:

    --gen_func_subsections, --generate_dead_funcs_list

  • Jianjun, George,

    Thanks for the suggestion. I have searched the TMS320C55x Optimizing C/C++ Compiler User’s Guide for these functions but could not find them.

    I'm still confused as to why the linker is including the entire static library when I am not calling any functions in the .lib file.

    I would also like to understand why the linker is not optimizing the final build by linking only the functions that *are* called by the main program code. Is this not a standard feature of the compiler? Is there a way to do this so that I can reduce the footprint of my final code?

  • MikeH said:
    Thanks for the suggestion. I have searched the TMS320C55x Optimizing C/C++ Compiler User’s Guide for these functions but could not find them.

    See http://www.ti.com/lit/ug/spru281g/spru281g.pdf sections 4.2.1 and 4.2.2

    MikeH said:
    why the linker is not optimizing the final build by linking only the functions that *are* called by the main program code.

    Yes, the linker tries not to pull in anything from the library that isn't directly referenced, but sometimes this optimization can be foiled by how the program or library is built.  If you do not build with --gen_func_subsections, individual functions in one module (object file) cannot be separated, and the unused one comes along for the ride.  Sometimes there are real dependencies that aren't obvious, such as printf requiring malloc.  Sometimes the user's code contains a reference to some library function they did not know about, such as C++ operator new requiring printf (in some versions of the library).  It's unlikely more could be said without examining your library and object files.

    How sure are you that you are getting every symbol in the library?  There's not one that the linker left out of the executable?  What does your command line look like?  What does your linker command file look like?

  • MikeH said:
    It's a library....since it's called file.lib.

    That is not necessarily sufficient to prove that it really is a library.  It might be an object file named .lib, for some strange reason.  Please run the following command and let us know if it gives any sort of error message:

    ar55 t file.lib
  • Sorry, my bad. I had neglected to comment out one (main) call to the .lib. After commenting it out the .lib is not linked.

    Thanks guys for the help.