Linux sprintf_s 未在此范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4828228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:40:15  来源:igfitidea点击:

sprintf_s was not declared in this scope

clinuxtr24731

提问by SPB

I have a C program that uses sprintf_s. It works fine in Windows, but when I compile my code in Linux it gives this error:

我有一个使用sprintf_s. 它在 Windows 中运行良好,但是当我在 Linux 中编译我的代码时,它会出现以下错误:

sprintf_s was not declared in this scope.

Why does this happen and how can I fix it?

为什么会发生这种情况,我该如何解决?

采纳答案by peoro

It's not standard, you won't find such function on Linux.

这不是标准的,你不会在 Linux 上找到这样的功能。

Standard function snprintfshould have a similar semantics.

标准函数snprintf应该有类似的语义。

回答by gnclmorais

sprintf_s is not part of the standard C library, so it is not portable, thus you are not able to use it in Linux. BUT you have snprintf, which is very similar and it should help you to accomplish what you want.

sprintf_s 不是标准 C 库的一部分,因此不可移植,因此您无法在 Linux 中使用它。但是你有 snprintf,它非常相似,它应该可以帮助你完成你想要的。

回答by Alberto Soto Ballesteros

sprintf_sis not part of the standard C library, and you won't be able to use it in Linux.

sprintf_s不是标准 C 库的一部分,您将无法在 Linux 中使用它。

However, snprintfis standard and should do the same task.

但是,snprintf是标准的,应该做同样的任务。

回答by Deduplicator

sprintf_sis only part of Annex K, an optional Annexto the C11 standard:

sprintf_s只是附件 K 的一部分,C11 标准的可选附件

Annex K

...

K.2 Scope

  1. This annex specifies a series of optionalextensions that can be useful in the mitigation of security vulnerabilities in programs, and comprise new functions, macros, and types declared or defined in existing standard headers.

...

K.3.5.3.6 The sprintf_s function

Synopsis

#define __STDC_WANT_LIB_EXT1__1
#include <stdio.h>
int sprintf_s(char * restrict s, rsize_t n,
const char * restrict format, ...);

附件K

...

K.2 范围

  1. 本附件规定了一系列可用于缓解程序安全漏洞的可选扩展,包括在现有标准头中声明或定义的新函数、宏和类型。

...

K.3.5.3.6 sprintf_s 函数

概要

#define __STDC_WANT_LIB_EXT1__1
#include <stdio.h>
int sprintf_s(char * restrict s, rsize_t n,
const char * restrict format, ...);

(emphasis added)

(强调)

It never made it into POSIX (or Linux) (and is not missed at all, there are even arguments about its usefulness in the committee).

它从未进入 POSIX(或 Linux)(并且根本没有错过,甚至在委员会中也有关于它的有用性的争论)。

For better portability, use snprintfwhich is part of the core standard and provides all the functionality you'll need.

为了获得更好的可移植性,请使用snprintf它作为核心标准的一部分并提供您需要的所有功能。

回答by Andrey

During a porting of my program from Windows to Linux, I wrote following implementation in my own windows.h:

在将我的程序从 Windows 移植到 Linux 的过程中,我在自己的 windows.h 中编写了以下实现:

inline int sprintf_s(char* buffer, size_t sizeOfBuffer, const char* format, ...)
{
    va_list ap;
    va_start(ap, format);
    int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
    va_end(ap);
    return result;
}

template<size_t sizeOfBuffer>
inline int sprintf_s(char (&buffer)[sizeOfBuffer], const char* format, ...)
{
    va_list ap;
    va_start(ap, format);
    int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
    va_end(ap);
    return result;
}

回答by rurban

snprintf is insecure, only sprintf_s is secure. snprintf does not guarantee to add a final \0, leading to possible subsequent overflows. look at https://github.com/rurban/safeclibfor a proper implementation.

snprintf 是不安全的,只有 sprintf_s 是安全的。snprintf 不保证添加最终的 \0,从而导致可能的后续溢出。查看https://github.com/rurban/safeclib以获得正确的实现。