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


Groups > comp.lang.c > #60646 > unrolled thread

Fist steps in compiling, gcc commands confusion

Started byAlla _ <modelling.data@gmail.com>
First post2015-04-21 08:18 -0700
Last post2015-04-21 11:16 -0700
Articles 16 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  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

#60646 — Fist steps in compiling, gcc commands confusion

FromAlla _ <modelling.data@gmail.com>
Date2015-04-21 08:18 -0700
SubjectFist steps in compiling, gcc commands confusion
Message-ID<9f1b89ff-cf47-4e01-b137-ba263e9e75f1@googlegroups.com>
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

Thank you! 

[toc] | [next] | [standalone]


#60649

FromAlla _ <modelling.data@gmail.com>
Date2015-04-21 08:25 -0700
Message-ID<be2ee571-ec9b-494e-abf9-93f6f2e94e9e@googlegroups.com>
In reply to#60646
On Tuesday, April 21, 2015 at 7:18:19 PM UTC+4, Alla _ wrote:
> 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
> 
> Thank you!

Just to add: I don't understand why the option with renaming the file has worked while the option gcc sample.c has not.

[toc] | [prev] | [next] | [standalone]


#60654

FromMorris Dovey <mrdovey@iedu.com>
Date2015-04-21 10:56 -0500
Message-ID<mh5s03$dgg$1@speranza.aioe.org>
In reply to#60649
On 4/21/15 10:25 AM, Alla _ wrote:
> Just to add: I don't understand why the option with renaming the file
> has worked while the option gcc sample.c has not.

In a terminal window, you can use the 'man' utility to view manual 
pages. I suggest reading the page for man itself by typing

man man

It's helpful to remember that you can exit from man by typing 'q' for quit.

Having done that, try

man gcc

and should see the answer to your question - and much, much more.

-- 
Morris Dovey
http://www.iedu.com/Solar

[toc] | [prev] | [next] | [standalone]


#60700

FromDavid Brown <david.brown@hesbynett.no>
Date2015-04-22 09:35 +0200
Message-ID<mh7iu8$mcg$1@dont-email.me>
In reply to#60654
On 21/04/15 17:56, Morris Dovey wrote:
> On 4/21/15 10:25 AM, Alla _ wrote:
>> Just to add: I don't understand why the option with renaming the file
>> has worked while the option gcc sample.c has not.
> 
> In a terminal window, you can use the 'man' utility to view manual
> pages. I suggest reading the page for man itself by typing
> 
> man man
> 
> It's helpful to remember that you can exit from man by typing 'q' for quit.
> 
> Having done that, try
> 
> man gcc
> 
> and should see the answer to your question - and much, much more.
> 

The "man" page for gcc has little useful information - the real
reference manual is easily found online:

<https://gcc.gnu.org/onlinedocs/>

It can be a good idea to check which version of gcc you have by

	gcc --version

Then you can look at the correct version of the manual from the website
(don't worry about the point release - if you have gcc 4.7.<something>
then look at the latest 4.7.<something else> manual without worrying
about the last number).


But while the gcc reference manual covers all the details, it's not
really a "getting started" guide.  Your best bet there is simply to
google a bit for "getting started with gcc".

[toc] | [prev] | [next] | [standalone]


#60737

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-04-22 11:15 -0400
Message-ID<5537BB13.4000909@verizon.net>
In reply to#60700
On 04/22/2015 03:35 AM, David Brown wrote:
> On 21/04/15 17:56, Morris Dovey wrote:
>> On 4/21/15 10:25 AM, Alla _ wrote:
>>> Just to add: I don't understand why the option with renaming the file
>>> has worked while the option gcc sample.c has not.
>>
>> In a terminal window, you can use the 'man' utility to view manual
>> pages. I suggest reading the page for man itself by typing
>>
>> man man
>>
>> It's helpful to remember that you can exit from man by typing 'q' for quit.
>>
>> Having done that, try
>>
>> man gcc
>>
>> and should see the answer to your question - and much, much more.
>>
> 
> The "man" page for gcc has little useful information - the real


On this particular issue, "man gcc" gives:
>        -o file
>            Place output in file file.  This applies regardless 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.



> reference manual is easily found online:
> 
> <https://gcc.gnu.org/onlinedocs/>

When I got to that source and retrieve the manual for the same version
of gcc (4.4.7), it says:

> -o file
> Place output in file file. This applies regardless 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.


Those descriptions appear to be word-for-word identical, though I might
have missed something. On what basis are you claiming that "the 'man'
page for gcc has little useful information"?

[toc] | [prev] | [next] | [standalone]


#60829

FromDavid Brown <david.brown@hesbynett.no>
Date2015-04-23 01:31 +0200
Message-ID<mh9auu$qbr$1@dont-email.me>
In reply to#60737
On 22/04/15 17:15, James Kuyper wrote:

