C++ 为什么 PRIu64 在这段代码中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14535556/
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-27 18:25:16  来源:igfitidea点击:

Why doesn't PRIu64 work in this code?

c++printfc++03

提问by Masked Man

As per this answer, I tried printing a uint64_t, but it gives me an error:

根据这个答案,我尝试打印 a uint64_t,但它给了我一个错误:

error: expected ``)' before 'PRIu64'

错误:'PRIu64' 之前的预期 ``)'

Following is the minimal code showing what I am trying to do:

以下是显示我正在尝试做的最小代码:

#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <cstdio>

class X {
  X() {
    uint64_t foo = 0;
    printf("%07" PRIu64 ": ", foo);
  }
};

int main() {}

This minimal code compiles, but my actual code does not. However, I have tried with the 2 lines inside X::X()exactly the same in my actual code, and that does not work.

这个最少的代码可以编译,但我的实际代码没有。但是,我已经尝试X::X()在我的实际代码中使用完全相同的 2 行,但这是行不通的。

What should I look for to debug this further? My actual code also #includes other headers. Could that be causing the problem? Does order of including the headers matter?

我应该寻找什么来进一步调试?我的实际代码还有#include其他标题。这可能是导致问题的原因吗?包含标题的顺序是否重要?

EditPRIu64is defined as follows on my machine:

编辑PRIu64在我的机器上定义如下:

# if __WORDSIZE == 64
#  define __PRI64_PREFIX    "l"
#  define __PRIPTR_PREFIX   "l"
# else
#  define __PRI64_PREFIX    "ll"
#  define __PRIPTR_PREFIX
# endif

# define PRIu64     __PRI64_PREFIX "u"

采纳答案by Collin

One other possibility for this issue I just found in my own code is if another header already pulls in <inttypes.h>beforeyou define __STDC_FORMAT_MACROS. For example:

我刚刚在自己的代码中发现此问题的另一种可能性是,如果<inttypes.h>您定义__STDC_FORMAT_MACROS. 例如:

Utils.h(Perhaps originally written for C, as it was in our case):

Utils.h(也许最初是为 C 编写的,就像我们的例子一样):

#include <inttypes.h>

// ... Function declarations

MyFile.cpp:

我的文件.cpp:

#include "Utils.h"

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

Since inttypes.hhas already been included by Util.h, the compiler doesn't include it again, and doesn't see the declaration of __STDC_FORMAT_MACROS.

由于inttypes.h已经被 包含Util.h,编译器不会再次包含它,也看不到 的声明__STDC_FORMAT_MACROS

The solution is either to edit Utils.hto include #define __STDC_FORMAT_MACROS, or to make sure to define it before doing any includes in MyFile.cpp.

解决方案是编辑Utils.h以包含#define __STDC_FORMAT_MACROS,或确保在执行任何包含之前定义它MyFile.cpp

#define __STDC_FORMAT_MACROS
#include "Utils.h"
#include <inttypes.h>

The original setup actually compiled just fine on GCC 4.8 on Ubuntu, but failed with an old ltib GCC 4.3 toolchain for PowerPC, which made it all the more perplexing at first.

最初的设置实际上在 Ubuntu 上的 GCC 4.8 上编译得很好,但是在 PowerPC 的旧 ltib GCC 4.3 工具链上失败了,这使得它起初更加令人困惑。

回答by namaenashi

In C++ the macros are not automatically defined just by including the file.

在 C++ 中,宏不是通过包含文件自动定义的。

You need to add the following:

您需要添加以下内容:

#define __STDC_FORMAT_MACROS 1

before

#include <inttypes.h>

How to printf uint64_t? Fails with: "spurious trailing ‘%' in format"

如何打印 uint64_t?失败:“格式中的虚假尾随 '%'”

回答by Yakk - Adam Nevraumont

PRIu64is not defined where you use it.

PRIu64没有定义在哪里使用它。

Replace it with the string "llu"and your code will compile (but that is not a fix, it just demonstrates the problem)

用字符串替换它,"llu"您的代码将编译(但这不是修复,它只是说明了问题)

Maybe the includeis missing. Maybe over zealos include guards and it being included without the magic token block the define. Maybe your pch is busted.

也许include是失踪了。也许狂热者包括守卫,并且在没有魔法标记的情况下将其包含在内define。也许你的 pch 被破坏了。

回答by user18853

If you are on android JNI platform. Put this in your Android.mk:

如果您使用的是 android JNI 平台。把它放在你的 Android.mk 中:

LOCAL_CPPFLAGS := -D__STDC_FORMAT_MACROS