Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Fist steps in compiling, gcc commands confusion Date: Tue, 21 Apr 2015 11:16:50 -0700 Organization: None to speak of Lines: 85 Message-ID: <87fv7tmjn1.fsf@kst-u.example.com> References: <9f1b89ff-cf47-4e01-b137-ba263e9e75f1@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="9be37d943435275679235b07c7947eef"; logging-data="24283"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+hqqFpvrhEnNY/lFRGbSwe" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:lGt/HiMNAvzvSVtHr4gcJGR2q98= sha1:j+q+g7j4FuO0UOVDpL2EnBHj5fM= Xref: csiph.com comp.lang.c:60669 Alla _ 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 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"