IBM Support

Duplicate symbols with -qtemplateregistry

Question & Answer


Question

I am using the -qtemplateregistry method of automatic template instantiation. I still get duplicate symbol warnings even though the documentation states that this prevents template instantiations from being compiled more than once. Is -qtemplateregistry working correctly?

Cause

A large number of duplicate symbols cannot be discarded. This is expected behavior, even when a program is compiled with the -qtemplateregistry option. Part of the reason for the duplicate symbols is that the compiler is required by the C++ Standard to keep a definition of each inline function in every module that references it (section 7.1.2, para.4). Compiling with -qtemplateregistry does not change this behavior.

Another explanation for the duplicate symbols is the behavior of the AIX linker. The compiler places all symbols of a compilation unit into an entity called a CSECT. The AIX linker operates on CSECTs, performing garbage collection on whole CSECTs rather than on individual symbols. The linker does not break down a CSECT to discard unused symbols. Thus, some unused symbols cannot be discarded because they are in the same CSECT as symbols that are used. The retention of unused symbols is especially noticeable when compiling class templates, where most member functions are typically defined inline in the class, thus implicitly making them inline.

Answer

The following options, used in conjunction with -qtemplateregistry, can reduce the number of duplicate symbols reported by the AIX linker.

-qfuncsect

Instructs the compiler to place each function in its own CSECT. This option helps to separate used and unused symbols. The linker will be able to discard a greater number of unused symbols. For template classes with most functions being non-inline and defined separate from the class definition, this option provides both a smaller code size and fewer duplicate symbols.

-qstaticinline

Declares inline functions with static linkage, making them local to the compilation unit. The result is that even though the functions exist in multiple compilation units, each instance is local to its compilation unit, and therefore, are not reported as duplicates by the linker.

-qweaksymbol (Only for XL C/C++ v7)

[{"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Component":"Compiler","Platform":[{"code":"PF002","label":"AIX"}],"Version":"7.0","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
12 October 2022

UID

swg21173350