2

I am working in a platform with limited flash memory, and I recently hit the wall and tried to reduce the ReadOnly area. and strings used in printf seemed to be a good place to start. while doing this I came across the following code snippet. and I happened to find that the following reuses "failed" for multiple prints ( looking at the executable). where as normal printf doesn't do this optimization even when the same string parts are used. is there any optimization options in GCC (GCC 4.8.4) which can lead to less storage space for debug strings?

#define printf_failed(str) printf("%s failed", str); // reuses failed
7
  • A simpler, and perhaps even faster variation might be to say: fputs(str, stdout); fputs(kFailed, stdout);... Commented May 6, 2016 at 15:09
  • Are the macro invocations in separate compilation units? Can you compile with link time optimization enabled? Commented May 6, 2016 at 18:13
  • I tried -flto flag,, still the executable has the same strings present Commented May 6, 2016 at 21:59
  • @bare_metal: that's strange. Did you use it both when compiling and when linking? Commented May 6, 2016 at 22:02
  • To test it i tried it in a single .c file , gcc -Os -flto test_c.c , and i looked at the a.out, i can find the same strings. by the way LTO is expected to compact or share part of strings ( not full strings) ? Commented May 6, 2016 at 22:08

1 Answer 1

2

You can use the option -fmerge-constants in gcc. This option is option enabled with -O, -O2, -O3 and -Os.

Sign up to request clarification or add additional context in comments.

3 Comments

This app has already -Os enabled. but i tried -fmerge-constants in a PC environment. but don't see the recurring "part of string" being merged ( looking at a.out)
stackoverflow.com/questions/26433563/… , -fmerge-constants only merges identical full length strings and not "part of strings". anyway thanks.
Yes, because of the '\0', you can not merge part of strings. But your "%s failed" is not a part of string, each time your macro is expanded, it should be a mergeable string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.