[CMake] Problem with CHECK_FUNCTION_EXISTS and strcasecmp
Michael Surette
mjsurette at gmail.com
Sat Mar 13 15:45:42 EST 2010
On 03/13/2010 01:03 PM, Mateusz Loskot wrote:
> Michael Surette wrote:
>> I am updating the CMake build files for a cross-platform project. One
>> of the tests is for strcasecmp for which I use CHECK_FUNCTION_EXISTS. If
>> it's not found the code generates its own function by that name.
>>
>> This works well with GCC under Linux, including cross-compiling with
>> MinGW, as well as MinGW under Windows.
>>
>> Testing this with MSVS 2008 Express under Windows XP, it fails to find
>> the function but gives errors and fails when I try to build.
>>
>> If I manually edit the config.h file generated by CMake so that it
>> misreports that there is a strcasecmp function available it compiles well.
>>
>> My conclusion is that either this function exists or a macro is defining
>> it, but it's not being found by CHECK_FUNCTION_EXISTS. I've greped
>> through the header files, but find no reference to it.
>>
>> How can I find this function reliably?
>
> CMake macro is right because Visual C++ does not define strcasecmp.
> This function is defined by POSIX (not even C99). Visual C++ does not
> implement POSIX. For Visual C++, you should look for equivalents, it is:
> _stricmp or _strnicmp
>
> Best regards,
Thank you for the prompt answer.
The problem isn't about standards or whether or not Visual C++ defines
strcasecmp, but that it handles it in a way that isn't compatible with
the CMake test. The whole idea of detecting resources is about working
around missing functions and defining the function yourself should be a
valid solution, regardless of the standards supported.
CMake detects whether or not that fuction exists and sets a variable.
When the code is compiled our own version of strcasecmp is #defined in
depending on that variable. This is a straightforward solution to a
simple problem. If Visual C++ really didn't define strcasecmp that
would be the end of it.
With observation, I have seen that Visual C++ automatically converts
calls to strcasecmp to be calls to _stricmp. This makes things messy,
because even though the function isn't officially there, we get compile
errors when we try to define it for ourselves and none when we don't.
This is a cross platform project and Visual C++ is only one of the many
targets we want to work. Visual C++ will compile the project without
code changes as long as we assume that it does have a strcasecmp
function, which as you pointed out, isn't true. IMHO, this is the sort
of thing that should be handled by CMake, not code changes.
Based on your information, I have added a check for _stricmp. When the
CMake script detects either strcasecmp or _stricmp it turns off
inserting our own strcasecmp. This solution builds well, although a
check for the compiler itself would probably be a better way.
Thanks for steering me in the right direction.
Mike
More information about the CMake
mailing list