Questions tagged [gcc-warning]
GCC is the GNU Compiler Collection, encompassing the gcc C compiler and the g++ C++ compiler, among others. It has powerful warning facilities that can reveal coding errors in C and C++ code.
gcc-warning
842
questions
327
votes
23
answers
330k
views
How do I best silence a warning about unused variables?
I have a cross platform application and in a few of my functions not all the values passed to functions are utilised. Hence I get a warning from GCC telling me that there are unused variables.
What ...
276
votes
14
answers
288k
views
How can I suppress "unused parameter" warnings in C?
For instance:
Bool NullFunc(const struct timespec *when, const char *who)
{
return TRUE;
}
In C++ I was able to put a /*...*/ comment around the parameters. But not in C of course, where it gives ...
259
votes
8
answers
107k
views
How can I turn on (literally) ALL of GCC's warnings?
I would like to enable—literally—all of the warnings that GCC has. (You'd think it would be easy...)
You'd think -Wall might do the trick, but nope! You still need -Wextra.
You'd think -Wextra might ...
198
votes
2
answers
14k
views
What is &&& operation in C
#include <stdio.h>
volatile int i;
int main()
{
int c;
for (i = 0; i < 3; i++)
{
c = i &&& i;
printf("%d\n", c);
}
return 0;
}
The ...
172
votes
4
answers
428k
views
warning: incompatible implicit declaration of built-in function ‘xyz’
I'm getting a number of these warnings when compiling a few binaries:
warning: incompatible implicit declaration of built-in function ‘strcpy’
warning: incompatible implicit declaration of built-in ...
169
votes
10
answers
67k
views
How to suppress GCC warnings from library headers?
I have a project that uses log4cxx, boost, etc. libraries whose headers generate lots of (repetitive) warnings. Is there a way to suppress warnings from library includes (i.e. #include <some-header....
100
votes
1
answer
16k
views
What exactly does GCC's -Wpsabi option do? What are the implications of supressing it?
Background
In the last year I was using the nlohmann json library[1] and was cross-compiling on x86_64 using GCC 5.x arm-linux-gnueabi-* with no warnings. When I updated GCC to a newer version, GCC ...
95
votes
8
answers
95k
views
Pedantic gcc warning: type qualifiers on function return type
When I compiled my C++ code with GCC 4.3 for the first time, (after having compiled it successfully with no warnings on 4.1, 4.0, 3.4 with the -Wall -Wextra options) I suddenly got a bunch of errors ...
80
votes
2
answers
12k
views
Compile and run program without main() in C
I'm trying to compile and run following program without main() function in C. I have compiled my program using the following command.
gcc -nostartfiles nomain.c
And compiler gives warning
/usr/bin/...
66
votes
2
answers
69k
views
function declared static but never defined
I have a header file suppose abc.h, where i have function declaration as:
static int function1();
I have included this header file in abc.c and has defined the function and used it.
static int ...
64
votes
4
answers
5k
views
Is there a GCC option to warn about writing `this-field` instead of `this->field`?
This following code (containing a vicious bug) compiles with GCC without any warning. But, of course, it doesn't work as expected by the developer (me).
#include <iostream>
struct A
{
bool ...
62
votes
5
answers
57k
views
How to circumvent format-truncation warning in GCC?
I'm getting the following gcc format-truncation warning:
test.c:8:33: warning: ‘/input’ directive output may be truncated writing 6 bytes into a region of size between 1 and 20 [-Wformat-truncation=]
...
61
votes
3
answers
102k
views
C warning implicit declaration of function 'exit'
This is my warning.
implicit declaration of function 'exit'
How i can remove it.
i am using linux & gcc compiler.
54
votes
4
answers
98k
views
How can I get rid of deprecated warnings in deprecated functions in GCC?
One way to implement deprecation warnings is to produce warnings on calls to deprecated functions, unless you are calling from a deprecated context. This way legacy code can call legacy code without ...
52
votes
6
answers
26k
views
Is there a way to get warned about unused functions?
I'd like to find unused functions in a codebase - including across compilations units. I'm using gcc as my compiler.
Here's an example:
foo.c (assume appropriate foo.h):
void foo() {
....
}
...
51
votes
3
answers
26k
views
How to use C++20's likely/unlikely attribute in if-else statement
This question is about C++20's [[likely]]/[[unlikely]] feature, not compiler-defined macros.
This documents (cppreference) only gave an example on applying them to a switch-case statement. This ...
45
votes
4
answers
2k
views
Why uninitialized instead of out-of-bounds?
In the code below why is b[9] uninitialized instead of out-of-bounds?
#include <stdio.h>
int main(void)
{
char b[] = {'N', 'i', 'c', 'e', ' ', 'y', 'o', 'u', '!'};
printf("b[9] = %d\n",...
41
votes
5
answers
31k
views
How can I make this GCC warning an error?
I get this warning from GCC:
warning: cannot pass objects of non-POD type 'class Something' through '...'; call will abort at runtime
It's pretty deadly, especially since it calls an abort. Why isn'...
36
votes
1
answer
11k
views
Why does gcc have a warning for long long?
What is the reason for the -Wlong-long gcc warning?
From the gcc man page:
-Wlong-long
Warn if long long type is used. This is enabled by either -Wpedantic or -Wtraditional in ISO C90 and C++...
35
votes
4
answers
114k
views
Warning: cast to/from pointer from/to integer of different size
I'm learning Pthreads. My code executes the way I want it to, I'm able to use it. But it gives me a warning on compilation.
I compile using:
gcc test.c -o test -pthread
with GCC 4.8.1. And I get ...
35
votes
3
answers
19k
views
How to do an explicit fall-through in C
The newer versions of gcc offer the Wimplicit-fallthrough, which is great to have for most switch statements. However, I have one switch statement where I want to allow fall throughs from all case-...
33
votes
5
answers
100k
views
gcc warning: braces around scalar initializer
I have look-up-table as defined below and I'm making use of GCC. When I compile I get warnings as
warning: braces around scalar initializer
What does this warning mean? How should I initialize this ...
33
votes
2
answers
2k
views
std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?
I have a hierarchy of base class and derived class. Base class has one virtual function which is overridden by derived class.
class Base
{
public:
~Base();
virtual void ...
32
votes
8
answers
58k
views
GCC missing braces around initializer [duplicate]
I have this struct in C below that I want to initialize to all zero. How do I get rid of the missing braces warning?
typedef struct {
uint32_t incoming[FRAME_TYPE_MAX];
uint32_t outgoing[...
31
votes
8
answers
11k
views
What's a proper way of type-punning a float to an int and vice-versa?
The code below performs a fast inverse square root operation by some bit hacks.
The algorithm was probably developed by Silicon Graphics in early 1990's and it's appeared in Quake 3 too.
more info
...
31
votes
4
answers
19k
views
Why does gcc -Wall give warning about zero-length format string?
I searched around a little bit for information on this but didn't find anything satisfactory. Is there some special behavior to the function call
sprintf(someString, "");
that explains why this is ...
30
votes
4
answers
12k
views
How to eliminate external lib/third party warnings in GCC [duplicate]
In the software project I'm working on, we use certain 3rd party libraries which, sadly, produce annoying gcc warnings.
We are striving to clean all code of warnings, and want to enable the treat-...
30
votes
2
answers
3k
views
Avoid or warn on implicit conversion from const char* to bool in GCC
Consider the following code:
void foo(bool parameter) {
std::cout << parameter << "\n";
}
int main() {
foo("const char *argument");
}
I want the compiler to raise a warning when ...
29
votes
3
answers
15k
views
What does if((x=0)) mean in C?
So apparently, in gcc/C, a compiler compiles when
if ((x=0)){ some code }
is used, while when
if (x=0){ some code }
is used, then compiler refuses to compile.
What are the differences between two?...
29
votes
2
answers
16k
views
Tell gcc that a function call will not return
I am using C99 under GCC.
I have a function declared static inline in a header that I cannot modify.
The function never returns but is not marked __attribute__((noreturn)).
How can I call the ...
28
votes
5
answers
12k
views
MSVC equivalent of __attribute__ ((warn_unused_result))?
I'm finding __attribute__ ((warn_unused_result)) to be very useful as a means of encouraging developers not to ignore error codes returned by functions, but I need this to work with MSVC as well as ...
27
votes
3
answers
53k
views
WARNING: "implicit declaration of function '...' is invalid in C99"
I'm getting this warning when I'm trying to compare RGB components of two UIColors
In .h file, I declared this
-(int) ColorDiff:(UIColor *) color1 :(UIColor *)color2;
In .m file
- (int) ...
26
votes
2
answers
80k
views
Disable warnings being treated as errors (cc1.exe)
I am developing a BREW application. When compiling the application to get a MOD file, I am continuously getting this error:
cc1.exe: warnings being treated as errors
I want to disable this warning. ...
26
votes
6
answers
18k
views
Don't understand "assuming signed overflow" warning
I am getting:
warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Wstrict-overflow]
on this line:
if ( this->m_PositionIndex[in] < this-&...
25
votes
9
answers
15k
views
Is there any way to get readable gcc error and warning output at the command line?
For some long errors, the gcc output is dense and has lots of line-wrapping etc. Especially when errors are subtle, it can take me 10-30 seconds of squinting to parse it with my eyes.
I've taken to ...
25
votes
6
answers
12k
views
How to make gcc warn about passing wrong enum to a function
gcc doesn't seem to produce a warning with the following code. How can I get it to produce a warning?
typedef enum
{
REG8_A,
REG8_B,
REG8_C
}REG8;
typedef enum
{
REG16_A,
REG16_B,...
24
votes
9
answers
37k
views
Suppress Compiler warning Function declared never referenced
So i have some code like this:
void foo (int, int);
void bar ( )
{
//Do Stuff
#if (IMPORTANT == 1)
foo (1, 2);
#endif
}
When doing a compile without "IMPORTANT" I get a compiler ...
23
votes
1
answer
33k
views
Meaning of the g++ flags "-Wall", " -W", and "-Werror"
What are these and what do they do?
-Wall
-W
-Werror
I am using the terminal in Ubuntu to compile programs with this command:
g++ -Wall -W -Werror main.cpp -o exec
What is the explanation?
23
votes
3
answers
6k
views
Why does GCC not warn for unreachable code?
Why doesn't GCC (4.6.3) give me any warning for the unreachable code in the below example?
#include <stdio.h>
int status(void)
{
static int first_time = 1;
if (first_time) {
...
21
votes
1
answer
10k
views
GCC v12.1 warning about serial compilation
I have upgraded my whole arch linux system today (12th May, 2022). gcc was also upgraded from v11.2 to v12.1. I tried compiling some of my programs with g++ (part of gcc compiler collection) by the ...
20
votes
2
answers
2k
views
How to make gcc warn undefined struct?
I have a struct defined in .h
struct buf_stats {
// ***
};
then in .c file
struct buf_stats *bs = malloc(sizeof(struct buf_states*)) ;
where buf_states is a typo.
but gcc does not warn me, ...
20
votes
4
answers
14k
views
How to print the address of a function?
I let gcc compile the following example using -Wall -pedantic:
#include <stdio.h>
int main(void)
{
printf("main: %p\n", main); /* line 5 */
printf("main: %p\n", (void*) main); /* line 6 */
...
19
votes
5
answers
62k
views
Error: this statement may fall through [-Werror=implicit-fallthrough=]
I am trying to compile mitk on ubuntu and I got this error :
error: this statement may fall through [-Werror=implicit-fallthrough=]
Here there is a part of code :
/** Get memory offset ...
18
votes
4
answers
3k
views
Have compiler check the number of array initializers
Initializing an array (in C++, but any solution which works for C will likely work here as well) with less initializers than it has elements is perfectly legal:
int array[10] = { 1, 2, 3 };
However, ...
17
votes
2
answers
51k
views
Why is GCC warning me this line is "misleadingly indented as if it were guarded by" an if?
The warning is:
/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:48:5: warning:
this ‘if’ clause does not guard... [-Wmisleading-indentation]
if (toHexSize < 1)
^~
/home/dronz/OF/apps/...
17
votes
3
answers
6k
views
Why doesn't GCC produce a warning when assigning a signed literal to an unsigned type?
Several questions on this website reveal pitfalls when mixing signed and unsigned types and most compilers seem to do a good job about generating warnings of this type. However, GCC doesn't seem to ...
17
votes
3
answers
3k
views
Why does std::abs return signed types
I'm getting warning for signed vs. unsigned comparison when I'm comparing a std::abs(int) against an unsigned. And indeed, std::abs returns signed values. Why was that choice made? It would have ...
16
votes
2
answers
29k
views
GCC -Wuninitialized / -Wmaybe-uninitialized issues
I am experiencing a very strange issue using gcc-4.7 (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2. I am unable to compile the following valid code without a warning:
extern void dostuff(void);
int test(...
16
votes
2
answers
5k
views
How to define extern variable along with declaration?
Wiki says:
The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible ...
16
votes
1
answer
266
views
How to warn when assigning or performing arithmetic with different enum types in GCC?
While I'm aware this is valid C not to differentiate between enum types.
GCC does have -Wenum-compare (which I'm using) and works as expected.
I tried using -Wconversion but this doesn't make any ...