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.

Compiler/TMS570LS3137: Processing the functions from .h files

Expert 1995 points
Part Number: TMS570LS3137

Tool/software: TI C/C++ Compiler

Hello,

When I use just a single function taken from a .h file, among the several ones can populate the file, does the compiler allocate also those functions I do not use ?

Regards,

-Marco

  • I presume this header file only contains the function prototypes, and not the full implementation of the functions.  In that case, a prototype for a function that is not called has no effect.  This FAQ (not from TI) may be helpful.

    Thanks and regards,

    -George

  • Well, I performed a test with 2 programs. Both include a .h files but only one calls one of the declared functions. Looking at the linker map, it seems that even if you include the .h file the compiler creates the object but does not link it to the executable. But, as soon as you call just one of the function it links and allocate all of the functions (by linking the related object).

    So, in other words it seems it compiles all the functions even if you use just one of them. Is there any option to set it up to compile only function(s) that you actually use in the program ?

  • That's not how it works. The header file (.h) is just a description of a function. It does not cause the function to be included in your application. It just tells the compiler that such a function exists, somewhere, and how to call it. This allows you to compile your source files. The compiler generates an object file (.obj) that has references to every function that was actually called. If you included the header file for a function but do not call it, there will not be a reference to it.

    At link time, the linker looks at every reference in the input object files and pulls in object files from the library to satisfy those references. If those object files from the library themselves have references (and they usually do), the linker repeats the process until all functions are satisfied.

    It may be the case that your program uses functions which themselves use other functions, leading the linker to pull in functions you weren't expecting.

    It's also possible that some functions are defined in the same module in the library, in which case the linker must pull them both in. This is the case with malloc/calloc/free/realloc. Even though you might not call realloc, you still get the function if you call malloc.

    If these extra functions come from your own code, make sure you are using --gen_function_subsections=on so that the linker can select only those functions that it really needs.

    Now, to your example. What function is the linker pulling into your application that you think it should not have?
  • When I used/escluded the functions from the .h file, I tried with adding/commenting rtiInit() from rti.c/.h. Well, that function does not seem to call other functions, but rather an initialization of variables.
    As I described above, even including rti.h in the sys_main file, if I do not call rtiInit(), the compiler correctly does not link any object related to rti (i.e. rti.obj), while if I call rtiInit(), it links each function of rti.c.

    However, I tried also by creating my own set of functions (e.g. functions.c/.h) containing very basic operations not "explicitally" related each other like addition (var1 + var2), subtraction, multiplication, etc. So, when I use, for example, only the addition the compiler allocates also the other functions anyway.

    I have ARM Optimizing C/C++ Compiler v16.9.0.LTS and I cannot find information about --gen_function_subsections in its manual; the same searching into the manual of the v18.1.0.LTS. There is --gen_data_subsections instead, but I suppose that is another scope. Is it possible --gen_function_subsections was introduced in other versions? If yes, can you tell me which one ?

  • I'm sorry, the option is actually --gen_func_subsections, I got that wrong earlier. It is certainly available in 16.9.0.LTS

    The examples I gave used only functions and function calls, but it is possible for functions to drag in other functions by way of variable references.

    When making your own tests, make sure you either use ---gen_func_subsections or put each function in its own input file.