Gcc is ignoring -Wno-unused-variable - c++

I'm using both gcc version 4.8.3 20140624 and gcc4.9.1 compiled from trunk. However, the flag doesn't seem to be ignored on online compilers. Here is the test program:
#include <iostream>
int main()
{
int i;
}
And shell output:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic -Wno-unused-variable -I./ -c -o test.o test.cpp
test.cpp:6:5: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc, char* argv[])
^
test.cpp:6:5: warning: unused parameter ‘argv’ [-Wunused-parameter]

With the following program
int main(int argc, char** argv)
{
int i;
}
with g++ -std=c++11 -O2 -Wall -Wextra -pedantic -Wno-unused-variable
You don't have warning for unused-variable i.
if you don't want warning for parameter argc, argv,
add also the compiler flag -Wno-unused-parameter
Live example

Related

Cygwin cmake problems on windows

I am trying to cmake on windows computer. I encountered following error in CMakeError.log
Run Build Command(s):/usr/bin/ninja.exe cmTC_ff288 && [1/2] Building C object CMakeFiles/cmTC_ff288.dir/CheckSymbolExists.c.o
FAILED: CMakeFiles/cmTC_ff288.dir/CheckSymbolExists.c.o
/usr/bin/cc -fdiagnostics-color=always -Wextra -Wall -Wno-expansion-to-defined -Wno-psabi -Wno-misleading-indentation -Wno-maybe-uninitialized -Wwrite-strings -Wundef -Wpointer-arith -Wmissing-format-attribute -Wformat-security -Wcast-align -Wno-tautological-compare -fno-strict-aliasing -fno-exceptions -o CMakeFiles/cmTC_ff288.dir/CheckSymbolExists.c.o -c CheckSymbolExists.c
CheckSymbolExists.c:2:10: fatal error: pthread_np.h: No such file or directory
2 | #include <pthread_np.h>
| ^~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
File /cygdrive/f/gtkwebkit/webkitgtk-2.34.1/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread_np.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_main_np
return ((int*)(&pthread_main_np))[argc];
#else
(void)argc;
return 0;
#endif
}
Does anyone know how to fix it? Since it is a temp file I could not modify this line of include

G++ raise compile errors instead of warnings for narrowing conversions

I want to get compile errors instead of warnings for this code:
#include <iostream>
int main(int argc, char ** argv)
{
float a = 1.3f;
int b = 2.0 * a;
std::cout << b << "\n";
}
If I compile it with:
g++ test.cpp -o test
I have no errors.
But If I compile the same code with:
g++ test.cpp -o test -Wconversion
I got the following warning:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:17: warning: conversion from ‘double’ to ‘int’ may change value [-Wfloat-conversion]
6 | int b = 2.0 * a;
I'm looking for a way to get compile errors instead of warnings only for this particular type of warning.
Obs.1: -Werror can make all warnings become errors but it is not what I am looking for
Obs.2: I'm using g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Use -Werror= to treat specific warnings as errors only:
g++ test.cpp -o test -Werror=conversion

Weird things happened when programming in c++20 with fmodules-ts

I have two source files: main.cxx and env.cxx. And main.cxx depends on env.cxx
If I compile them with
c++ -fmodules-ts -std=c++20 -Wall -g env.cxx main.cxx, then everything works well.
But if I compile them with
c++ -fmodules-ts -std=c++20 -Wall -g -c env.cxx
c++ -fmodules-ts -std=c++20 -Wall -g -c main.cxx
c++ -fmodules-ts -std=c++20 -Wall -g env.o main.o
I get segmentation fault.
I do not know whether it is a gcc bug or not, because it works well again if I rewrite them in c++17 style, removing the module declarations.
My compiler is gcc 11.1.0 (The newest version up to now)
Here is env.cxx:
module;
#include <cstdlib>
#include <string>
#include <string_view>
#include <stdexcept>
export module env;
export namespace env
{
std::string get_env(const char* name)
{
char* p = ::getenv(name);
if (p == nullptr)
throw std::runtime_error("environment variable not found");
return std::string(p);
}
// These two functions are useless this time.
std::string get_env(const std::string& name)
{
return get_env(name.data());
}
std::string get_env(std::string_view name)
{
std::string s{name};
return get_env(s.data());
}
}
And main.cxx
module;
#include <iostream>
#include <fstream>
import env;
export module main;
int main(int argc, char** argv)
{
std::cout << env::get_env("HOME") << std::endl;
return 0;
}
Can you reproduce this?
Update 2021.06-19 15:44
In fact the two compiling commands above both work well, and the way that produces segfault is actually:
c++ -fmodules-ts -std=c++20 -Wall -g -c env.cxx
c++ -fmodules-ts -std=c++20 -Wall -g -c main.cxx
c++ -fmodules-ts -std=c++20 -Wall -g main.o env.o
Note that the place of main.o and env.o has been changed.

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.

Toggle warnings with fsyntax-only parameter

I have a question concerning compilation between g++ and gcc.
If I write this code:
int main(int args, char* argv[]){
return 0;
}
and compile it with: g++ -fsyntax-only -Wall -Wextra -Werror
whether the file has a .c or a .cpp extension it won't complain about unused paramaters(args and argv).
The -Wunused option will only work if I compile the file with a .c extension and gcc.
So my question is: is it possible to enable warnings with fsyntax-only parameter in all other cases ?
Thank you in advance for any reponse
No, it is not, -fsyntax-only only checks the syntax. – nos
Options to Request or Suppress Warnings:
-fsyntax-only Check the code for syntax
errors, but don’t do anything beyond that.
This anomaly appears to have been a g++ compiler bug. The OP's
observations are confirmed with g++ 5.4 but g++ 6.3 gives
the expect warnings:
$ g++-6 -fsyntax-only -Wall -Wextra -Werror test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:1:14: error: unused parameter ‘args’ [-Werror=unused-parameter]
int main(int args, char* argv[]){
^~~~
test.cpp:1:31: error: unused parameter ‘argv’ [-Werror=unused-parameter]
int main(int args, char* argv[]){
^
cc1plus: all warnings being treated as errors