I'm generating a .c file with global string variable from .make file and compile it into the static library. After linking that library to the shared library, global string symbol is missing, because it wasn't used in the library. How to forcibly link that specific object to the dynamic library, or use that string so the symbol will be added to the shared lib.
I've tried to generate another .c file from shared library makefile, but no result. I need that string to show up in shared lib's .data section.
Static lib's generated file
#ifndef _teststatic
#define _teststatic
#endif
char __test_static_string[] = "This string is in static lib.";
Shared lib's generated file
#ifndef _testshared
#define _testshared
#endif
char __test_shared_string[] = "This string is in shared lib.";
extern char __test_static_string[];
.data section of the shared library
Contents of section .data:
264f20 204f2600 00000000 00000000 00000000 O&.............
264f30 00000000 00000000 00000000 00000000 ................
264f40 00000000 00000000 00000000 00000000 ................
264f50 e61d1f00 00000000 f31d1f00 00000000 ................
264f60 0a1e1f00 00000000 191e1f00 00000000 ................
264f70 54686973 20737472 696e6720 69732069 This string is i
264f80 6e207368 61726564 206c6962 2e00 n shared lib..
Using gcc flag
--whole-archive
is no option.
I've found another - better way to do this.
char mystring[] = "This is generated string";
void foo()
{
(void)mystring;
}
This one works for header files(probably will work for cpp files too).
char __attribute__((weak)) mystring[] = "This string is in static
lib.";
for windows:
char __declspec(selectany) mystring[] = "This string is in static
lib."; // this one may be optimized based on provided flags, haven't
tested thoroughly.