I’m working with a code generator that produces C code and I need linkonce to eliminate duplicate definitions - but the clang compiler complains that the ‘selectany’ attribute is not recognized (even though I see looking through the clang source code that there’s code to handle it.) So I’m wondering why the attribute isn’t recognized, I’m guessing it could be any number of things (platform [OS X], source language (.c), and so on…)
Grepping the source code tells me that selectany is only implemented as microsoft extension. Test files use it in combination with __declspec and they enable this functionality with -fsm-compatibility. Note that I have no idea what this attribute actually does, I just did a search for you
OK that makes sense. So forget selectany for a moment - what I really want to know is, is there any way, from C, to give a symbol linkonce linkage? Or is this capability only available via the IR?
I can’t think of any construct in standard C that will get linkonce_odr linkage.
There is attribute((weak)) which gives ‘weak’ linkage. There are two differences:
- It is not “odr”, meaning it can be overridden by a different definition. Inlining or constant propagation is not allowed.
- It is not discardable, meaning it cannot be dropped if unreferenced.