'Can gcc optimize const strings (rodata) that are the same, but declared in different modules?

I'm using GCC for embedded, STM32 (arm-none-eabi-gcc latest) and I'm not clear about compiler optimization related to const string arguments (using -O3)

Considering this example

const char test[] = "testing string";

void foo(const char* str) {

    Uart1Puts(str);
}

void bar(const char* str) {

    Uart1Puts(str);
}

Calling like this

    foo("testing string");
    bar("testing string");

will produce the same result (size, ram) as

    foo(test);
    bar(test);

However, if I have another function in another module

extern const char test;
    void func_in_other_module(const char* str) {
    
        Uart1Puts(str);
    }

Calls will produce different results

func_in_other_module("testing string"); // bigger this one with approx. size of this string
func_in_other_module(test);

Now my question is can I convince GCC to figure out that consts are identical and should be one?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source