[cmake-developers] CheckSymbolExists.cmake

Brad King brad.king at kitware.com
Thu Jun 6 09:26:02 EDT 2013


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".

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; }

 $ gcc -std=c99 -pedantic -Werror -O3 symbol_main.c symbol_impl.c

Alternatively we could go back to your suggestion to cast through
an integer type but use a few #ifdef's to try to choose an integer
type large enough for the pointer (long, long long, or __int64).

-Brad



More information about the cmake-developers mailing list