0

I wonder what does the command gcc -o sample sample.c really means and what it does in the system.

I know gcc is the command to invoke gcc compiler, but what does -o and filename(without extension) followed by filename(with extension) means here.

4
  • 4
    gcc -o sample sample.c compiles the C code in sample.c to an executable binary sample, which you can run by typing ./sample. The -o flag sets the name of the output file, sample.c is the source code file.
    – grooveplex
    Jun 15, 2016 at 7:08
  • Apart from -o what are the other flags we can set? Jun 15, 2016 at 7:11
  • there are lots. You can see them all by running man gcc in a Terminal.
    – grooveplex
    Jun 15, 2016 at 7:13
  • @Selvaperumal - is your question answered? If so - feel free to mark the related answer as accepted. Or do you still need more information? Greetings.
    – dufte
    Jun 17, 2016 at 8:40

2 Answers 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.

5
  • I would advise you to include the most important information from the Wikipedia link in case it ever breaks.
    – grooveplex
    Jun 15, 2016 at 7:15
  • Added the starting portion - but to be honest - if Wikipedia breaks - i doubt AskUbuntu still exists ;)
    – dufte
    Jun 15, 2016 at 7:20
  • Me too, actually, but it's generally advised to include the most important information from links in your answer
    – grooveplex
    Jun 15, 2016 at 7:22
  • It would be good to include the relevant part of the manpage for -o
    – user423626
    Jun 15, 2016 at 7:24
  • good point - i've added the -o section from the man page
    – dufte
    Jun 15, 2016 at 8:09
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.

2
  • Apart from -o what are the other flags we can set? – Jun 15, 2016 at 7:12
  • There are many, run man gcc in your terminal. Jun 15, 2016 at 7:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .