How to get gcc to skip errors, but still output them.

Asked
Active3 hr before
Viewed126 times

8 Answers

errors
90%

Up-to-date versions of GCC will attempt to skip certain errors where possible.,Say the body of foo(){... contains a const-violation. The translation unit will not produce an object file but any decent compiler will continue past this error into bar(){...,If a compiler encounters an error, it has to guess what the correct code should be and try to follow. Effectively that means that you always need to fix the first error and re-run the compilations, since the rest will be nonsense., I changed the ioctl stuff to unlocked_ioctl and this object compiled without a fuss, so I think the above shows an example of gcc continuing past the error! Thanks for the explanation – chrisvarnz Jul 28 '11 at 15:57

If a compiler encounters an error, it has to guess what the correct code should be and try to follow. Effectively that means that you always need to fix the first error and re-run the compilations, since the rest will be nonsense.

Make sure that you haven't turned -Wfatal-errors on.

-Wfatal - errors
load more v
88%

GCC terminates when it can't go further.,If a compiler encounters an error, it has to guess what the correct code should be and try to follow. Effectively that means that you always need to fix the first error and re-run the compilations, since the rest will be nonsense.,Other errors are unrecoverable. If you miss out some curly braces there's no reasonable guess that can be mades as to how to proceed.,Say the body of foo(){... contains a const-violation. The translation unit will not produce an object file but any decent compiler will continue past this error into bar(){...

If a compiler encounters an error, it has to guess what the correct code should be and try to follow. Effectively that means that you always need to fix the first error and re-run the compilations, since the rest will be nonsense.

Make sure that you haven't turned -Wfatal-errors on.

-Wfatal - errors
load more v
72%

Check the code for syntax errors, but don’t do anything beyond that. ,It doesn’t warn when both branches contain just a null statement. This warning also warn for conditional operators: ,Warn whenever a switch statement does not have a default case. ,These warnings are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables.

-Waddress
   -
   Warray - bounds = 1(only with - O2) -
   Warray - compare -
   Warray - parameter = 2(C and Objective - C only) -
   Wbool - compare -
   Wbool - operation -
   Wc++11 - compat - Wc++14 - compat -
   Wcatch - value(C++and Objective - C++only) -
   Wchar - subscripts -
   Wcomment -
   Wduplicate - decl - specifier(C and Objective - C only) -
   Wenum - compare( in C / ObjC; this is on by
      default in C++) -
   Wformat -
   Wformat - overflow -
   Wformat - truncation -
   Wint - in -bool - context -
   Wimplicit(C and Objective - C only) -
   Wimplicit - int(C and Objective - C only) -
   Wimplicit - function -declaration(C and Objective - C only) -
   Winit - self(only
      for C++) -
   Wlogical - not - parentheses -
   Wmain(only
      for C / ObjC and unless - ffreestanding) -
   Wmaybe - uninitialized -
   Wmemset - elt - size -
   Wmemset - transposed - args -
   Wmisleading - indentation(only
      for C / C++) -
   Wmismatched - dealloc -
   Wmismatched - new - delete(only
      for C / C++) -
   Wmissing - attributes -
   Wmissing - braces(only
      for C / ObjC) -
   Wmultistatement - macros -
   Wnarrowing(only
      for C++) -
   Wnonnull -
   Wnonnull - compare -
   Wopenmp - simd -
   Wparentheses -
   Wpessimizing - move(only
      for C++) -
   Wpointer - sign -
   Wrange - loop - construct(only
      for C++) -
   Wreorder -
   Wrestrict -
   Wreturn - type -
   Wsequence - point -
   Wsign - compare(only in C++) -
   Wsizeof - array - div -
   Wsizeof - pointer - div -
   Wsizeof - pointer - memaccess -
   Wstrict - aliasing -
   Wstrict - overflow = 1 -
   Wswitch -
   Wtautological - compare -
   Wtrigraphs -
   Wuninitialized -
   Wunknown - pragmas -
   Wunused - function -Wunused - label -
   Wunused - value -
   Wunused - variable -
   Wvla - parameter(C and Objective - C only) -
   Wvolatile - register -
   var -Wzero - length - bounds
load more v
65%

For details of in-depth Linux/UNIX system programming training courses that I teach, look here. , HTML rendering created 2021-08-27 by Michael Kerrisk, author of The Linux Programming Interface, maintainer of the Linux man-pages project. ,Pages that refer to this page: as(1),  dpkg-architecture(1),  uselib(2),  alloca(3),  backtrace(3),  dladdr(3),  dlopen(3),  lttng-ust-cyg-profile(3),  offsetof(3),  printf(3),  sincos(3),  strftime(3),  feature_test_macros(7),  hier(7),  math_error(7),  warning::debuginfo(7stap)

load more v
75%

How to set up a compiler , Source code C and C++ tips Getting a compiler Book recommendations Forum ,How to learn C++ or C ,Jumping into C++, the Cprogramming.com ebook

and
struct {
   int x;
   int y;
}
myStruct;
load more v
40%

If we were to inadvertently add a command script to this makefile text, make would produce the overriding warning.,and allow several separate makefiles to add their own prerequisites. Then in a makefile we could write:,When run, this makefile does not terminate with an error status, although an error most definitely occurs:,One of the most common errors for new make users is omitting parentheses around variable names:

$(warning A top - level warning)

FOO: = $(warning Right - hand side of a simple variable) bar
BAZ = $(warning Right - hand side of a recursive variable) boo

$(warning A target) target: $(warning In a prerequisite list) makefile $(BAZ)
$(warning In a command script)
ls
$(BAZ):
$ make
makefile: 1: A top - level warning
makefile: 2: Right - hand side of a simple variable
makefile: 5: A target
makefile: 5: In a prerequisite list
makefile: 5: Right - hand side of a recursive variable
makefile: 8: Right - hand side of a recursive variable
makefile: 6: In a command script
ls
makefile
REQUIRED_DIRS = ...
   _MKDIRS: = $(shell
      for d in $(REQUIRED_DIRS);\ do\
         [
            [-d $$d]
         ] || mkdir - p $$d;\
      done)

$(objects): $(sources)
# GNU Make 3.80
# Copyright(C) 2002 Free Software Foundation, Inc.
# This is free software;
see the source
for copying conditions.
# There is NO warranty;
not even
for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
normal command execution occurs here

# Make data base, printed on Thu Apr 29 20: 58: 13 2004

# Variables
   ...
   # Directories
   ...
   # Implicit Rules
   ...
   # Pattern - specific variable values
   ...
   # Files
   ...
   # VPATH Search Paths
# automatic
   <
   D = $(patsubst % /,%,$(dir $<))
      # environment EMACS_DIR = C: /usr/emacs - 21.3 .50 .7 #
      default CWEAVE = cweave # makefile(from `../mp3_player/makefile', line 35)
CPPFLAGS = $(addprefix -I ,$(include_dirs))
# makefile (from `.. / ch07 - separate - binaries / makefile ', line 44)
         RM: = rm - f # makefile(from `../mp3_player/makefile', line 14)
define make-library
  libraries += $1
  sources   += $2

  $1: $(call source-to-object,$2)
        $(AR) $(ARFLAGS) $$@ $$^
endef
% .c % .h: % .y
# commands to execute(from `../mp3_player/makefile', line 73):
        $(YACC.y) --defines $<
        $(MV) y.tab.c $*.c
        $(MV) y.tab.h $*.h

%: %.c
#  commands to execute (built-in):
        $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

%.o: %.c
#  commands to execute (built-in):
        $(COMPILE.c) $(OUTPUT_OPTION) $<
% .c % .h: YYLEXFLAG: = -d %
   .c % .h: % .y
$(YACC.y) --defines $ <
   $(MV) y.tab.c $ * .c
$(MV) y.tab.h $ * .h
# Pattern - specific variable values

   %
   .c:
   # makefile(from `Makefile', line 1)
# YYLEXFLAG := -d
# variable set hash-table stats:
# Load=1/16=6%, Rehash=0, Collisions=0/1=0%

%.h :
# makefile (from `
      Makefile ', line 1)
      # YYLEXFLAG: = -d # variable set hash - table stats:
      # Load = 1 / 16 = 6 % , Rehash = 0, Collisions = 0 / 1 = 0 %

      # 2 pattern - specific variable values
# Not a target:
   .p.o:
   # Implicit rule search has not been done.
# Modification time never checked.
# File has not been updated.
# commands to execute(built - in ):
   $(COMPILE.p) $(OUTPUT_OPTION) $ <

   lib / ui / libui.a: lib / ui / ui.o
# Implicit rule search has not been done.
# Last modified 2004 - 04 - 01 22: 04: 09.515625
# File has been updated.
# Successfully updated.
# commands to execute(from `../mp3_player/lib/ui/module.mk', line 3):
        ar rv $@ $^

lib/codec/codec.o: ../mp3_player/lib/codec/codec.c ../mp3_player/lib/codec/codec.c ..
/mp3_player/include/codec/codec.h
#  Implicit rule search has been done.
#  Implicit/static pattern stem: `
      lib / codec / codec '
      # Last modified 2004 - 04 - 01 22: 04: 08.40625 # File has been updated.# Successfully updated.# commands to execute(built - in ):
      $(COMPILE.c) $(OUTPUT_OPTION) $ <
$ make--warn - undefined - variables - n
makefile: 35: warning: undefined variable MAKECMDGOALS
makefile: 45: warning: undefined variable CFLAGS
makefile: 45: warning: undefined variable TARGET_ARCH
   ...
   makefile: 35: warning: undefined variable MAKECMDGOALS
make: warning: undefined variable CFLAGS
make: warning: undefined variable TARGET_ARCH
make: warning: undefined variable CFLAGS
make: warning: undefined variable TARGET_ARCH
   ...
   make: warning: undefined variable LDFLAGS
make: warning: undefined variable TARGET_ARCH
make: warning: undefined variable LOADLIBES
make: warning: undefined variable LDLIBS
File all does not exist.
File app / player / play_mp3 does not exist.
File app / player / play_mp3.o does not exist.
Must remake target app / player / play_mp3.o.
gcc..... / mp3_player / app / player / play_mp3.c
Successfully remade target file app / player / play_mp3.o.
File all does not exist.
Considering target file app / player / play_mp3.
File app / player / play_mp3 does not exist.
Considering target file app / player / play_mp3.o.
File app / player / play_mp3.o does not exist.
Pruning file.. / mp3_player / app / player / play_mp3.c.
Pruning file.. / mp3_player / app / player / play_mp3.c.
Pruning file.. / mp3_player / include / player / play_mp3.h.
Finished prerequisites of target file app / player / play_mp3.o.
Must remake target app / player / play_mp3.o.
gcc..... / mp3_player / app / player / play_mp3.c
Successfully remade target file app / player / play_mp3.o.
Pruning file app / player / play_mp3.o.
File all does not exist.
File app / player / play_mp3 does not exist.
Looking
for an implicit rule
for app / player / play_mp3.
Trying pattern rule with stem play_mp3.
Trying implicit prerequisite app / player / play_mp3.o.
Found an implicit rule
for app / player / play_mp3.
File app / player / play_mp3.o does not exist.
Looking
for an implicit rule
for app / player / play_mp3.o.
Trying pattern rule with stem play_mp3.
Trying implicit prerequisite app / player / play_mp3.c.
Found prerequisite app / player / play_mp3.c as VPATH.. / mp3_player / app / player /
   play_mp3.c
Found an implicit rule
for app / player / play_mp3.o.
Must remake target app / player / play_mp3.o.
gcc..... / mp3_player / app / player / play_mp3.c
Successfully remade target file app / player / play_mp3.o.
Got a SIGCHLD;
1 unreaped children.
gcc..... / mp3_player / app / player / play_mp3.c
Putting child 0x10033800(app / player / play_mp3.o) PID 576 on the chain.
Live child 0x10033800(app / player / play_mp3.o) PID 576
Got a SIGCHLD;
1 unreaped children.
Reaping winning child 0x10033800 PID 576
Removing child 0x10033800 PID 576 from chain.
load more v
22%

Check the code for syntax errors, but don’t do anything beyond that.,Warn whenever a switch statement does not have a default case.,Make all warnings into errors.,Do not warn when attempting to free an object that was not allocated on the heap.

float area(float radius) {
   return 3.14159 * radius * radius;
}
load more v
60%

Next, verify that the Visual C++ developer command prompt is set up correctly. In the command prompt window, enter cl and verify that the output looks something like this:,To run your program, enter hello at the command prompt.,To compile your program, enter cl hello.c at the developer command prompt.,Enter notepad hello.c at the developer command prompt. In the Notepad alert dialog that pops up, choose Yes to create a new hello.c file in your working directory.

Next, verify that the Visual C++ developer command prompt is set up correctly. In the command prompt window, enter cl and verify that the output looks something like this:

C: \Program Files(x86)\ Microsoft Visual Studio\ 2017\ Enterprise > cl
Microsoft(R) C / C++Optimizing Compiler Version 19.10 .25017
for x86
Copyright(C) Microsoft Corporation.All rights reserved.

usage: cl[option...] filename...[/link linkoption... ]

In Notepad, enter the following lines of code:

#include <stdio.h>

int main()
{
    printf("Hello, World! This is a native C program compiled on the command line.\n");
    return 0;
}

Switch back to the developer command prompt window. Enter dir at the command prompt to list the contents of the c:\hello directory. You should see the source file hello.c in the directory listing, which looks something like:

C:\hello>dir
Volume in drive C has no label.
Volume Serial Number is CC62-6545

Directory of C:\hello

10/02/2017 03:46 PM <DIR> .
   10/02/2017 03:46 PM <DIR> ..
      10/02/2017 03:36 PM 143 hello.c
      1 File(s) 143 bytes
      2 Dir(s) 514,900,566,016 bytes free

You can see the executable program name, hello.exe, in the lines of output information that the compiler displays:

c: \hello > cl hello.c
Microsoft(R) C / C++Optimizing Compiler Version 19.10 .25017
for x86
Copyright(C) Microsoft Corporation.All rights reserved.

hello.c
Microsoft(R) Incremental Linker Version 14.10 .25017 .0
Copyright(C) Microsoft Corporation.All rights reserved.

   /
   out: hello.exe
hello.obj

The program displays this text and then exits:

Hello, World!This is a native C program compiled on the command line.
load more v

Other "errors-undefined" queries related to "How to get gcc to skip errors, but still output them."