为什么PRIu64在此代码中不起作用? [英] Why doesn't PRIu64 work in this code?

查看:82
本文介绍了为什么PRIu64在此代码中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照此答案,我尝试打印 uint64_t ,但是它给了我一个错误:

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

错误:应在"PRIu64"之前输入)"

error: expected ``)' before '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() {}

这个最小的代码可以编译,但是我的实际代码却不能.但是,我尝试使用 X :: X()中的两行代码与我的实际代码完全相同,但这是行不通的.

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.

我应该寻找什么来进一步调试它?我的实际代码还 #include 其他标头.那会导致问题吗?包含标头的顺序重要吗?

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?

修改 PRIu64 在我的机器上定义如下:

Edit PRIu64 is defined as follows on my machine:

# 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"

推荐答案

我刚刚在自己的代码中发现的此问题的另一种可能性是,是否有另一个标头引入了< inttypes.h> 之前,您定义 __ STDC_FORMAT_MACROS .例如:

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

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

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

#include <inttypes.h>

// ... Function declarations

MyFile.cpp :

#include "Utils.h"

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

因为 Util.h 已包含 inttypes.h ,所以编译器不再包含它,也看不到的声明__STDC_FORMAT_MACROS .

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

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

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

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

原始设置实际上在Ubuntu上的GCC 4.8上编译得很好,但是由于旧的ltib GCC 4.3 PowerPC工具链失败,因此一开始就更加令人困惑.

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.

这篇关于为什么PRIu64在此代码中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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