See https://man7.org/linux/man-pages/man1/gcc.1.html. Under the Synopsis section, you will see the -o option.

Regarding

Well OK, the compiler knows where to look for stdio.h, but the executable must be linked to stdio.o, mustn't it? Where does the stdio.o file reside?

The answer to the first question is "No". Since the answer to the first question is "No", the second question is not relevant.

The functions and variables declared in stdio.h need not be in stdio.o. They are usually in a library (.a or .so) which are found in one of the directories where the linker looks for library files.

In general, there is no rule that each .h file has a corresponding .o file. It is possible to have the functions and variables declared in a .h file to be implemented in multiple .c files that will result in multiple .o files. It is also possible to have functions and variables declared in multiple .h files to be implemented in one .c file. How these are organized varies from project to project.

Each .c file, on the other hand, has a corresponding .o file (I haven't seen any platforms where multiple .c files can be compiled to create one .o file). All the .o files resulting from compiling the .c files are linked together to create an executable.

Answer from R Sahu on Stack Overflow
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Optimize-Options.html
Optimize Options (Using the GNU Compiler Collection (GCC))
Most optimizations are completely disabled at -O0 or if an -O level is not set on the command line, even if individual optimization flags are specified. Similarly, -Og suppresses many optimization passes. Depending on the target and how GCC was configured, a slightly different set of optimizations ...
🌐
RapidTables
rapidtables.com › code › linux › gcc › gcc-o.html
gcc -o / -O option flags (output / optimization)
Set the compiler's optimization level. +increase ++increase more +++increase even more -reduce --reduce more ---reduce even more · $ gcc -Olevel [options] [source files] [object files] [-o output file]
Top answer
1 of 3
7

See https://man7.org/linux/man-pages/man1/gcc.1.html. Under the Synopsis section, you will see the -o option.

Regarding

Well OK, the compiler knows where to look for stdio.h, but the executable must be linked to stdio.o, mustn't it? Where does the stdio.o file reside?

The answer to the first question is "No". Since the answer to the first question is "No", the second question is not relevant.

The functions and variables declared in stdio.h need not be in stdio.o. They are usually in a library (.a or .so) which are found in one of the directories where the linker looks for library files.

In general, there is no rule that each .h file has a corresponding .o file. It is possible to have the functions and variables declared in a .h file to be implemented in multiple .c files that will result in multiple .o files. It is also possible to have functions and variables declared in multiple .h files to be implemented in one .c file. How these are organized varies from project to project.

Each .c file, on the other hand, has a corresponding .o file (I haven't seen any platforms where multiple .c files can be compiled to create one .o file). All the .o files resulting from compiling the .c files are linked together to create an executable.

2 of 3
4

It indicates the output file, i.e. what your object-file or executable should be. You can see this information by doing gcc --help or man gcc.

<stdio.h> is part of the standard library, which your compiler is already linking to.

🌐
Stanford
web.stanford.edu › class › archive › cs › cs107 › cs107.1186 › unixref › topics › gcc
gcc (how to compile c programs)
gcc takes many different command line options (flags) that change its behavior. One of the most common flags is the "optimization level" flag, -O (uppercase 'o'). gcc has the ability to optimize your code for various situations.
🌐
Mithilesh's Blog
esymith.hashnode.dev › a-comprehensive-guide-to-gcc-flags
A Comprehensive Guide to GCC Flags
April 14, 2024 - In this guide, we'll delve into the most commonly used GCC flags, exploring their functionalities and how they can help optimize your code.
🌐
Linux Questions
linuxquestions.org › questions › linux-software-2 › gcc-flags-553790
GCC Flags
I am reading a book called setting up LAMP and there is a section which teaches how to install mysql from source. The source files are to be compiled,
🌐
IBM
ibm.com › docs › en › xl-c-and-cpp-linux › 16.1.0
Supported GCC options
We cannot provide a description for this page right now
🌐
Quora
quora.com › How-do-the-GCC-flags-O-O2-etc-optimize-for-speed-What-do-they-do-specifically
How do the GCC flags -O / -O2 etc. optimize for speed? What do they do specifically? - Quora
Answer (1 of 2): I don’t think Quora’s servers will be able to handle the length of that answer :D GCC is 7.3 million lines of code, and probably 7.29 million of those are doing code optimization. But, the TL;DR version is, it writes another program that is the same as yours in terms of ...
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Flags.html
Flags (GNU Compiler Collection (GCC) Internals)
Nonzero in an insn, call_insn, jump_insn, barrier, or set which is part of a function prologue and sets the stack pointer, sets the frame pointer, or saves a register. This flag should also be set on an instruction that sets up a temporary register to use in place of the frame pointer.
Find elsewhere
🌐
Linux Handbook
linuxhandbook.com › gcc-flags
Important GCC Flags in Linux
December 16, 2022 - This flag can be crucial for those who are dealing with dynamic linking, especially with shared libraries. While creating shared libraries, you should have the position-independent code so that you can load the shared library from any address rather than a static one. For example, here, I have created a shared library named libmain.so from the main.c: gcc -c -Wall -Werror -fPIC main.c gcc -shared -o libmain.so main.o
Top answer
1 of 2
3

Your question

What gcc -o sample sample.c actually does

The command you have pasted triggers gcc to compile sample.c. As a result you'll get an output/executable with the same sample.

The parameters (as -o in your example which defined the name of the output file ) are explained in the man page of gcc. You can open it by running man gcc

I am quoting the -o section of the man page here

-o file

       Place output in file file.  This applies to whatever sort of output
       is being produced, whether it be an executable file, an object
       file, an assembler file or preprocessed C code.

       If -o is not specified, the default is to put an executable file in
       a.out, the object file for source.suffix in source.o, its assembler
       file in source.s, a precompiled header file in source.suffix.gch,
       and all preprocessed C source on standard output.

In General

Wikipedia explains a good amount of gcc here.

I am quoting only the start - but the complete article is worth a read and offers a lot of additional links and references

The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain. The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL). GCC has played an important role in the growth of free software, as both a tool and an example.

Originally named the GNU C Compiler, when it only handled the C programming language, GCC 1.0 was released in 1987. It was extended to compile C++ in December of that year. Front ends were later developed for Objective-C, Objective-C++, Fortran, Java, Ada, and Go among others.

GCC has been ported to a wide variety of processor architectures, and is widely deployed as a tool in the development of both free and proprietary software. GCC is also available for most embedded platforms,[citation needed] including Symbian (called gcce), AMCC, and Freescale Power Architecture-based chips. The compiler can target a wide variety of platforms, including video game consoles such as the PlayStation 2 and Dreamcast.

As well as being the official compiler of the GNU operating system, GCC has been adopted as the standard compiler by many other modern Unix-like computer operating systems, including Linux and the BSD family, although FreeBSD and OS X have moved to the LLVM system. Versions are also available for Microsoft Windows and other operating systems; GCC can compile code for Android and iOS.

2 of 2
1

GCC is GNU C (language) compiler. It's used to "convert" programs written in C programming language into binary executable on computer.

The syntax gcc -o sample sample.c means: Compile the file sample.c and name the output sample.

Then you can launch your compiled program with <path_to_file>/sample, or from within the directory ./sample

The default name for compiled program is a.out, so with -o parameter you can specify your desired output name.

🌐
Gjbex
gjbex.github.io › Defensive_programming_and_debugging › CodeValidation › Compilers › gcc_flags
Flags for gcc/g++ - Defensive programming and debugging
It is highly recommended to always use these two compiler options, and eliminate all warnings reported by them, i.e., ... The gcc compiler can also check for language specification conformity." The -Wpedantic flag will activate this, and you should specify the specification it should check, i.e.,
Top answer
1 of 16
178

Here are mine:

  • -Wextra and -Wall: essential.
  • -Wfloat-equal: useful because usually testing floating-point numbers for equality is bad.
  • -Wundef: warn if an uninitialized identifier is evaluated in an #if directive.
  • -Wshadow: warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.
  • -Wpointer-arith: warn if anything depends upon the size of a function or of void.
  • -Wcast-align: warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries.
  • -Wstrict-prototypes: warn if a function is declared or defined without specifying the argument types.
  • -Wstrict-overflow=5: warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. (The value 5 may be too strict, see the manual page.)
  • -Wwrite-strings: give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning.
  • -Waggregate-return: warn if any functions that return structures or unions are defined or called.
  • -Wcast-qual: warn whenever a pointer is cast to remove a type qualifier from the target type*.
  • -Wswitch-default: warn whenever a switch statement does not have a default case*.
  • -Wswitch-enum: warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration*.
  • -Wconversion: warn for implicit conversions that may alter a value*.
  • -Wunreachable-code: warn if the compiler detects that code will never be executed*.

Those marked * sometimes give too many spurious warnings, so I use them on as-needed basis.

2 of 16
77

Several of the -f code generation options are interesting:

  • -fverbose-asm is useful if you're compiling with -S to examine the assembly output - it adds some informative comments.

  • -finstrument-functions adds code to call user-supplied profiling functions at every function entry and exit point.

  • --coverage instruments the branches and calls in the program and creates a coverage notes file, so that when the program is run coverage data is produced that can be formatted by the gcov program to help analysing test coverage.

  • -fsanitize={address,thread,undefined} enables the AddressSanitizer, ThreadSanitizer and UndefinedBehaviorSanitizer code sanitizers, respectively. These instrument the program to check for various sorts of errors at runtime.

Previously this answer also mentioned -ftrapv, however this functionality has been superseded by -fsanitize=signed-integer-overflow which is one of the sanitizers enabled by -fsanitize=undefined.

🌐
Reddit
reddit.com › r/c_programming › the best and worst gcc compiler flags for embedded
r/C_Programming on Reddit: The Best and Worst GCC Compiler Flags For Embedded
September 13, 2019 - This way, normal structs don't incur any performance penalties, the code will be compiled correctly even if the flag is not added & structs that are passed to other places are called out explicitly. This can be done with either the pack pragma or in the case of gcc/clang the packed attribute:
🌐
Reddit
reddit.com › r/c_programming › gcc flags
r/C_Programming on Reddit: GCC flags
November 18, 2023 -

Hi,

I'm a beginner, barely scratching the surface of C at the moment. Question is, what flags do I choose for compilation? There are some "basic" like -Wall, -W, -pedantic, -ansi, -std=. GCC documentation has a ton of different flags.
Should I learn Make or CMake early to avoid retyping flags every time to compile my source files?Any help, advice are greatly appreciated.
Edit: thank you, guys. Lots of useful and interesting information. You're awesome!

Top answer
1 of 5
13
I'd recommend what you have plus Wextra. As for the second question, simply alias a build command. alias gcc_test='gcc -Wall -Wextra -pedantic -std=c99' Obviously, replace gcc_test with whatever you will remember! Then you can use gcc_test -o myprogram myprogram.c
2 of 5
4
For warnings, start with -Wall -Wextra. Everybody has a different set of flags they like on top of these. You don’t need to get everything now, just start with those two. For standard, start with -std=c11 or -std=c17. Or -std=gnu11 or -std=gnu17. The exact standard is not a big deal. Just pick one, because if you don’t pick one, you’ll get some default and you won’t know which one you’re using. Add -g so you can use the debugger. Add -fsanitize=address when you need help finding memory errors. Avoid using -pedantic. It’s not really that helpful. It just kinda gets in the way. Should I learn Make or CMake early to avoid retyping flags every time to compile my source files? My first recommendation is to use a good build system like Meson, or an IDE like Visual Studio, Code::Blocks, or Xcode. You can use CMake instead, but it kinda sucks. You can use Make, but it sucks a lot. Maybe you like these better. There are a lot of reasons why Make sucks, so I don’t recommend it to anyone. Even if you don’t use one of those build systems, you can at least write a shell script to compile everything. Paste your command line into a text file and then chmod +x that text file, so you can run it to build. For example, you could have a text file named build which looks like this: gcc -Wall -Wextra -g -std=c17 main.c lib.c Then you chmod it +x: $ chmod +x build Then you can run it: $ ./build
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-4.6.4 › gcc
Using the GNU Compiler Collection (GCC)
The internals of the GNU compilers, including how to port them to new targets and some information about how to write front ends for new languages, are documented in a separate manual. See Introduction. G++ and GCC: You can compile C or C++ programs.
🌐
Medium
medium.com › @eightlimbed › commonly-used-gcc-options-explained-8304dfccea20
Commonly Used GCC Options Explained | by Lee Gaines | Medium
December 19, 2019 - In conclusion, these four GCC options (or “flags”) are useful for keeping your code in good shape and prevents common mistakes.