> Those descriptions appear to be word-for-word identical, though I might
> have missed something. On what basis are you claiming that "the 'man'
> page for gcc has little useful information"?
>

Apparently on no basis at all.  The man page and the reference manual 
come from the same source.  And though I find the html and pdf formats 
far more useful for most purposes, the information content is identical.

I was under the impression that the man page only had a very compressed 
summary of the switches, not the descriptions of them.

Sorry for any confusion caused.

[toc] | [prev] | [next] | [standalone]


#60744

FromKeith Thompson <kst-u@mib.org>
Date2015-04-22 08:43 -0700
Message-ID<87h9s8kw1w.fsf@kst-u.example.com>
In reply to#60700
David Brown <david.brown@hesbynett.no> writes:
[...]
> The "man" page for gcc has little useful information - the real
> reference manual is easily found online:
>
> <https://gcc.gnu.org/onlinedocs/>

You can also use "info gcc" if you have both the GNU "info" command and
the gcc documentation installed.

-- 
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"

[toc] | [prev] | [next] | [standalone]


#60767

FromBartc <bc@freeuk.com>
Date2015-04-22 18:57 +0100
Message-ID<mh8ncu$7pd$1@dont-email.me>
In reply to#60744
On 22/04/2015 16:43, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> The "man" page for gcc has little useful information - the real
>> reference manual is easily found online:
>>
>> <https://gcc.gnu.org/onlinedocs/>
>
> You can also use "info gcc" if you have both the GNU "info" command and
> the gcc documentation installed.

The problem with gcc is that it's a monster. The only info file I can 
find on my Windows installation is "gcc.info", a 48,000-line text file 
full of literally thousands of options. Where do you start?

(And that's if you've managed to find it in the first place, especially 
if you don't know it's called gcc.info. Apparently in my version, it's 
in .\share\info. Where else could it be!)

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#60774

FromKeith Thompson <kst-u@mib.org>
Date2015-04-22 11:35 -0700
Message-ID<87pp6wj9k9.fsf@kst-u.example.com>
In reply to#60767
Bartc <bc@freeuk.com> writes:
> On 22/04/2015 16:43, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>> [...]
>>> The "man" page for gcc has little useful information - the real
>>> reference manual is easily found online:
>>>
>>> <https://gcc.gnu.org/onlinedocs/>
>>
>> You can also use "info gcc" if you have both the GNU "info" command and
>> the gcc documentation installed.
>
> The problem with gcc is that it's a monster. The only info file I can
> find on my Windows installation is "gcc.info", a 48,000-line text file
> full of literally thousands of options. Where do you start?
>
> (And that's if you've managed to find it in the first place,
> especially if you don't know it's called gcc.info. Apparently in my
> version, it's in .\share\info. Where else could it be!)

You start at the top, but you need an info viewer to view it.  If you
use emacs, it has an "info" mode; there's also a standalone "info"
program.

Given a reasonable info viewer, you'll get a view of the documentation
similar to what you'll find here:

    https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/

That's the same documentation formatted as HTML rather than in info
format.  (I think the "info" format predates HTML.)

-- 
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"

[toc] | [prev] | [next] | [standalone]


#60656

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-04-21 12:01 -0400
Message-ID<55367443.1000308@verizon.net>
In reply to#60646
On 04/21/2015 11:18 AM, Alla _ wrote:
> 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?

"-o filename" tells gcc to give the output file the specified file name.
By default, it uses a.out.

Command 1 tells gcc to compile two files, one named sample.c and the
other named sample, and to create a program named a.out from them. Since
sample has no extension, gcc can't tell what kind of file it is - my
experiments suggest that it defaults to assuming that "sample" contains
a linker script. I don't know anything about linker scripts.

Unless the resource that recommended command 1 went on to explain how to
construct and use linker scripts, ignore all advice from that resource
about using gcc.

Command 2 tells gcc to compile and link sample.c and to name the
resulting program "sample".

Command 3 tells gcc to name the output file "sample.c", but it doesn't
identify any input files. gcc understandably complains about this.
Ignore all advice you get from this resource about using gcc.

> The only command that produced results for me was gcc sample.c -o sample
> Although the book suggests simple gcc sample.c

That works, but it names the output file a.out. You'll generally want to
rename the your program to something else, so it's simpler instruct gcc
to give it the right name from the beginning.

[toc] | [prev] | [next] | [standalone]


#60657

FromJohn Gordon <gordon@panix.com>
Date2015-04-21 16:01 +0000
Message-ID<mh5s9k$l9k$1@reader1.panix.com>
In reply to#60646
In <9f1b89ff-cf47-4e01-b137-ba263e9e75f1@googlegroups.com> Alla _ <modelling.data@gmail.com> writes:

> Hello!

> Different resources advice different commands to compile a file:

> 1) gcc sample.c sample

