[CMake] Problem with CHECK_FUNCTION_EXISTS and strcasecmp

Michael Surette mjsurette at gmail.com
Mon Mar 15 00:22:16 EDT 2010


On 03/14/2010 06:14 AM, Michael Wild wrote:
>
>
>
>> 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
>>
>
> The problem is that CMake _can't_ check this without your help. The reason is that strcasecmp is a preprocessor macro in one of the MS headers and in order for it to work you need to tell CMake to include this header when creating the small test-program.
>
That would have been my first guess too but I searched my entire drive 
for files containing strcasecmp and all that showed up was my project 
files and a header file from my MinGW install.  After giving it a little 
more thought I searched my project files for stricmp.  That's where the 
macro was.  My apologies to Microsoft for my impure thoughts.  I never 
did think of it as a shortcoming of CMake, just an inability on my part 
to find the macro.

> But you might as well check for the MSVC variable in your CMake code...

As it turns out, the search for _stricmp is the right way to go.

>
> HTH
>
> Michael

Thanks

Mike
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>




More information about the CMake mailing list