[CMake] Checking function or symbol in namespace

Marcel Loose loose at astron.nl
Fri Jan 22 04:24:30 EST 2010


On Thu, 2010-01-21 at 13:47 +0100, Michael Wild wrote:
> On 21. Jan, 2010, at 12:07 , Marcel Loose wrote:
> 
> > On Thu, 2010-01-21 at 10:14 +0100, Michael Wild wrote:
> >> On 21. Jan, 2010, at 9:56 , Marcel Loose wrote:
> >> 
> >>> On Thu, 2010-01-21 at 06:52 +0100, Michael Wild wrote:
> >>>> On 21. Jan, 2010, at 24:55 , Mateusz Loskot wrote:
> >>>> 
> >>>>> Hi,
> >>>>> 
> >>>>> I'm trying to perform the following checks:
> >>>>> 
> >>>>> check_function_exists(std::pow HAVE_POW)
> >>>>> 
> >>>>> or
> >>>>> 
> >>>>> check_symbol_exists(std::pow cmath HAVE_POW)
> >>>>> 
> >>>>> but it looks that both macros have troubles
> >>>>> with resolving std:: namespace.
> >>>>> 
> >>>>> The documentation is not very clear about that.
> >>>>> 
> >>>>> Or I should stick to try_compile based tests?
> >>>>> 
> >>>>> Best regards,
> >>>>> 
> >>>> 
> >>>> 
> >>>> AFAIK check_symbol_exists is used for preprocessor symbols.
> >>> check_function_exists looks for C functions, not C++. Also notice,
> > that
> >>> older C++ standard libraries and non-compliant C++ compilers don't
> > use
> >>> the namespace std. You might want to have a look at
> >>> CMakeBackwardCompatibilityCXX.cmake or the various other modules
it
> >>> uses.
> >>>> 
> >>>> 
> >>>> HTH
> >>>> 
> >>>> Michael
> >>>> 
> >>> I thought that check_symbol_exists() was meant to check for the
> >>> *declaration* of a symbol (e.g. a function) in a header file; and
> > that
> >>> check_function_exists() was meant to check for the *definition* of
a
> >>> function (i.e. exists as symbol in a library).
> >>> 
> >>> Best regards,
> >>> Marcel Loose.
> >> 
> >> Just checked: check_symbol_exists works for both. E.g. testing for
> > deflateInit in zlib.h (which is a #define) results in the following
test
> > code:
> >> 
> >> #include <zlib.h>
> >> 
> >> void cmakeRequireSymbol(int dummy,...){(void)dummy;}
> >> int main()
> >> {
> >> #ifndef deflateInit
> >>  cmakeRequireSymbol(0,&deflateInit);
> >> #endif
> >>  return 0;
> >> }
> >> 
> >> 
> >> As you can see, the code will compile successfully for both cases.
> >> 
> >> IMHO the documentation should be clarified...
> >> 
> >> 
> >> Michael
> > 
> > Yes, I agree. The documentation could be clearer about that.
> > 
> > If I understand it correctly, check_symbol_exists() will only try to
> > compile a test program, whereas check_function_exists() will also
try to
> > link a test program. Right or wrong?
> > 
> > Best regards,
> > Marcel Loose.
> 
> Yes, check_function_exists compiles and links a small program.
However, it will only work for real C functions, not preprocessor
symbols.
> 
> OTOH, none of these functions checks the function signature...
> 
> Michael

Partly true.
If the preprocessor symbol provides a generic name for a function, which
may have different names on different platforms, it will still work.

For example.

# define basename  __xdg_basename

would define the symbol basename that would point to the function
__xdg_basename.

A test program using basename() as a function would still compile and
link, provided that the symbol __xdg_basename appears in one of the link
libraries.

Best regards,
Marcel Loose.



More information about the CMake mailing list