【问题标题】:undefined reference to `strcpy_s' can't compile对“strcpy_s”的未定义引用无法编译
【发布时间】:2013-05-13 10:48:15
【问题描述】:

我遵循这本书,无法编译这个例子。有什么建议吗?

  1 #define __STDC_WANT_LIB_EXT1__ 1
  2 #include <string.h>
  3 #include <stdio.h>
  4 
  5 
  6 int main(void)
  7 {
  8         char source[] = "Here we go...";
  9         char destination[50];
 10 
 11         if(strcpy_s(destination, sizeof(destination), source))
 12                 printf("An error occurred copying the string.n");
 13 
 14 
 15         return 0;
 16 }

错误:

/tmp/ccc5KZDZ.o: In function `main':
test.c:(.text+0x48): undefined reference to `strcpy_s'
collect2: error: ld returned 1 exit status

【问题讨论】:

标签: c reference undefined strcpy tr24731


【解决方案1】:

strcpy_s() 函数在 TR 24731-1(参见 Do you use the TR 24731 'safe' functions)以及 ISO/IEC 9899:2011(C 2011 标准)的可选附件 K 中定义。

您可以通过以下方式测试您的实现是否支持它:

__STDC_LIB_EXT1__整型常数200509L,用于表示 符合本技术报告。

(根据 TR)。附件 K 简单地说:

定义__STDC_LIB_EXT1__ 的实现应符合 本附件中的规范。380)

380) 未定义 __STDC_LIB_EXT1__ 的实现不需要遵守这些 规格。

answer 表示 C 2011 标准的 TC1(技术勘误 1)定义 __STDC_LIB_EXT1__ 对于附件 K 版本的函数应为 201112L(2011 年 12 月)。我没有跟踪附件 K 和 TR 24731-1 之间是否存在差异。

所以,您应该可以通过测试__STDC_LIB_EXT1__ 来测试您的库是否支持strcpy_s(),但它是可选的。 Ulrich Drepper 显然声明 GNU C 库不支持 TR 24731-1 函数;我不知道这是否仍然成立。

另外,请注意,其中某些功能的 Microsoft 接口与相同功能的标准接口不同(例如,vsnprintf_s())。这限制了它们作为跨平台可移植工具的实用性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2012-02-10
    相关资源
    最近更新 更多