Home > Enterprise >  clang unable to recognize 'nullptr' without flag
clang unable to recognize 'nullptr' without flag

Time:12-29

I am running:

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

when I run: clang *.cpp -o test, I get:

    identifier = nullptr;
                  ^
1 error generated.

however, when I run: clang -std=c 11 *.cpp -o test, I get no errors.

Does anyone else have this issue with the latest version of clang?

CodePudding user response:

Apple clang 14.0.0 defaults to C 98.

For example with this source file:

long version = __cplusplus;
void *p = nullptr;

You can see this behavior:

$ clang   -E nullptr.cpp | grep -v '^#'
long version = 199711L;
void *p = nullptr;

$ clang   -c nullptr.cpp           
nullptr.cpp:2:11: error: use of undeclared identifier 'nullptr'
void *p = nullptr;
          ^
1 error generated.

$ clang   -c -std=c  11 nullptr.cpp

The solution is to explicitly request a language standard of C 11 or newer via -std=c XY, as shown above.

  • Related