GNU C++: unique_ptr.h no such file or directory - c++

I am cross compiling some code from Windows on Linux machine (Kubuntu 16.05),g++ 5.4.0 64bit. Using Code Lite IDE as a dev env.
I have got several lines of code where I init unique pointer with
std::make_unique
The compiler complains with the error:
error: 'make_unique' is not a member of 'std'
I tried to add <memory> as well as <unique_ptr.h> to the header. Then the compiler complains that it can't find <unique_ptr.h> file. Interestingly,when I click to open the file from within the editor it is found and opened. The file is located in /usr/include/c++/5/bits/unique_ptr.h
I made sure that the compiler version that builds the code is indeed 5.4, so I don't understand why it doesn't support unique_ptr out of the box.I make sure to enable C++11 and C++14 flags:
-g;-O0;-std=c++14;-std=c++11;-Wall
Also, in the includes I add /user/include
What am I missing here? Do I have to include in the project the /usr/include/c++/5/bits/ directory explicitly?

Try these flags:
-g -O0 -std=c++14 -Wall. Note that semicolon is not needed for separating flags.
Compiler will take the latest entry of -std so you are effectively compiling with C++11 but not C++14.

As you can see HERE enabling -std=c++11 after a newer standard disables the first declared standard. Enabling only C++14 is enough.

C++11 introduced std::unique_ptr, but there was no std::make_unique (this broke the "symmetry" of shared_ptr/make_shared).
They fixed that in C++14, adding std::make_unique.
So, if you compile your code in C++11 mode, you can't use std::make_unique.
As others already pointed out, you need to set the latest C++ standard with the -std compiler option; in this case, it's -std=c++14 to enable also std::make_unique.

Related

Enabling the -std=c++0x or -std=gnu++0x compiler options

I try to compile a particular source code written in C++, and I do not get an executable file. Instead, I get the following message from the terminal:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/array:35,
from cpptesting.cpp:6:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
What does this message mean, and how can I fix the system? I have tried entering the directory 'include', but could find no 'c++' option. Does anyone have advice?
How do you compile it? if you are using console command like gcc .\cpptesting.cpp, you should just add an option to it:
gcc -std=gnu++0x .\cpptesting.cpp
If you are compiling more complex program with make, check a makefile.
By the way, you are using pretty old gcc version, in the more recent versions this standard is enabled by default(IIRC).

Stop Apple clang from adding Xcode paths to standard include paths

Apple's clang appears to magically include Xcode header files which conflict with my code. For example, they define stoi() while standard C++ headers do not. Clang does not do this on other platforms, e.g. FreeBSD.
Example of error on Mac OS:
$ clang -I. -I../../../include -c compile.cpp
compile.cpp:949:18: error: call to 'stoi' is ambiguous
arg.iValue = stoi(current.token);
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:4019:37: note:
candidate function
_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* ...
^
This compile works fine on FreeBSD.
The problem is because in addition to the standard C++ headers in /usr/include/c++, Apple clang is including the headers in the Xcode application tool chain. Is there some way to turn that off without using the -nobuiltininc flag?
The default C++ library for newer versions of clang on OSX is libc++, which is a pretty full implementation of the C++11 standard. The headers are all C++11, and there are no conditional sections to remove methods that should only be available in that standard (e.g. attempting to compile using --std=c++03 doesn't have the desired effect.
The version of stdlib++ that's present in the apple environment doesn't have this issue as (1) it's not a complete implementation of the C++11 standard, and (2) you can actually compile for older c++ standards.
As a result, if you compile using --stdlib=libstdc++, you are compiling against the GNU standard library headers and library which don't have the issue with the definition of std::stoi (and others routines if I'm interpreting the question correctly).
Bear in mind, though, that you have to compile every c++ element of your project with this flag, as otherwise you end up with code that has been compiled with different standard libraries and they will not link together.
The real problem, though, is that if you start using C++11 features and start compiling explicitly with --std=c++11, then you're probably going to encounter this problem at some point in the future with gcc as well. Newer versions of the compiler support more of the C++11 standard, and so would define this routine in the headers and you're back to square one.

How to activate c++11 support on MinGW using CMake?

I've installed latest version of MinGW/MSYS environment. Now I try to activat c++11 standard support. I've added to my cmake script:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
If I try to build a simple test. I got the following compile failure:
error: 'shared_ptr' is not a member of 'std'
It seems to me that c++11 support is not set. If I remove the std::shared_ptr declaration from my code and try to compile some code of the chrono library it works. This confuses me.
Why some c++11 libraries are supported but core elements like shared_ptr fails?
Do I need to do something extra to enable smart pointers or they are just not there?
You can/should (also) use -std=c++0x so as to not enable g++ extension.
Make sure to #include <memory>.

How can I update code::blocks to use TDM-GCC?

I need to use unique_ptr in my C++ assignment.
I downloaded a new compiler, TDM-GCC-4.7.1, and installed it. Then I changed the directory of GNU GCC Compiler to the installation path in option: Setting->Compiler...->Toolchain Executable.
But it doesn't work. When I define a unique_ptr. A error would occur: "unique pointer is not a command of 'std' "
The reason of using smart pointer is to provide strong exception safety, which is also a requirement of this assignment. I just need to use this new feature of C++11... Plus, the OS I use is Window 7.
Thanks!
In CodeBlocks you can set C++11 mode either in project compiler settings or global compiler settings. Since you may be using it more often, here is how you change it globally:
Go to Settings -> Compiler -> (There should be list of options here, in Compiler flags) -> Select "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"
Here is how to change it for a single project:
Go to Project -> Build options -> (There should be list of options here, in Compiler flags) -> Select "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"
Download the latest tdm-gcc http://tdm-gcc.tdragon.net
Settings->Compiler, Selected Compiler [GNU GCC Compiler] Copy and Create a new configuration [GNU GCC Compiler 4.9.2]
Detail configuration
Set the new compiler configuration as default by click Set as default
Change Compiler settings to having c++11.
PS: code::blocks ver 13.12, tdm-gcc 4.9.2
Remember to #include <memory>
Add -std=gnu++0x or -std=c++11 compiler flags......whichever works
That might be a late answer, but for anyone who's asking for that, changing the option "linker for dynamic libs" from gcc to g++ works for me

Supressing warnings for a codeblock using gcc 4.1.2?

I have a section of code that uses a Boost library that results in a lot of these type of warning messages:
class x has virtual functions but non-virtual destructor
Using g++ 4.1.2, how do I temporarily disable warnings for this codeblock then enable them again after it.
Everything I try (pragma gcc diagnostic, etc) seems to only be supported by a later version of g++.
How do I suppress just these warnings?
Take a look at the -Wno-non-virtual-dtor.
You can restrict this option to affect only certain files.
Can you move your "bad" code to separate c++ file? If so, turn this option only for that file.