【问题标题】:How to use strlcpy in g++'s -std=c++0x mode?如何在 g++ 的 -std=c++0x 模式下使用 strlcpy?
【发布时间】:2013-01-25 09:47:08
【问题描述】:

当我使用 g++ 的-std=c++0x 参数时,我想使用string.h 中缺少的strlcpy(调用外部api)。

% g++ -std=c++0x foo.cpp
foo.cpp: In function 'int main(int, char**)':
foo.cpp:5:11: error: 'strlcpy' was not declared in this scope
% g++ foo.cpp
% cat foo.cpp
#include <string.h>

int main(int argc, char* argv[])
{
  const char src[] = "foo";
  char dest[1024] = { 0 };
  strlcpy(dest, src, sizeof(dest));
  return 0;
}

是否可以使用strlcpystd=c++0x 标志,还是我必须放弃后者?

此外,我无法在 cygwin 中找到 strlcpy 联机帮助页,尽管它们似乎具有该功能。有什么指点吗?

我在 cygwin 上使用 gcc 4.7.2。

【问题讨论】:

    标签: g++ cygwin string.h


    【解决方案1】:

    引用维基百科:Criticism on strlcpy

    比较流行的strlcat和strlcpy在基础上被诟病 他们鼓励使用 C 字符串,从而产生更多问题 比他们解决的。因此,它们没有被纳入 GNU C 库(由 Linux 上的软件使用),尽管它们是 在 OpenBSD、FreeBSD、NetBSD、Solaris、Mac OS X、QNX 和 甚至在 Linux 内核内部。

    【讨论】:

    • 由于在我的情况下不使用 c 字符串的唯一选择是删除外部 api,我宁愿使用“更安全”的 c 字符串函数,你不同意吗?
    • 确实如此。请注意尺寸
    猜你喜欢
    • 2012-09-29
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多