[cmake-developers] CheckSymbolExists.cmake
Christopher Sean Morrison
brlcad at mac.com
Thu Jun 6 10:37:19 EDT 2013
On Jun 6, 2013, at 9:26 AM, Brad King wrote:
> On 06/05/2013 05:33 PM, Christopher Sean Morrison wrote:
>> union t { void (*fn)(); char val[8]; } a = {0};
>> a.fn = (void (*)())&printf;
>
> Yes, if we know the symbol is a function, but it could be data too.
> That's why the name "CheckSymbolExists" was chosen to replace the
> original "CheckFunctionExists".
Ah yes, I lost sight of the forest looking at a tree...
> We can return to hiding sending the pointer through varargs
> but move the symbol definition to somewhere the compiler will
> not optimize it out:
>
> $ cat symbol_main.c
> #include <stdio.h> /* declares tested symbol */
> extern int cmakeRequireSymbol(int, ...);
> int main(int argc, const char* argv[]) { return cmakeRequireSymbol(argc, &printf); }
>
> $ cat symbol_impl.c
> int cmakeRequireSymbol(int x, ...) { return x; }
This sounds looks like the best option to me, better than casting through an integer. Maybe also passing &argv, argv[argc-1], or similar just in case some compiler out there warns about it not being used. Does assume at least a c89-compliant compiler, but I'd be surprised if any compiler will be able to detect an unused vararg in a separate compilation unit anytime soon. It should hopefully be robust.
Want me to work up a patch or is someone already on it?
Cheers!
Sean
More information about the cmake-developers
mailing list