I had a code inside the "main" that could be detached into a static library, so I separated this part into a new static library folder and included it with my main script.
as follows I had the example with a no-library version
#include "XXX_lib/XXX_Core.h"
void main(){
/* list of code that uses some functions from XXX_lib/XXX_Core.h library
* which is been found inside the main project as a sub folder and
* I included the main header file from this folder
*/
}
and the project tree was like
|-Main_Project_foler
|--- main.c
|--- XXX_LIB -> (folder)
|-------XXX_Core.h
|-------XXX_Core.c
|-------XXX_OS.h
|-------XXX_OS.c
|-------XXX_Patterns.h
|-------XXX_Patterns.c
|------- .....
afterward, I separated the XXX_lib files into a static library and generate an XXX_lib.lib file that I can use along with my main code as a static library and I can reuse it with other modules in my project so the project structure was like
|-STATIC_LIBRARY_XXX_folder
|--- Debug ->(folder)
|-------XXX_LIB.lib
|--- XXX_LIB -> (folder)
|-------XXX_Core.h
|-------XXX_Core.c
|-------XXX_OS.h
|-------XXX_OS.c
|-------XXX_Patterns.h
|-------XXX_Patterns.c
|------- .....
|
|-Main_Project_folder
|--- main.c
and I included the absolute path of my XXX_Lib directory for the compiler and the .lib file to the linker search path and the compiler is TI Arm C/C++ Compiler Version 18.12.5.LTS and the code are running on a 32-bit MCU ( TM4C129 ) and options passed to the compiler have many include paths so excluding them with ${manyIncludePaths} arg the flags are
-mv7M4 --code_state=16 --float_support=FPv4SPD16 -me -Ooff --opt_for_speed=2 ${manyIncludePaths} --define=PART_TM4C129XNCZAD --define=ccs --define=TIVAWARE --define=ccs="ccs" -g --gcc --diag_warning=225 --diag_wrap=off --display_error_number --gen_func_subsections=on --enum_type=packed --abi=eabi
and the linker flags are
-m"${ProjName}.map" --heap_size=0 --stack_size=512 -i"${INHERITED_LIBRARY_PATH}" -i"C:/ti/wolfssl/tirtos/packages/ti/net/wolfssl/lib" -i"${CG_TOOL_ROOT}/lib" -i"${CG_TOOL_ROOT}/include" --priority --reread_libs --diag_wrap=off --display_error_number --warn_sections --xml_link_info="${ProjName}_linkInfo.xml" --rom_model
these are the default linker flags and I added --lto
and noticed that the code size is reduced in both cases no-library and library versions.
after that, I noticed the final .out file generated by the compiler increased by twice the static library size with optimization set to level 2 for Static Library and main code
I checked the map file in both versions(no-library and with library versions) and found that my module in the library version had a file (XXX_Patterns.c) that had taken much size than usual (I mean than no-library version), so from this comparison, I noticed that this file had many static structures with #pragma allocating them to static SRAM cause my MCU has an EPI peripheral, so I used external memory (SRAM),
XXX_Patterns.c file is like
#pragma DATA_SECTION(objColor1, ".xram3");
static XXX_ColorType objColor1[XX_MAX_NUMBER];
#pragma DATA_SECTION(objColor2, ".xram3");
static XXX_ColorType2 objColor2;
static INT32U u32Count;
static INT32U u32MaxLoops;
static void XXX_func1(void);
static void XXX_func2(void);
...
so, How to eliminate that size difference between the no-library and library versions because the two examples I mentioned have the same functionality(the one with the library separated and the one that is not-separated)?
the problem was in compiler flags as it needs to separate each subsection of functions into a separate block obj so while the ".out" is generated it should link only the used sections in the main object file that is going to be linked so as you can see in the image below just enable this option and the code size would be the same as no-library version.