Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #60669

Re: Fist steps in compiling, gcc commands confusion

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Fist steps in compiling, gcc commands confusion
Date 2015-04-21 11:16 -0700
Organization None to speak of
Message-ID <87fv7tmjn1.fsf@kst-u.example.com> (permalink)
References <9f1b89ff-cf47-4e01-b137-ba263e9e75f1@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:
> Hello!
>
> Different resources advice different commands to compile a file:
>
> 1) gcc sample.c sample
>
> 2) gcc sample.c -o sample
>
> 3) gcc -o sample.c
>
> What are the difference in the meaning? What does -o stand for? 
>
> The only command that produced results for me was gcc sample.c -o sample
> Although the book suggests simple gcc sample.c

This should all be documented in the gcc (or clang) manual.

    gcc sample.c sample

This is wrong.  It will try to use "sample" as an input file, but it
won't know what to do with it.

    gcc sample.c -o sample

This is right.  It compiles the C source file "sample.c" and creates
"sample" as an executable file.  If "sample.c" is a "hello, world"
program, then typing "./sample" will print "hello, world".

    gcc -o sample.c

This is wrong.  It doesn't provide any input files, so gcc probably will
do nothing other than printing an error message.  If it does do
anything, it will treat "sample.c" as an output file, clobbering your
source file and replacing it with a useless binary.

Are you sure that options 1 and 3 were actually stated that way?  If so,
can you tell us which resource you got them from, so we can steer people
away from them?

The "-o" option specifies an output file.

This:

    gcc sample.c

compiles and links your program and creates an executable file with the
default name, which for obscure historical reasons is "a.out".  Although
it's the default, it's rarely used in practice.

    gcc sample.c -o sample

Like the above, but it specifies the name of the executable.  This is
probably the one you'll use the most, at least initially.

    gcc -c sample.c

The "-c" option tells gcc just to compile, not to link.  The output file
will be called "sample.o".  It can then be fed to later "gcc" or "ld"
commands to produce an executable.

At this stage, you're probably just writing small programs that are
contained in a single source file.  For that, all you need to know is:

    gcc sample.c -o sample

to create an executable file and

    ./sample

to run it.

All this is reasonably consistent for compilers for UNIX-like systems
(that includes Linux, OSX, the BSDs, and others).  Compilers for
non-UNIX-like systems, particularly Windows, tend to have different
conventions -- and Windows in particular places a greater emphasis on
using an IDE (a graphical Interactive Development Environment) rather
than using the command line.  But you can use the command line on
Windows, or an IDE (like XCode or Eclipse) on a UNIX-like system.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Fist steps in compiling, gcc commands confusion Alla _ <modelling.data@gmail.com> - 2015-04-21 08:18 -0700
  Re: Fist steps in compiling, gcc commands confusion Alla _ <modelling.data@gmail.com> - 2015-04-21 08:25 -0700
    Re: Fist steps in compiling, gcc commands confusion Morris Dovey <mrdovey@iedu.com> - 2015-04-21 10:56 -0500
      Re: Fist steps in compiling, gcc commands confusion David Brown <david.brown@hesbynett.no> - 2015-04-22 09:35 +0200
        Re: Fist steps in compiling, gcc commands confusion James Kuyper <jameskuyper@verizon.net> - 2015-04-22 11:15 -0400
          Re: Fist steps in compiling, gcc commands confusion David Brown <david.brown@hesbynett.no> - 2015-04-23 01:31 +0200
        Re: Fist steps in compiling, gcc commands confusion Keith Thompson <kst-u@mib.org> - 2015-04-22 08:43 -0700
          Re: Fist steps in compiling, gcc commands confusion Bartc <bc@freeuk.com> - 2015-04-22 18:57 +0100
            Re: Fist steps in compiling, gcc commands confusion Keith Thompson <kst-u@mib.org> - 2015-04-22 11:35 -0700
  Re: Fist steps in compiling, gcc commands confusion James Kuyper <jameskuyper@verizon.net> - 2015-04-21 12:01 -0400
  Re: Fist steps in compiling, gcc commands confusion John Gordon <gordon@panix.com> - 2015-04-21 16:01 +0000
    Re: Fist steps in compiling, gcc commands confusion Tim Prince <tprince@computer.org> - 2015-04-21 13:11 -0400
    Re: Fist steps in compiling, gcc commands confusion Keith Thompson <kst-u@mib.org> - 2015-04-21 12:09 -0700
  Re: Fist steps in compiling, gcc commands confusion Bartc <bc@freeuk.com> - 2015-04-21 18:25 +0100
    Re: Fist steps in compiling, gcc commands confusion Keith Thompson <kst-u@mib.org> - 2015-04-21 12:11 -0700
  Re: Fist steps in compiling, gcc commands confusion Keith Thompson <kst-u@mib.org> - 2015-04-21 11:16 -0700

csiph-web