Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-25-2007, 10:15 PM   #1
LQ Newbie
Registered: Jun 2007
Posts: 10

Rep: Reputation: 0
Help in C programming in Linux

Hi there.

I just wrote my first C prg in Linux (let's call it sample.c).... How can I compile it?

Old 06-25-2007, 10:26 PM   #2
Senior Member
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
try this:
gcc -o sample sample.c
it will call the ("gcc") compiler and instruct it ("-o sample") to build the program "sample" from the source code "sample.c".
Old 06-25-2007, 10:39 PM   #3
LQ Newbie
Registered: Jun 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Hi Patrick. I tried the gcc command you suggested, there are no errors and it built the program called sample. however, when I excute sample from the prompt it tells me that "sample: command not found"... I do not see the object file though, should it create a file called "sample.o"? Thanks.
Old 06-25-2007, 10:46 PM   #4
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,342

Rep: Reputation: 341Reputation: 341Reputation: 341Reputation: 341

if you are in the same directory
Old 06-25-2007, 10:50 PM   #5
LQ Newbie
Registered: Jun 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Ok, that helped. thanks Dive

Ok, that helped. thanks Dive
Old 06-26-2007, 09:04 AM   #6
Registered: Oct 2005
Distribution: Ubuntu 12.04 X86_64
Posts: 960

Rep: Reputation: 35
the reason that you dont see the .o files or all the other stuff usially created with a compiler is that GCC by default ether pipes output between processes or deletes the files afterwards,

if you need the filles for somthing there is an option to keep them

try man gcc for more details
Old 06-27-2007, 06:19 AM   #7
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
gcc -c ...
is for just compiling .c files into .o (no linking).

gcc -o prog ...
will compile the code if needed and then link it together into the program.
You can give both .c files (that will be compiled) and .o files
(previously compiled) as input for the linking.

Note that the "-o prog" is optional. If you don't specify the name of the output program that is to be created, it will be named "a.out" by default.

I suggest you read up on Makefiles and the "make" command too.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Forum for Linux-Gurus[Programming/Administration] or Linux-Advanced Users [ Progr raees LQ Suggestions & Feedback 5 04-12-2005 08:35 PM
Exp. Windows/Linux-Script Programmer - Need Linux Programming Mentor! ruttiger Linux - Newbie 2 10-28-2001 12:00 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration