1

I'm trying to compile cpputest with MinGHW-w64 and it's failing because it can't find nullptr_t. The function that is failing is cpputest\src\CppUTest\SimpleString.cpp:StringFrom

SimpleString StringFrom(const nullptr_t value)
{
    (void) value;
    return "(null)";
}

I tried to manually compile this file with

C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\g++.exe -DCPPUTEST_HAVE_STRDUP=1 -DHAVE_CONFIG_H -D_TIMESPEC_DEFINED=1 @CMakeFiles/CppUTest.dir/includes_CXX.rsp -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorNewMacros.h" -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h" -std=c++11 -Wall -Wextra -pedantic -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wsign-conversion -Wno-padded -Wno-long-long -Woverloaded-virtual -Wno-old-style-cast -Wno-c++14-compat -O2 -g -DNDEBUG -o CMakeFiles\CppUTest.dir\SimpleString.cpp.obj -c C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp

It fails with

C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:565:31: error: 'nullptr_t' does not name a type
 SimpleString StringFrom(const nullptr_t value)
                               ^~~~~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:565:31: note: 'nullptr_t' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'?
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:32:1:
+#include <cstddef>

C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:565:31:
 SimpleString StringFrom(const nullptr_t value)
                               ^~~~~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:565:14: error: redefinition of 'SimpleString StringFrom(int)'
 SimpleString StringFrom(const nullptr_t value)
              ^~~~~~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:479:14: note: 'SimpleString StringFrom(int)' previously defined here
 SimpleString StringFrom(int value)
              ^~~~~~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp: In function 'SimpleString StringFrom(cpputest_longlong)':
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:576:29: warning: unknown conversion type character 'l' in format [-Wformat=]
     return StringFromFormat("%lld", value);
                             ^~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:576:29: warning: too many arguments for format [-Wformat-extra-args]
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp: In function 'SimpleString StringFrom(cpputest_ulonglong)':
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:581:29: warning: unknown conversion type character 'l' in format [-Wformat=]
     return StringFromFormat("%llu", value);
                             ^~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:581:29: warning: too many arguments for format [-Wformat-extra-args]
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp: In function 'SimpleString HexStringFrom(cpputest_longlong)':
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:586:29: warning: unknown conversion type character 'l' in format [-Wformat=]
     return StringFromFormat("%llx", value);
                             ^~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:586:29: warning: too many arguments for format [-Wformat-extra-args]
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp: In function 'SimpleString HexStringFrom(cpputest_ulonglong)':
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:591:29: warning: unknown conversion type character 'l' in format [-Wformat=]
     return StringFromFormat("%llx", value);
                             ^~~~~~
C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:591:29: warning: too many arguments for format [-Wformat-extra-args]

I tried the naive solution of including cstddef:

#include <cstddef>

SimpleString StringFrom(const nullptr_t value)
{
    (void) value;
    return "(null)";
}

But that fails with the same error:

C:\git\tdd\cpputest\src\CppUTest\SimpleString.cpp:568:31: error: 'nullptr_t' does not name a type
 SimpleString StringFrom(const nullptr_t value)

This error doesn't make any sense as I'm including cstddef.

This problem is related with this question.

Any help is appreciated!

Leonardo
  • 1,533
  • 17
  • 28
  • 2
    Are you sure the file includes ``? If you make a [mre] of `#include void foo(nullptr_t) {} int main() { foo(nullptr); }` does that compile? – NathanOliver Dec 04 '19 at 18:51
  • Edited the question. Yes, I'm positive. Unless I'm hallucinating and that `#include ` doesn't do what I think it does. – Leonardo Dec 04 '19 at 18:56
  • Does the minimal example compile or do you get an error for that as well? – NathanOliver Dec 04 '19 at 18:56
  • It also fails. If I add `using namespace std;`, then it works. – Leonardo Dec 04 '19 at 19:01
  • 2
    Ah, yes. `nullptr_t` is not guaranteed to be outside `std::`. You can add `using std::nullptr_t` to the top of the file to fix that. – NathanOliver Dec 04 '19 at 19:04
  • 2
    Or you can just use `std::nullptr_t` like you do with every other std type ever... –  Dec 04 '19 at 19:06
  • Not my code, it's from cpputest. This seems a fairly basic thing to be missing. Shouldn't CMake take care of this? – Leonardo Dec 04 '19 at 19:22

1 Answers1

0

The fix is simple enough:

SimpleString StringFrom(const std::nullptr_t value)
{
    (void) value;
    return "(null)";
}

Thanks for NathanOliver- Reinstate Monica for helping to figure this out.

Leonardo
  • 1,533
  • 17
  • 28