3

I want to use CMake to check whether stdlib.h has the getopt() function (I know this has been true for many years, but I still need to perform this check, and this question is about the general issue).

I know how to check for the presence of an include file:

check_include_files(malloc.h HAVE_MALLOC_H)

but not the presence of a function made available by that file. How can I do that?

Edit: Actually getopt() is never in stdlib.h, it's typically in unistd.h, but never mind, the question can stand as asked.

einpoklum
  • 118,144
  • 57
  • 340
  • 684

3 Answers3

4

You can do it using CheckSymbolExists():

CHECK_SYMBOL_EXISTS(getopt stdlib.h HAVE_GETOPT)
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • So, your answer is technically what I asked for, but I realized that what my code really needed was my answer, i.e. checking for the availability of a function in general rather than whether it's available via a certain header. – einpoklum Jan 26 '18 at 15:09
2

I was thinking of using CheckFunctionExists():

CheckFunctionExists(getopt, HAVE_GETOPT)

... and as it turns out, I actually need this one rather than the accepted answer, since the availability of a function generally is what was more significant in my case, rather than whether it was made available by a certain header.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

you may use this code to implement your own function to match string

        #define CHUNK 1024 /* read 1024 bytes at a time */
    char buf[CHUNK];
    FILE *file;

    if (check_file("malloc.h",HAVE_MALLOC_H) == 1)
        printf("Match found!");
    else
        printf("Match not found");

    int check_file(char *file_name, char *match_str)
    {
        file = fopen(file_name, "r");
        while(fgets(match_str,CHUNK , file) != EOF)
        {
            if(strstr(str,match_str) != NULL)
                return 1;
        }
        return 0;
    }
JDP
  • 56
  • 2
  • 10