在 C 中使用 Strfromd/strfromf 来转换为字符串而不使用 snprintf :警告:函数“strfromd”的隐式声明

0 c makefile

我正在探索使用strfromd、strfromf函数作为snprintf的替代方案,分别在我的 C 代码中将双精度、整数转换为字符串值。(我使用 Oracle 虚拟机 Ubuntu 20.04 版本进行构建。)

\n

我已经包括了

\n
#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n
Run Code Online (Sandbox Code Playgroud)\n

在我的代码中,但在编译过程中我收到以下警告:

\n
#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n
Run Code Online (Sandbox Code Playgroud)\n

我可以通过声明以下内容在我的电脑中删除此警告:

\n
int strfromd(char *restrict str, size_t n,const char *restrict format, double fp);\nint strfromf(char *restrict str, size_t n,const char *restrict format, float fp);\n
Run Code Online (Sandbox Code Playgroud)\n

在我的代码顶部(不确定这是否是正确的方法),并且通过我的虚拟机中的这一特定更改解决了警告,但是当在不同的环境中尝试时,我收到错误 - 对“ strfromd”的未定义引用错误并且我的构建崩溃了。

\n

有人在使用任何strfromd//函数时遇到类似的问题吗strfromfstrfroml可以采取什么措施来消除代码执行的这些问题?

\n

注意:\n我已经尝试过以下练习:

\n
#define __STDC_WANT_IEC_60559_BFP_EXT__\n#include <stdlib.h>\n
Run Code Online (Sandbox Code Playgroud)\n

该警告在我的虚拟机中已得到解决,但在其他环境中仍然给出了对“strfromd”错误的未定义引用。

\n

And*_*nle 5

根据手册strfromd(3)

strfromd()自版本 2.25 起, 、、strfromf()strfroml()函数在 glibc 中可用。

所以你的系统需要有glibc2.25以后的版本。

此外,您还应该#define __STDC_WANT_IEC_60559_BFP_EXT__

glibc 的功能测试宏要求(请参阅 feature_test_macros(7)):

  strfromd(), strfromf(), strfroml():
      __STDC_WANT_IEC_60559_BFP_EXT__
Run Code Online (Sandbox Code Playgroud)