Undefined symbols for architecture x86_64: "_gets_s"

765 views Asked by At

I am attempting to compile an example program from an the book Beginning C 5th Ed. by Ivor Horton. Attempting to compile it on OSX and I am getting the following output from gcc:

$ gcc program6_07.c 
program6_07.c:18:59: warning: format specifies type 'int' but the argument has
      type 'size_t' (aka 'unsigned long') [-Wformat]
          "Terminate input by entering an empty line:\n", str_len);
                                                          ^~~~~~~
program6_07.c:23:5: warning: implicit declaration of function 'gets_s' is
      invalid in C99 [-Wimplicit-function-declaration]
    gets_s(buf, buf_len);                                  // Read a lin...
    ^
program6_07.c:24:9: warning: implicit declaration of function 'strnlen_s' is
      invalid in C99 [-Wimplicit-function-declaration]
    if(!strnlen_s(buf, buf_len))                           // Empty lin...
        ^
program6_07.c:27:8: warning: implicit declaration of function 'strcat_s' is
      invalid in C99 [-Wimplicit-function-declaration]
    if(strcat_s(str, str_len, buf))                        // Concatenat...
       ^
program6_07.c:33:60: warning: data argument not used by format string
      [-Wformat-extra-args]
  printf("The words in the prose that you entered are:\n", str);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ^
program6_07.c:37:18: warning: implicit declaration of function 'strtok_s' is
      invalid in C99 [-Wimplicit-function-declaration]
  char * pWord = strtok_s(str, &str_len, delimiters, &ptr);  // Find 1st word
                 ^
program6_07.c:37:10: warning: incompatible integer to pointer conversion
      initializing 'char *' with an expression of type 'int' [-Wint-conversion]
  char * pWord = strtok_s(str, &str_len, delimiters, &ptr);  // Find 1st word
         ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
program6_07.c:45:13: warning: incompatible integer to pointer conversion
      assigning to 'char *' from 'int' [-Wint-conversion]
      pWord = strtok_s(NULL, &str_len, delimiters, &ptr);    // Find sub...
            ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 warnings generated.
Undefined symbols for architecture x86_64:
  "_gets_s", referenced from:
      _main in program6_07-4a1c4c.o
  "_strcat_s", referenced from:
      _main in program6_07-4a1c4c.o
  "_strnlen_s", referenced from:
      _main in program6_07-4a1c4c.o
  "_strtok_s", referenced from:
      _main in program6_07-4a1c4c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

the code of the program is as follows:

// Program 6.7 Find all the words
#define __STDC_WANT_LIB_EXT1__ 1                           // Make optional versions of functions available
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main(void)
{
  char delimiters[] = " \".,;:!?)(";                       // Prose delimiters
  char buf[100];                                           // Buffer for a line of keyboard input
  char str[1000];                                          // Stores the prose to be tokenized
  char* ptr = NULL;                                        // Pointer used by strtok_s()
  str[0] = '\0';                                           // Set 1st character to null

  size_t str_len = sizeof(str);
  size_t buf_len = sizeof(buf);
  printf("Enter some prose that is less than %d characters.\n"
          "Terminate input by entering an empty line:\n", str_len);

  // Read multiple lines of prose from the keyboard
  while(true)
  {
    gets_s(buf, buf_len);                                  // Read a line of input
    if(!strnlen_s(buf, buf_len))                           // Empty line ends input
      break;

    if(strcat_s(str, str_len, buf))                        // Concatenate the line with str
    {
      printf("Maximum permitted input length exceeded.");
      return 1;
    }
  }
  printf("The words in the prose that you entered are:\n", str);

  // Find and list all the words in the prose
  unsigned int word_count = 0;
  char * pWord = strtok_s(str, &str_len, delimiters, &ptr);  // Find 1st word
  if(pWord)
  {
    do
    {
      printf("%-18s", pWord);
      if(++word_count % 5 == 0)
        printf("\n");
      pWord = strtok_s(NULL, &str_len, delimiters, &ptr);    // Find subsequent words
    }while(pWord);                                           // NULL ends tokenizing
    printf("\n%u words found.\n", word_count);
  }
  else
    printf("No words found.\n");

  return 0;
}

I had thought that I had defined optional functions but I guess I have not? Is this just a dated book or a bad example or am I doing something wrong when I attempt to compile it?

1

There are 1 answers

2
Jonathan Leffler On BEST ANSWER

The gets_s() function is defined in Annex K of ISO/IEC 9899:2011, the current C standard, and in TR 24731-1. It is optional. It is not widely implemented — in fact, it is essentially only available on Microsoft systems using the Microsoft C libraries. Similar comments apply to the other _s functions — they're defined in Annex K and not generally implemented anywhere except under Microsoft's compilers.

Note that you can test whether the Annex K functions in general are available on an implementation by testing whether __STDC_LIB_EXT1__ is set by the compiler. It should have a value of 200509L for conformance with the original TR 24731-1 or 201112L for conformance with Annex K. If that is defined by the implementation, your code should define __STDC_WANT_LIB_EXT1__ to expose the definitions of the Annex K functions.

You will need to use different functions:

  • For gets_s(), the standard fgets() is probably the closest, but it includes the newline at the end of the input which gets_s() does not. Also, gets_s() truncates the output string on an error — if there isn't a newline before there is no space left; it also reads to the next newline if that happens. The fgets() function does none of these.
  • For strnlen_s(), use strnlen(), available on macOS Sierra (and Mac OS X) and Linux.
  • For strcat_s(), probably use strlcat(), again available on macOS Sierra and Linux.
  • For strtok_s(), probably use strtok_r(), which is a standard part of POSIX. Note that the interface to strtok_r() is different from the interface to strtok_s(), but the functionality is essentially the same.