Search code examples
c++cgccg++syntax-checking

Difference between stopping after compiling and checking for syntax only


When using gcc or g++, what is the difference between telling GCC to only compile a file and not link (-S), combined with telling it to not produce an output file (-o "nul"):

gcc.exe/g++.exe -S "filename" -o "nul"

... and telling it to check syntax only (-fsyntax-only)?

gcc.exe/g++.exe -c "filename" -fsyntax-only

Both options do not produce output, only run the compilation stage, and take about the same amount of time to run.

I am using the Windows ports of GCC, which treats "nul" as the null device that eats all output (like /dev/null).

Edit: as Mike Seymour pointed out, when passing -fsyntax-only to GCC, it will not attempt to perform optimization even if it is told to do so, which is not the case for -S. In other words this is slower (I've performed the measurements):

gcc.exe -S "filename" -o "nul" -O3

... than this:

gcc.exe -c "filename" -fsyntax-only -O3

Solution

  • The first does produce output, in the form of assembly, since that's what -S does; then you throw the output away. (Assuming that's what writing to "nul" means on your platform; I'd have expected it to write to a file called "nul".)

    The result is similar to the second, but does more work (compiling and generating assembly), and doesn't really express the intent of checking the syntax. The extra static analysis during code generation and optimisation might generate extra warnings that would be missed when only checking syntax.