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
Related
I am using Eclipse CDT 3.8.1 with GCC Cross Compiler 8.6.0. I know how to change the C++ standard in the project properties, but I don't know what standard is used by default.
I know the default is not C++11 and the Eclipse language support page doesn't mention C++03, so I suspect the default is C++98. However, Eclipse CDT must support C++03 because adding the compiler flag -std=c++03 doesn't cause any errors.
https://www.eclipse.org/community/eclipse_newsletter/2017/april/article3.php
What is the default standard when I create a new project?
Eclipse's parser itself doesn't have a notion of a C++ standard mode. It will recognize all the C++ features that have been implemented in its parser (which, as of writing this, is all C++98 and C++11 features, some (but not all) C++14 features, and a handful of C++17 features).
However, standard library headers often contain sections that are conditional on macros denoting the C++ standard version (e.g. #if __cplusplus >= 201103 is a common check for "C++11 or later"). To determine the value of these macros, Eclipse invokes the compiler specified in the project's toolchain to discover built-in macros. The discovered value of e.g. the __cplusplus macro will depend on what standards mode the compiler runs in for this invocation.
The flags to this compiler invocation are specified in the project properties, as you mentioned. If you don't provide a flag there, the compiler will use whatever its default mode is. I believe GCC has been using -std=c++14 as the default from GCC 6 onwards. (Though I don't quite know what "GCC Cross Compiler 8.6.0" is. According to the GCC website, the latest version is 8.1.)
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.
I use two C++ compilers / IDEs. (1) g++ in Code::Blocks and (2) Visual C++ in Visual Studio.
I have some question on how to set up the compiler in the way I want it to be.
Code::Blocks: I want to make G++ follow standard C++. For example:
int x;
cin >> x;
int arr[x];
...wouldn't work in standard C++.
In standard C++, one has to use dynamic allocation. For example, the Visual C++ compiler would reject this code. However, this code would work in Code::Blocks.
How do I make g++ reject this code?
Also, how do I tell g++ and Visual C++ 2013 to follow and only follow C++11 standard? It seems to me the default one is still C++98/03.
Compiler options for standard-conformance.
With g++ you want, at minimum,
-std=c++11 for the C++11 standard.
-pedantic to warn about use of language extensions.
-Wall -Wextra to up the warning level to reasonable.
With Visual C++ you want, at minimum,
/nologo to turn off a verbose version and copyright message,
/EHsc to turn on exception support,
/GR to turn on RTTI support, Run Time Type Information such as dynamic_cast,
/W4 to up the warning level to reasonable.
With Visual C++ there is no special option to indicate the C++ standard version.
How to preset options for command line use.
MinGW g++ generally (one exception is the Nuwen distribution) defaults to producing executables that rely on DLLs that reside in the mingw\bin directory, which means that you generally need that in your PATH environment variable.
This means that it's not such a good idea to add options via a batch file or shortcut or command intepreter macro. Instead use option -dumpspecs and redirection to create a specs file (with that name) in the directory specified by this Windows command:
g++ --print-search-dirs | find "install:"
Read up on how to edit that file, then fix it. ;-)
You might want to set the CPATH environment variable to your general include directory.
With Visual C++:
Compilers options can be preset in the CL environment variable.
Include paths in the INCLUDE variable.
Library paths in the LIB variable.
Linker options can be preset in the LINK variable.
For the linker options you may want to have /entry:mainCRTStartup to support ordinary standard main startup function also when producing a GUI subsystem executable. However this conflicts a little with MFC. It's been many years since I did anything MFC so if you want details or solution please post a separate question which others can answer.
How to set options in each IDE (Code::Blocks, Visual Studio).
This is as simple as reading each IDE's documentation. ;-)
Note that options can be set both globally and per project.
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.
I downloaded boost 1.53 and extracted to Users/user/Desktop/boost_1_53_0
I successfully built boost as static libraries following instructions at: http://libcinder.org/docs/v0.8.5/_cinder_boost.html
I changed XCODE's "Build Settings" "Header Search Paths" to /Users/user/Desktop/boost_1_53_0 as well as the "Library Search Paths" to Users/user/Desktop/boost_1_53_0/stage/lib
Then I build and get errors:
The first error I'm getting is "has_binary_operator.hpp" Parse issue Expected member name or ';' after declaration specifiers at the line:
template < typename Lhs, typename Rhs >
struct operator_exists {
...
Does anyone have tips for what the build settings should be? The project contains some files that are cocoa/objective C (.m) and others that are C++(.mm) using boost. I'm completely new to trying to use boost under OSX mountain lion and XCODE 4.6.1.
I'm a little late for this one, but in case anybody else has this issue...
I had this problem with boost in projects using Objective C. I would get parse and semantic issues in has_binary_operator.hpp and lexical_cast.hpp.
A quick fix was inserting "#undef check" before including boost headers in Objective C source files, as it would appear "check" is a reserved word in Objective C.
You can make sure to set compiler type is GNU98 or something like that, XCODE4 default compiler may be C++11 compliant
I'm using boost from XCODE and I agree with fatih_k. I have all my c and c++ language settings set to GNU99 and GNU++98. I also have the 'compile sources as' option set to 'According to file type'.
I found that the projects I create always defaults to using Objective C, so maybe just go through your project setting and make sure you have everything configured for C++ and not Objective C.
I have not tried the C++11 options with boost yet.
The Xcode 4.x default compiler is apple llvm, and it uses libc++ as default. You can change the settings in Xcode
To change the stdlib as libc++
a. Select the target from Xcode and go to the BuildSettings tab.
b. Go to Apple LLVM compiler 4.2 - Language option
Change the option as libstd from libc++
To change the compiler
a. Select the target from Xcode and go to the BuildSettings tab.
b. Go down Build options
Change the option C/C++/Objective C - Default option will be "Apple LLVM compiler 4.2" Change it to g++ 4.2