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

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).

Related

Compiling multiple C++ files in command line?

I'm doing a tutorial right now that doesn't make it to clear on how to do this.
I have two files, int2.cpp which has my main and int1.cpp which is a function (int2 calls on int1) I know they will work but how would one type it into the command line? tutorial says g++ int2.cpp int1.cpp -o int2.cpp, but it says " g++ is an illegal command"
I'm using DOSbox 0.74.
I compile things with tcc sorry but it says -o isn't a command line option
I compile things with tcc sorry but it says -o isn't a command line option
TurboC++ is an obsolete compiler (for an obsolete variant of C++); don't use it.
TinyC (e.g. the tcc command) is a compiler for C, not for C++.
C and C++ are different languages, and you want to learn at least C++11 (since older standards of C++ are obsolete and very different, it is not worth learning them in 2017).
So get and use a free software C++11 compiler like GCC or Clang. BTW both are easily available on most Linux distributions (which I recommend you to use).
Of course you'll want to compile with warnings and debug information, so use
g++ -Wall -Wextra -g with GCC and clang++ -Wall -Wextra -g with Clang.
BTW, you probably want to compile several translation units into a single executable binary. This often involves a linking step (i.e. running the linker on several object files, also using the g++ command). Consider learning to use some build automation tool like GNU make (which has a lot of builtin rules to help in doing that).
g++ is an illegal command means the compiler is not installed on your path. The syntax is (almost) right, you likely just don't have gcc installed.
g++ int2.cpp int1.cpp -o int2.exe

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

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.

Passing CXXFLAGS to make has no effect

I am trying to compile some software with make on Ubuntu 16.04. However, it fails with the error:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file
requires compiler and library support for the ISO C++ 2011 standard. This
support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
A quick websearch shows that compiler options can be passed to make either by
export CXXFLAGS="-std=c++11"
or
make CXXFLAGS="-std=c++11"
None of those options have worked. I have also tried with deleting everything in the build folder and starting from scratch. The error still persists.
How can I pass CXXFLAGS to make ?

Including C++11 header file with in C++ source file

I'm trying to figure out how to make use of C++11 headers in my C++ project. Specifically I'm trying to use FakeIt mocking framework (https://github.com/eranpeer/FakeIt) for unit testing my legacy application which is built with C++.
The FakeIt page says it is supported for both C++11 & C++ projects but when I build my application with the header from the library it complains about not able to resolve the C++11 symbols.
gtest/include/fakeit.hpp:28:25: error: unordered_set: No such file or directory
gtest/include/fakeit.hpp:29:17: error: tuple: No such file or directory
gtest/include/fakeit.hpp:32:18: error: atomic: No such file or directory
I'm new to C++, so would appreciate any pointers here thanks.
C++11 support is often not enabled by default. Compile your program with the -std=c++11 argument. For example:
g++ -std=c++11 -I/path/to/libray file.cpp
Also, do make sure your compiler actually supports C++11, it should complain if it doesn't understand -std=c++11. Here is a list of C++11 features supported by various g++ versions.

Warning while compiling libraries with both C and C++ files

I am working on the project which contains both C++ files and C files. In Code::Blocks, I have selected the following checkbox -std=c++11 in project build options.
Then get the following warning.
||warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C [enabled by default]|
If I also check the checkbox of -std=c99,
I will get an additional warning as the following.
||warning: command line option ‘-std=c99’ is valid for C/ObjC but not for C++ [enabled by default]|
Is there a way to work around this? Can I set the -std=c++11 flag for only the C++ files and -std=c99 for the C files?
I believe you are having this problem because you have enabled the -std=c++11
option in the Global compiler settings for GCC. I know you say you have enabled it
in "project build options", but enabling it in global compiler options is what causes
this behaviour.
It a bug in C::B that this global option is described as:
Have g++ follow the C++11 ISO C++ language standard
when in fact it will be applied to both g++ and gcc.
However you are probably ill-advised to set a C++ language standard option
in the global compiler options, even if it would only apply to g++. That
way, you won't ever be able to build any C++ projects that conform to
an earlier standard than C++11 but not C++11.
You should go back to Global compiler settings, click Reset defaults and
OK out. Then set the -std=c++11 compiler option (and any other non-default
options that you want) at the project level:
Right-click on your project in the workspace tree-view and open Build options.
Ensure the Selected compiler is GNU GCC Compiler and select the
Compiler settings -> Compiler flags tab. There you will see the
same list of checkboxes for compiler options that you saw in Global compiler settings.
Enable the -std=c++11 option and OK out.
Now, any .c files in your project will automatically be compiled with gcc,
without any g++-specific flags, and .cpp files will automatically be
compiled with g++, with -std=c++11 and any other g++ options you have
chosen.
Try reading this:
Using Global Compiler Variables
It describes different compiler variables, and build options. It is most likely there that you will need to either make a change or find out what variables are used specifically for each C and C++ targets that are compiled.