LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2009, 05:34 PM   #1
tigertim71
Member
 
Registered: Oct 2008
Location: London, England
Distribution: Puppy
Posts: 93

Rep: Reputation: 15
first program in C in Ubuntu


Hi-

I'm trying to get a first program going in C in Ubuntu 8.04.

It's the usual "hello world" stuff the books introduce you to but there's a few things I can't understand at this stage.

The book mentions a /home/MyPrograms directory. I can't seem to find this at all? And should my program be saved in a text document labeled (name).c
eg. hello.c?

From what I can see the final commands are executed in the terminal.

Could someone please explain...


Thanks
 
Old 02-28-2009, 05:42 PM   #2
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 58
You would want to make a directory called /home/MyPrograms. Yes, you should name your source code files with the .c extension.
To compile a helloworld.c file eg.
Code:
include <stdio.h>

int main(){
    printf("Hello World");
    return 0;
}
You would want to run this command to compile it:
Code:
gcc helloworld.c -o helloworld
That creates a program called helloworld
To run it, first make sure its executable by running
Code:
chmod +x helloworld
Then run
Code:
./helloworld
and that should output Hello World

Last edited by AceofSpades19; 02-28-2009 at 05:45 PM.
 
Old 02-28-2009, 05:46 PM   #3
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: Debian 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,766

Rep: Reputation: Disabled
#mkdir ./MyPrograms

save your text file as hello.c there

#cd ./MyPrograms


read the apt-get manual as well
 
Old 02-28-2009, 06:47 PM   #4
tigertim71
Member
 
Registered: Oct 2008
Location: London, England
Distribution: Puppy
Posts: 93

Original Poster
Rep: Reputation: 15
first c program in ubuntu

I did a couple of things but no result.

The first was to manually create a MyPrograms directory in /home/myusername, put the home code in there in a home.c text file, but all I got was

Code:
gcc: hello.c: No such file or directory
gcc: no input files
after putting in the terminal instructions gcc hello.c -o hello.exe

I then tried the
Code:
#mkdir ./MyPrograms
(as below), after deleting my manual entry (as above) but could not find the directory.

Any suggestions?
 
Old 02-28-2009, 07:34 PM   #5
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 58
Quote:
Originally Posted by tigertim71 View Post
I did a couple of things but no result.

The first was to manually create a MyPrograms directory in /home/myusername, put the home code in there in a home.c text file, but all I got was

Code:
gcc: hello.c: No such file or directory
gcc: no input files
after putting in the terminal instructions gcc hello.c -o hello.exe

I then tried the
Code:
#mkdir ./MyPrograms
(as below), after deleting my manual entry (as above) but could not find the directory.

Any suggestions?
if you have a filename called home.c, you wouldn't compile it with
gcc hello.c -o hello, you would compile it with gcc home.c -o home and you do not need to put the .exe extension for a program
 
Old 03-01-2009, 02:41 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, Tigertim71 -

I wouldn't worry about the "/home/MyPrograms" stuff. Just focus on compiling your program:
Quote:
1. Create your "hello.c" text file:
Code:
#include <stdio.h>

int
main (int argc, char *argv[])
{
  printf ("hello world!\n");
  return 0;
}
2. Verify that the file exists (this is actually important):
Quote:
ls -l hello.c
<= If you don't see "hello.c" when you type this "ls -l" command, you're going to have problems. Post back if you have any problems up to this point
3. Compile and link the file ("gcc" does both things for you at once):
Quote:
gcc -g -Wall -o hello hello.c
<= These extra "flags" ("-g" and "-Wall") will help you debug more advanced programs you write
4. Execute:
Quote:
./hello
'Hope that helps .. PSM

PS:
Linux offers many IDE's (kdevelop and Eclipse, among many others) to simplify compiling and debugging programs, should you prefer.

Last edited by paulsm4; 03-01-2009 at 02:43 AM.
 
Old 03-01-2009, 04:02 AM   #7
tigertim71
Member
 
Registered: Oct 2008
Location: London, England
Distribution: Puppy
Posts: 93

Original Poster
Rep: Reputation: 15
fist c program in ubuntu

Hi paulsm4,

Thanks for your post. I'm stuck on stage 2 and am looking into other areas of investigation as to why this is going wrong.

I'm quite new to Ubuntu so am wondering if this could be another reason:

Originally, my system is set up as a dual boot with XP. Going into gparted everything looks fine. But looking in the Ubuntu system monitor I get this:

device directory type total free available used
/dev/sda3 / ext3 9.2 GB 6.4 GB 6.0 GB 2.8 GB
/dev/sda4 /home ext3 36.0 GB 35.8 GB 34.0 GB 216.2 MB
gvfs-fuse-daemon /home/mysystemname/.gvfs fuse.gvfs-fuse-daemon
9.2 GB 6.4 GB 6.0 GB 2.8 GB
/dev/sda1 windows fuseblk 55.9 GB 41.3 GB 41.3 GB 14.6 GB

