c - 体系结构 x86_64 : "_gets_s" 的 undefined symbol

标签 c tr24731

我正在尝试从 Beginning C 5th Ed 一书中编译示例程序。艾弗霍顿。尝试在 OSX 上编译它,我从 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)

程序代码如下:

// 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;
}

我以为我已经定义了可选函数,但我想我没有?这只是一本过时的书还是一个不好的例子,还是我在尝试编译它时做错了什么?

最佳答案

gets_s() 函数在 ISO/IEC 9899:2011 的附录 K、当前的 C 标准和 TR 24731-1 中定义。它是可选的。它没有得到广泛实现——事实上,它基本上只能在使用 Microsoft C 库的 Microsoft 系统上使用。类似的注释适用于其他 _s 函数——它们在附件 K 中定义,通常不会在任何地方实现,除了在 Microsoft 的编译器下。

请注意,您可以通过测试编译器是否设置了 __STDC_LIB_EXT1__ 来测试一般情况下 Annex K 函数是否可用于实现。它的值应为 200509L 以符合原始 TR 24731-1 或 201112L 以符合 Annex K。如果由实现定义,您的代码应定义__STDC_WANT_LIB_EXT1__ 公开 Annex K 函数的定义。

您将需要使用不同的功能:

  • 对于 gets_s(),标准 fgets()可能是最接近的,但它在输入末尾包含换行符,而 gets_s() 没有。此外,gets_s() 会在出现错误时截断输出字符串——如果在没有剩余空间之前没有换行符;如果发生这种情况,它还会读取到下一个换行符。 fgets() 函数不执行这些操作。
  • 对于 strnlen_s(),使用 strnlen() ,适用于 macOS Sierra(和 Mac OS X)和 Linux。
  • 对于strcat_s(),可能使用strlcat() ,在 macOS Sierra 和 Linux 上再次可用。
  • 对于strtok_s(),可能使用strtok_r() ,这是 POSIX 的标准部分。请注意,strtok_r() 的接口(interface)与 strtok_s() 的接口(interface)不同,但功能基本相同。

关于c - 体系结构 x86_64 : "_gets_s" 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272394/

相关文章:

c - 在树中查找函数

c - 使用给定的指针初始化字符串

c - scanf 和 scanf_s 的区别

c - 未定义的对 memcpy_s 的引用

c - vsnprintf 和 vsprintf_s 有什么区别?

c - 如何读取/写入匿名共享映射?

c - 直到 C 中的 EOF 语法

c - 为什么指针到指针转换的隐式指针是合法的?

c - 为什么 gcc(或 glibc)不实现 _s 函数?

c - C 中的 Visual Studio 错误,0xfefefefe 处未处理的异常