Why doesn't PRIu64 work in this code?

馋奶兔 提交于 2019-12-01 02:10:53

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 (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>

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.

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>

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.

namaenashi

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

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"

PRIu64 is not defined where you use it.

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

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

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

LOCAL_CPPFLAGS := -D__STDC_FORMAT_MACROS
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!