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.
Related
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 need to set the compiler options in my makefile project in eclipse.
I followed this instruction:
http://www.eclipse.org/forums/index.php/mv/msg/282618/787571/
where the last part states:
Obviously make sure your rule includes "-std=c++0x" as a compiler option.
But there are no "tool-settings" in the project properties of my makefile project.
Is there another way to set the compiler options for a makefile project?
Thanks.
If it's a makefile project, then the makefile specifies how to build it, not the Eclipse settings.
Exactly how to specify the compiler options depends on how the makefile is written, but typically they are in a variable called CXXFLAGS, since that's what the default C++ build rule uses.
Unless you need to support outdated compilers, I suggest specifying c++11 rather than c++0x.
I have a project containing a mixture of C and C++ source. It currently builds with GCC on OS X. The project has bespoke build scripts which invoke the gcc command to compile both the C and C++ source, and separately invoke the linker.
I am now trying to get it building with Clang.
Invoking clang does compile the source files correctly; it distinguishes between .c and .cpp source files, and compiles for the appropriate language in each case. I have problems at link time, though. When the linker is invoked as clang, the C++ runtime libraries are not linked in, causing a build error due to missing symbols.
I can link successfully when I set clang++ as the build tool, but this then causes compile-time errors and warnings; it really doesn't like compiling C source with the C++ compiler.
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
...
/usr/include/stdio.h:250:49: error: redefinition of parameter 'restrict'
I have to specify a single tool for the build scripts to use as the compiler/linker, so I need to do a simple substitution of clang in place of gcc.
Is there any way I can persuade clang (not clang++) to link with the C++ runtime libraries?
Options such as -stdlib=libc++ don't work.
You should just be able to use the normal linker flag, same as you'd do for gcc: clang -lc++ or clang -lstdc++ depending on which implementation you want. (and you should want libc++)
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