This tells gcc to compile the files "sample.c" and "sample".

> 2) gcc sample.c -o sample

This tells gcc to compile the file "sample.c" and create an output file
named "sample" instead of the default name.

> 3) gcc -o sample.c

This command is an error, because you haven't specified any files
to compile.

> What does -o stand for? 

It tells gcc to use a specific output filename instead of the default
name.

These are very basic questions; I suggest you do a web search for a gcc
tutorial.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#60661

FromTim Prince <tprince@computer.org>
Date2015-04-21 13:11 -0400
Message-ID<cpnemcF8vnaU1@mid.individual.net>
In reply to#60657
On 4/21/2015 12:01 PM, John Gordon wrote:
> In <9f1b89ff-cf47-4e01-b137-ba263e9e75f1@googlegroups.com> Alla _ <modelling.data@gmail.com> writes:
> 
>> Hello!
> 
>> Different resources advice different commands to compile a file:
> 
>> 1) gcc sample.c sample
> 
> This tells gcc to compile the files "sample.c" and "sample".
> 
>> 2) gcc sample.c -o sample
> 
> This tells gcc to compile the file "sample.c" and create an output file
> named "sample" instead of the default name.
> 
>> 3) gcc -o sample.c
> 
> This command is an error, because you haven't specified any files
> to compile.
> 
>> What does -o stand for? 
> 
> It tells gcc to use a specific output filename instead of the default
> name.
> 
> These are very basic questions; I suggest you do a web search for a gcc
> tutorial.
> 
and, once you have consulted such tutorials, gcc-help mail list is the
place to ask questions specific to gcc.

[toc] | [prev] | [next] | [standalone]


#60674

FromKeith Thompson <kst-u@mib.org>
Date2015-04-21 12:09 -0700
Message-ID<877ft5mh7v.fsf@kst-u.example.com>
In reply to#60657
John Gordon <gordon@panix.com> writes:
> In <9f1b89ff-cf47-4e01-b137-ba263e9e75f1@googlegroups.com> Alla _ <modelling.data@gmail.com> writes:
>> Different resources advice different commands to compile a file:
>
>> 1) gcc sample.c sample
>
> This tells gcc to compile the files "sample.c" and "sample".

No, it doesn't.  The file name "sample" doesn't have an extension,
so gcc isn't going to assume it's a source file to be compiled.
(Apparently it turns out that it assumes it's a linker script,
which is not what I would have guessed.)

Unless you already know exactly what gcc does with
    gcc sample.c sample
I think it's safe to assume that it's simply incorrect.

-- 
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"

[toc] | [prev] | [next] | [standalone]


#60665

FromBartc <bc@freeuk.com>
Date2015-04-21 18:25 +0100
Message-ID<mh615a$db6$1@dont-email.me>
In reply to#60646
On 21/04/2015 16:18, Alla _ wrote:
> 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

I don't understand what (1) and (3) do.

  gcc sample.c -o sample

compiles "sample.c" into an object code (I think "sample.o") and links 
that into the executable file "sample", which you then have to run using:

./sample

gcc sample.c

compiles "sample.c" into an object file "sample.c", and then links that 
into the executable file "a.out" (don't ask why), which you have to run 
using:

./a.out

gcc -c sample.c

compiles "sample.c" into an object file "sample.c" only; it doesn't 
create an executable.

Summary:

* Every .c input file is compiled into a corresponding .o file.

* gcc will always link all input files (.c or already-compiled .o) into 
an executable unless -c is used.

* gcc will always name the executable "a.out" unless -o is used.

That's about the extent of my knowledge of gcc.

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#60675

FromKeith Thompson <kst-u@mib.org>
Date2015-04-21 12:11 -0700
Message-ID<87383tmh4i.fsf@kst-u.example.com>
In reply to#60665
Bartc <bc@freeuk.com> writes:
[...]
> gcc sample.c
>
> compiles "sample.c" into an object file "sample.c", and then links
> that into the executable file "a.out" (don't ask why), which you have
> to run using:
>
> ./a.out

I presume that was a typo; the object file is named "sample.o".

> gcc -c sample.c
>
> compiles "sample.c" into an object file "sample.c" only; it doesn't
> create an executable.
>
> Summary:
>
> * Every .c input file is compiled into a corresponding .o file.
>
> * gcc will always link all input files (.c or already-compiled .o)
> into an executable unless -c is used.
>
> * gcc will always name the executable "a.out" unless -o is used.

If you tell it to produce an executable (by not specifying "-c"), any
generated .o files are automatically deleted.

-- 
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"

[toc] | [prev] | [next] | [standalone]


#60669

FromKeith Thompson <kst-u@mib.org>
Date2015-04-21 11:16 -0700
Message-ID<87fv7tmjn1.fsf@kst-u.example.com>
In reply to#60646
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"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web