Repository: cocotb/cocotb

Issue:
Compile error in VhpiCbHdl.cpp: expected ‘)’ before ‘PRIu32’
I'm getting the following error:
```
  In file included from cocotb/share/include/gpi.h:60,
                   from cocotb/share/lib/vhpi/../gpi/gpi_priv.h:32,
                   from cocotb/share/lib/vhpi/VhpiImpl.h:31,
                   from cocotb/share/lib/vhpi/VhpiCbHdl.cpp:29:
  cocotb/share/lib/vhpi/VhpiCbHdl.cpp: In member function ‘virtual GpiIterator::Status VhpiIterator::next_handle(std::string&, GpiObjHdl**, void**)’:
  cocotb/share/lib/vhpi/VhpiCbHdl.cpp:1150:69: error: expected ‘)’ before ‘PRIu32’
   1150 |         LOG_DEBUG("Unable to get the name for this object of type " PRIu32, type);
        |                                                                     ^~~~~~
  cocotb/share/include/gpi_logging.h:52:98: note: in definition of macro ‘LOG_DEBUG’
     52 | #define LOG_DEBUG(...)     gpi_log("cocotb.gpi", GPIDebug,         __FILE__, __func__, __LINE__, __VA_ARGS__);
        |                                                                                                  ^~~~~~~~~~~
  cocotb/share/include/gpi_logging.h:52:35: note: to match this ‘(’
     52 | #define LOG_DEBUG(...)     gpi_log("cocotb.gpi", GPIDebug,         __FILE__, __func__, __LINE__, __VA_ARGS__);
        |                                   ^
  cocotb/share/lib/vhpi/VhpiCbHdl.cpp:1150:9: note: in expansion of macro ‘LOG_DEBUG’
   1150 |         LOG_DEBUG("Unable to get the name for this object of type " PRIu32, type);
        |         ^~~~~~~~~
  error: command 'gcc' failed with exit status 1
```
This came in via #1361 (https://github.com/cocotb/cocotb/pull/1361/files#diff-8baf84ef545a81134ec388a5b271c5b6R1131).


Discussion:
Does adding `#include <cstdint>` fix this?
Moving `<cinttypes>` to be the first include may help here.

It's also possible that your compiler is broken, and does not conform with the standard. Can you give your compiler version?
This is with both gcc 9.2.0 and 4.9.3.
Can you confirm that `--std=c++11` is being passed?
`__STDC_FORMAT_MACROS` is relevant here, but everything I found said that it is required to be set by default in C++11 mode.
Yes, it is being passed, and also moving ``<cinttypes>`` to the top fixes it.
I could open a PR, but there should be a good comment near the include line which I cannot write.
Can you try to bisect exactly which include `<cinttypes>` needs to be before? Eg, first reorder within:
```
#include <assert.h>
#include "VhpiImpl.h"
#include <limits>     // numeric_limits
#include <cinttypes>  // fixed-size int types and format strings
```
then assuming that `VhpiImpl.h` is the culprit, put `#include <cinttypes>` within the include stanzas of `VhpiImpl.h` until you find the offending header there, etc.

My hunch is that there's a bug here that we should report upstream.
Okay, I'm now at the point where it would need to go into ``cocotb/share/include/embed.h`` (as the 2nd include; last point where it works is in ``cocotb/share/lib/gpi/gpi_priv.h``), but this is compiled as C, and so doesn't work.
This sounds relevant (I'm on Redhat 6): https://github.com/MaskRay/ccls/issues/152
Right, exporting ``CPPFLAGS "-D__STDC_FORMAT_MACROS"`` works, but I have to stop here for today.

Resolving patch:
diff --git a/cocotb/_build_libs.py b/cocotb/_build_libs.py
--- a/cocotb/_build_libs.py
+++ b/cocotb/_build_libs.py
@@ -166,6 +166,9 @@ def _get_python_lib():
 _extra_cc_compile_args = ["-std=gnu99"] + _cc_warns
 _extra_cxx_compile_args = ["-std=c++11"] + _ccx_warns
 
+# Make PRI* format macros available with C++11 compiler but older libc, e.g. on RHEL6.
+_extra_cxx_compile_args += ["-D__STDC_FORMAT_MACROS"]
+
 def _get_common_lib_ext(include_dir, share_lib_dir, sim_define):
     """
     Defines common libraries.


Test patch: