39

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

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() {}

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.

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?

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"
Community
  • 1
  • 1
Masked Man
  • 1
  • 7
  • 40
  • 80
  • Why are you using printf in C++? Furthermore, we can't debug code we can't see... Common errors include missing ; or ). – Alex Chamberlain Jan 26 '13 at 09:31
  • 2
    Can you check what exactly `PRIu64` is defined as? – Ajay Jan 26 '13 at 09:49
  • I do know `cout`. :) It is slower than `printf` on my system, so I prefer using it only in cases where `printf` isn't good enough. I know you cannot debug code you cannot see. I am hoping to get some ideas on what could be wrong, so that I could try debugging further. – Masked Man Jan 26 '13 at 09:49
  • @Deidara-senpai: Add the line which gives you the error, since your minimal example compiles. – Zeta Jan 26 '13 at 09:50
  • @Ajay Edited question. That gives me some ideas on how to debug further. Thanks. – Masked Man Jan 26 '13 at 09:59
  • @Zeta In my actual code, I tried with the exact same 2 lines. Unfortunately, I cannot share the actual code. – Masked Man Jan 26 '13 at 10:00
  • @MaskedMan [Collin's answer](http://stackoverflow.com/a/30851225/2436175) should be the accepted one: it very well match the fact that your minimal example works, but not your actual code. – Antonio Dec 05 '16 at 17:00
  • @Antonio you are right, I did that now. I left that job a couple of years ago, so no longer remember what the original issue was, but nonetheless, the answer describes the problem clearly. – Masked Man Dec 05 '16 at 17:22

4 Answers4

32

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"

Community
  • 1
  • 1
namaenashi
  • 321
  • 3
  • 2
  • I wonder if you have actually read the question and my code. Not sure I follow what's the point of linking to that other answer, which I have already linked to in the first line of my question, meaning that I have already read that answer before asking the question. – Masked Man Sep 12 '13 at 06:36
29

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.

Collin
  • 11,977
  • 2
  • 46
  • 60
5

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 1
    I found out that the header isn't getting included, although I have used the `#include` directive exactly as above. I copied the `#define`s from the `inttypes.h` and then it works. What would cause an included header to get not included? There are no guard statements around the `#include`. – Masked Man Jan 26 '13 at 13:45
  • 1
    0Pch (Precompiled headers). Pragmas. Guards inside the header. Most compilers have ways to dump what files they include from where. Or you could stuff an error into the header and recompile the source file. – Yakk - Adam Nevraumont Jan 26 '13 at 14:41
2

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

LOCAL_CPPFLAGS := -D__STDC_FORMAT_MACROS
user18853
  • 2,771
  • 1
  • 21
  • 17