【发布时间】:2017-10-04 16:37:44
【问题描述】:
如何编写代码以在没有警告的情况下进行跨平台编译。例如,我在 x64 平台上没有收到警告,但在 ARM (raspberry PI) 上却收到了警告:
警告:格式“%lu”需要“long unsigned int”类型的参数,但参数 5 的类型为“size_t {aka unsigned int}”
不用说我不想禁用警告。
更多示例和场景:
warning: format ‘%lu’ expects argument of type ‘long unsigned int’,
but argument 5 has type ‘uint64_t {aka long long unsigned int}’
uint64_t Created; // 8 bytes
time_t now = time(NULL);
"Current time: %li sec, %lu nanosecs", now, msg.Created
size_t 可能是罪魁祸首:
sizeof的基本使用:
warning: format ‘%lu’ expects argument of type ‘long unsigned int’,
but argument 4 has type ‘unsigned int’
tr_debug("pbJobs size: %lu", sizeof(pbJobs));
tr_debug 相当于 Mbed OS 平台的 printf。是的,我在 Mbed OS 和 Linux 上编译。
【问题讨论】:
-
对所使用的类型使用正确的格式说明符?使用您使用的标准类型特别容易。
-
阅读文档以找到每种标准类型的正确格式说明符。
-
贴出引起waring的代码及其参数的类型。
-
不要在 C++ 中使用
*printf()并避免整个问题。 -
你可以使用
std::cout避免这个问题吗?
标签: c++ c format printf cross-platform