Hide clang warnings using makefile - c++

I have a makefile with the following lines:
CXX=clang++
CXXFLAGS=-g -std=c++11 -pedantic -Wno-c++11-extensions
I am still getting this:
warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
Is there a way that I can hide the warning using the makefile?

POST QUESTION EDIT
The contents in the original post by rsay3 insufficient to being answered properly. What is not stated in the post is the correct approach which leaves us to only deduce that the problem is actually elsewhere, an elsewhere which is not stated in the question.
If -std=c++11 is used we should not get any warnings about a certain feature only being available in C++11 (since we are already compiling it as C++11), and the redudant -Wno-c++11-extensions is correct (even though uneccessary).
PRE QUESTION EDIT
-W... and an added warning-specifiers enables a certain warning, if you'd like to supress a diagnostic you will have to use -Wno-..., as in the below example:
CXXFLAGS=-g -std=c++11 -pedantic -Wno-c++11-extensions
// main.cpp
int main () {
auto val = 123;
}
% clang++ -Wall -std=c++03 main.cpp
foo.cpp:2:3: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto val = 123;
foo.cpp:2:8: warning: unused variable 'val' [-Wunused-variable]
auto val = 123;
% clang++ -Wall -Wno-c++11-extensions -std=c++03 main.cpp
foo.cpp:2:8: warning: unused variable 'val' [-Wunused-variable]
auto val = 123;

Related

g++ compile options with -std=c++14 compile error showing Werror=c++11-compat

My environment Arch Linux, gcc 7.2
I'm learning C++ and I'm using keyword constexpr to define a constant, while compile, it give me an error message
error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
I can compile my program with default g++, but cannot compile with -std=c++14 and -Werror
The command I'm using is:
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
I believe the -Werror option caused the issue. but what is the issue? can someone tell me please?
#include <iostream>
int main() {
constexpr double yen_dollar = 0.107;
std::cout << yen_dollar << std::endl;
return 0;
}
test.cpp:4:5: error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
constexpr double yen_dollar = 0.107;
^~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: ‘constexpr’ was not declared in this scope
test.cpp:5:16: error: ‘yen_dollar’ was not declared in this scope
std::cout << yen_dollar << std::endl;
From the GCC documentation §3.4 Options Controlling C Dialect, one can read:
-ansi
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
Since, you compiled with
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
-ansi overwrites -std=c++14 with -std=c++98. This is why constexpr is not recognized.
Solution: get rid of the -ansi flag.

Which compilation flags should I use to avoid run time errors

Just learned here that -Wsequence-point comiplation flag will pop a warning when the code can invoke UB. I tried it on a statement like
int x = 1;
int y = x+ ++x;
and it worked very nicely. Until now I have compiled with gcc or g++ only using -ansi -pedantic -Wall . Do you have any other helpful flags to make the code more safe and robust?
As alk summed up, use these flags:
-pedantic -Wall -Wextra -Wconversion
First, I think you don't want to use the -ansi flag, as suggested in Should I use "-ansi" or explicit "-std=..." as compiler flags?
Secondly, -Wextra seems to be quite useful too, as suggested in -Wextra how useful is it really?
Thirdly, -Wconversion seems also useful, as suggested in Can I make GCC warn on passing too-wide types to functions?
Fourthly, -pedantic is also helpul,
as suggested in What is the purpose of using -pedantic in GCC/G++ compiler?.
Lastly, enabling -Wall should be fine in this case, so I am pretty doubtful about what you said.
Example with gcc:
Georgioss-MacBook-Pro:~ gsamaras$ cat main.c
int main(void)
{
int x = 1;
int y = x+ ++x;
return 0;
}
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:4:16: warning: unsequenced modification and access to 'x' [-Wunsequenced]
int y = x+ ++x;
~ ^
main.c:4:9: warning: unused variable 'y' [-Wunused-variable]
int y = x+ ++x;
^
2 warnings generated.
Georgioss-MacBook-Pro:~ gsamaras$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.38)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Example with g++, same version:
Georgioss-MacBook-Pro:~ gsamaras$ cp main.c main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp
main.cpp:4:16: warning: unsequenced modification and access to 'x'
[-Wunsequenced]
int y = x+ ++x;
~ ^
main.cpp:4:9: warning: unused variable 'y' [-Wunused-variable]
int y = x+ ++x;
^
2 warnings generated.
Relevant answer of mine, that Wall saves the day once more with a similar problem.

How to enable narrowing warnings in CodeBlocks?

I'm writing c++ program using codeblocks IDE
int main()
{
int i =0;
int f = 3.14;
i = f; //must give me a warning message, possible loss data.
}
Why the compilation not show a narrowing warning message?
How to enable that?
Note: I have fixed my compiler options as -std=c++11 -Wall
in the other compiler options put -Wconversion
( code::block 16 )
for:
int i =0;
int f = 3.14;
i = f;
warning: conversion to ‘int’ alters ‘double’ constant
value [-Wfloat-conversion]
Some useful warning that I use always:
-Wall -Weffc++ -Wextra -pedantic -Wfatal-errors -pedantic-errors

C++ initialize array size using constant variable [duplicate]

int a;
cin >> a;
int ints[a];
Why does this not throw any kind of warning while compiling? How do I know when this array thing is actually using the heap or the stack?
g++ -std=c++11 -Wall *.cpp -o main
ISO C++ disallows the use of variable length arrays, which g++ happily tells you if you increase the strictness of it by passing it the -pedantic flag.
Using -pedantic will issue a warning about things breaking the standard. If you want g++ to issue an error and with this refuse compilation because of such things; use -pedantic-errors.
g++ -Wall -pedantic -std=c++11 apa.cpp
apa.cpp: In function ‘int main(int, char**)’:
apa.cpp:8:13: warning: ISO C++ forbids variable length array ‘ints’ [-Wvla]
int ints[a];
^
apa.cpp:8:7: warning: unused variable ‘ints’ [-Wunused-variable]
int ints[a];
^

g++ variable size array no warning?

int a;
cin >> a;
int ints[a];
Why does this not throw any kind of warning while compiling? How do I know when this array thing is actually using the heap or the stack?
g++ -std=c++11 -Wall *.cpp -o main
ISO C++ disallows the use of variable length arrays, which g++ happily tells you if you increase the strictness of it by passing it the -pedantic flag.
Using -pedantic will issue a warning about things breaking the standard. If you want g++ to issue an error and with this refuse compilation because of such things; use -pedantic-errors.
g++ -Wall -pedantic -std=c++11 apa.cpp
apa.cpp: In function ‘int main(int, char**)’:
apa.cpp:8:13: warning: ISO C++ forbids variable length array ‘ints’ [-Wvla]
int ints[a];
^
apa.cpp:8:7: warning: unused variable ‘ints’ [-Wunused-variable]
int ints[a];
^