How do I compile a program that uses nullptr?

I'd like to use nullptr instead of NULL, but when I compile using G++:

 
g++ main.cpp renderer.cpp -I c:\some\includedir -o game


I get the following compiler warning (and error):

1
2
3
Warning: "non-static data member initializers only available with -std=c++11 or -std=gnu+11"

"Error: 'nullptr' was not declared in this scope"


I researched and understand that nullptr was added to C++ in 2011 and I need to use the suggested flag (-std=c++11 or std=gnu+11), but if I use either of them like:

 
g++ -std=c++11 main.cpp renderer.cpp -I some/includedir -o game


...I get a Windows Error Alert box saying:

 
"The Procedure entry point __gxx_personality_v0 could not be located in dynamic link library"


..and the program fails to compile. Does anybody have any idea what I'm doing wrong or how to properly compile a C++ program using the latest version of C++ so nullptr works?

What I'm using:
- Compiling program in the Windows command line
- editing code in VIM
- OS: Windows 8.1
- GCC Version: 4.9.3

Thanks!
Last edited on
Hi,
> How to properly compile a C++ program using the latest version of C++ so nullptr works
The word nullptr is normally supposed to have a special blue color like that of a keyword. However, if it does not happen in your case, you may try this :

#define nullptr 0
Does that help? :)
No, because it is the wrong answer.

The warning is very explicit about what is wrong: you need to use std=c++11 or -std=gnu+11 at the command line.


Since the OP did that, which fixed the nullptr problem,
He then had a different problem when he tried to run his code:

 
"The Procedure entry point __gxx_personality_v0 could not be located in dynamic link library"

This is because of of a DLL conflict. See http://stackoverflow.com/a/20455257/2706707 for the solution.

Hope this helps.
Duoas, almost jumped out of the seat when the program compiled successfully. That worked perfectly, thank you very much!
@Duoas
I simply think nullptr is too much of a simple concept. Why do they have to go that far as to use std = c++11 or std=gnu + 11 at the command line so that the complier supports that trivial keyword?
Last edited on
@hashbrown

You should always compile with a high level of warnings - warnings are your friend in helping to write good code. At least always use these:

g++ -std=c++11 -Wall -Wextra -pedantic-errors -o MyProgName *.cpp

Despite these seemingly comprehensive settings, there are still some handy warnings which are not turned on:

http://www.cplusplus.com/forum/general/183731/#msg899203

If possible, try to always have the latest compilers - gcc is at version 6.1 currently. Not sure what the story is in terms of using them with Windows, I gather one has to use whatever the latest version of MinGW is. I am a Linux guy, so I am not up with all the details on that :+)

Edit: Apparently MinGW GCC 6.10 for Windows does exist :+)

Good Luck !!

@closed account 5a8Ym39o6 (696)

#define nullptr 0

No, that is wrong. nullptr was invented to avoid the problems encountered with using NULL with a pointer type. What you have done is provide a definition of NULL with a different name. Note that nullptr is a prvalue.

nullptr has the type std::nullptr_t Use of the c++11 standard or greater is necessary because those things are defined there.

http://en.cppreference.com/w/cpp/language/nullptr
http://en.cppreference.com/w/cpp/types/nullptr_t
http://en.cppreference.com/w/cpp/types/NULL

Last edited on
IIRC the latest version of GCC defaults to C++14 or something...
@Duaos

Yep, another good reason to upgrade to the latest version, one could leave out the -std= unless they wanted to do C++17 .... in which case the normal method of -std=c++17 or -std=c++1z plus some other flags for concepts etc.
Topic archived. No new replies allowed.