在跨平台的应用程序中使用的snprintf [英] Using snprintf in a cross-platform application

查看:471
本文介绍了在跨平台的应用程序中使用的snprintf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,预计将与所有主要的编译器编译的C程序。目前,我在GCC在Linux机器上开发并承诺code之前,将汇编MSVC。为了使交叉编译容易,我与 -ansi 编译和 -pedantic 标志。这个工作很好,直到我开始使用的snprintf 这是不是在C89标准提供。 GCC可以编译这个没有 -ansi 开关但因为它不具有C99支持MSVC总是会失败。

I am writing a C program that is expected to be compiled with all major compilers. Currently I am developing on GCC on a linux machine and will compile on MSVC before committing the code. To make the cross-compiling easy, I am compiling with -ansi and -pedantic flags. This worked well until I started using snprintf which is not available in C89 standard. GCC can compile this without the -ansi switch but MSVC will fail always as it doesn't have C99 support.

所以,我不喜欢的东西,

So I did something like,

#ifdef WIN32 
#define snprintf sprintf_s
#endif

这工作得很好,因为的snprintf sprintf_s 具有相同的签名。我想知道这是正确的做法?

This works well because snprintf and sprintf_s has same signatures. I am wondering is this the correct approach?

推荐答案

您的做法是注定要失败的。

No. Your approach is doomed to failure.

开方 COS 具有相同的原型。你认为你可以在程序中交换他们和以前获得相同的行为/变更后?

sqrt and cos have the same prototype. Do you think you can swap them in a program and obtain the same behaviour from before / after the change?

您或许应该写你自己的的snprintf ,或从互联网上下载的实现(的谷歌是你的朋友),并使用这两个在Linux和Windows。

You probably should write your own snprintf, or download an implementation from the internet (google is your friend) and use that both in Linux and Windows.

这篇关于在跨平台的应用程序中使用的snprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