Why is / exactly the same as /home/mysystemname.gvfs or is this just normal? Could this be causing a conflict with trying to locate the hello.c file?
 
Old 03-01-2009, 11:49 AM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Q: How did you create "hello.c"
<= the way you edited this file might give some clues as to where we can find it
Quote:
EXAMPLE:
1. I log in to Ubuntu 8.04 (I happen to be using the Gnome desktop)

2. I bring up a command prompt:
Applications, Accessories, Terminal
Quote:
pwd
/home/paulsm
<= This tells me the directory my command prompt is in
3. I start a text editor:
Applications, Accessories, Text editor
a) I type in "hello world"
b) I "Save"
Name= hello.c
Save in Folder= paulsm
[Save]

4. Back in the command prompt, I type "ls -l", and I see "hello.c" (I see it because I'm in directory "/home/paulsm").

5. I look for "hello.c":
Applications, Places, Search for Files...
Name contains= hello.c
Look in folder= paulsm
[Find]
pwd

Last edited by paulsm4; 03-01-2009 at 12:10 PM.
 
Old 03-01-2009, 02:44 PM   #9
tigertim71
Member
 
Registered: Oct 2008
Location: London, England
Distribution: Puppy
Posts: 93

Original Poster
Rep: Reputation: 15
first C program in Ubuntu

Hi paulsm4,

OK- the good news is that I've passed stage 2 of your first post. I did notice however that the system seems to prefer it if files are kept in my own home directory and not in a new folder eg /MyPrograms.

I also noticed 2 hello files after entering "ls -l":

77 2009-03-01 19:00 hello.c
99 2009-03-01 18:43 hello.c~

but in search for files, there is only 1 so this may not be significant.

However, at stage 3, when I enter
Code:
gcc -g -Wall -o hello hello.c
I get the following message:

Code:
hello.c:1:19: error: stdio.h: No such file or directory
hello.c: In function ‘main’:
hello.c:6: warning: implicit declaration of function ‘printf’
hello.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
but at least I'm passed stage 2.....
 
Old 03-01-2009, 03:16 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

It sounds like you're nearly home free (and apologies that you've invested this much effort - things usually go a lot quicker and smoother. Honestly!)

ANYWAY:
1. From your command prompt, type:
Quote:
dpkg -l|grep -i gcc
ii gcc 4:4.2.3-1ubuntu6 The GNU C compiler
ii gcc-4.2 4.2.3-2ubuntu7 The GNU C compiler
ii gcc-4.2-base 4.2.3-2ubuntu7 The GNU Compiler Collection (base package)
ii libgcc1 1:4.2.3-2ubuntu7 GCC support library
ii libgomp1 4.2.3-2ubuntu7 GCC OpenMP (GOMP) support library
<= I'm guessing you *have* gcc (the compiler), but you need to install the C runtime headers and libraries
2. Use the Synaptic GUI to install:
Administration, Synaptic, Search, gcc
<= Install:
gcc-4.2 (or whatever your current version is)
gcc-4.2-base (this is probably what's causing the error)
gcc-4.2-doc (optional...but helpful)
libgccl (if you're missing the .h headers, you should also make sure you have all the C runtime libraries, too)
<= Install g++

3. If you did a "File, Save as" hello.c to "/home/MyPrograms", you should be able to compile it like this:
Quote:
gcc -o hello /home/MyPrograms/hello.c
... OR ...
cd /home/MyPrograms
gcc -o hello hello.c
There's nothing "magic" about /home/MyPrograms

4. The "hello.c~" is almost certainly either a temp file (created by your text editor), or a backup file (created by your text editor)

5. For more complicated programs, obviously, you won't be typing "gcc -o hello hello.c" on a command line. You'll probably instead use tools like "make" (for batch-oriented builds from a command line), or "kdevelop" (an interactive GUI for creating and debugging programs).

'Hope that helps .. PSM
 
Old 03-01-2009, 06:01 PM   #11
tigertim71
Member
 
Registered: Oct 2008
Location: London, England
Distribution: Puppy
Posts: 93

Original Poster
Rep: Reputation: 15
Thumbs up first program in Ubuntu using C

Hi paulsm4,

Many Thanks. I have faith now that the best motto is: don't give up.

I just saw "hello world!" on my screen...
 
  


Reply



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
Is There a Better Mouse Program for Ubuntu 7.10 ?? taurusx5 Linux - Software 4 04-20-2008 06:44 PM
Need a Frontpage like Program for Ubuntu 7.10 vwtech Linux - Software 2 12-29-2007 11:58 PM
Ubuntu Tune-Up Program? zoso375 Linux - General 3 11-22-2007 11:45 PM
Compile a c++ program in Ubuntu 5.10 nuwandias Ubuntu 8 03-16-2007 12:55 PM
Program for Ubuntu linux4life88 Ubuntu 2 05-20-2005 03:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

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