LinuxQuestions.org
Review your favorite Linux distribution.
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 06-05-2010, 02:41 AM   #1
bcbotha
Member
 
Registered: Nov 2008
Location: South Africa
Distribution: Ubuntu, Windows
Posts: 151

Rep: Reputation: 16
new C programmer and Hello, World is not working


Hi I'm trying to learn C programming using a Fedora 12 system, I have gcc version 4.4.2 20091027 installed and I'm trying to use Nano as my text editor. I created the source code for hello, world and saved it as hello.c, I then compiled it using
Code:
$ gcc hello.c
a.out was then created but when I type
Code:
$ a.out
to execute it I get this
Code:
$ bash: a.out: command not found
Please help me find what I'm missing or not doing.

Thanks
 
Old 06-05-2010, 02:49 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
./a.out should do it.
 
Old 06-05-2010, 02:54 AM   #3
bcbotha
Member
 
Registered: Nov 2008
Location: South Africa
Distribution: Ubuntu, Windows
Posts: 151

Original Poster
Rep: Reputation: 16
Thanks for the quick reply jiml8 but this is what the command returned:
Code:
$ bash ./a.out: No such file or directory
When I saved the source code from nano it autosaved in my brendan@home~]$ directory, and when I compiled it the a.out file is there too.
 
Old 06-05-2010, 02:58 AM   #4
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
please list the contents of your current directory..
 
0 members found this post helpful.
Old 06-05-2010, 03:00 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

~/a.out

But it would be easier if you are standing in the directory where you create the hello.c file and where the compiled version is (the a.out file). If this is the case, jiml8's original answer works.

BTW: you might want to include the -o <name> option, this creates a more readable c file. I.e.: gcc hello.c -o hello, will create an executable hello file instead of a plain a.out.

Hope this helps.
 
Old 06-05-2010, 03:10 AM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
~/a.out only works if the file is in the root of his home directory.

Basically, cd into the directory where you compiled the file. ./filename means "execute filename which is in the current directory". If you are in that directory and you specified ./a.out then either the executable was created without execute permission (which would be very strange) or the executable was not created, which means the compiler bombed, or you created the executable using the -o option.
 
Old 06-05-2010, 03:10 AM   #7
bcbotha
Member
 
Registered: Nov 2008
Location: South Africa
Distribution: Ubuntu, Windows
Posts: 151

Original Poster
Rep: Reputation: 16
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .config
C_programmes .dbus Desktop .dmrc Documents Downloads .esd_auth .gconf
.gconfd .gnome2 .gnome2_private .gnote .gnupg .gstreamer-0.10 .gtk-bookmarks .gvfs
.ICEauthority .icedteaplugin .icons .imsettings.log .local .mozilla Music .nautilus
Pictures Public .pulse .pulse-cookie .recently-used.xbel.ssh Templates .themes
.thumbnails Videos .xsession-errors .xsession-errors.old
 
Old 06-05-2010, 03:15 AM   #8
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
This proves the value of the hello program

This really showcases the value of the seemingly trivial hello program.
 
Old 06-05-2010, 03:27 AM   #9
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
I would try

ls C_programmes

and if you can then see the hello.c and a.out, change to that subdirectory with

cd C_programmes

Then, just to make sure, list the new current directory with a simple

ls

to make sure that you have moved into the directory which contains hello.c and a.out.

When you are in the directory with a.out, you should be able to run it with

./a.out

You need to prefix the name of the executable with ./ because Linux does not look in the current directory for executable programs. There is a search path in your environment that Linux uses to search for executable commands, but, by default, that search path does not include your current directory.
 
0 members found this post helpful.
Old 06-05-2010, 04:07 AM   #10
bcbotha
Member
 
Registered: Nov 2008
Location: South Africa
Distribution: Ubuntu, Windows
Posts: 151

Original Poster
Rep: Reputation: 16
OK.... IT WORKS.
I had to copy the hello.c file to C_programmes then compile it, then run it with ./a.out.
Thanks Arthur, I didn't know linux wouldn't execute from current directory.

Thanks guys for your help
 
Old 06-05-2010, 10:35 AM   #11
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Linux WILL execute from current directory but by default the bash shell does not include the current directory in the path. This is a security mechanism. To execute from the current directory, you have to preface the command with ./ to explicitly tell bash that you want the file in the current directory.
 
1 members found this post helpful.
  


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
Hello World Module not working vrishankaryan Linux - Newbie 1 08-30-2007 09:14 PM
Working in a Windows AD world :( PB0711 Linux - General 1 10-13-2006 06:23 AM
Programmer to Programmer ( Long Story Of A GUI ) mdoubledragon Programming 1 10-13-2005 05:41 PM
Ethernet card not working in FC3.. Anybody in the world Who can help me out .. ozzie_penguin Linux - Hardware 1 08-17-2005 04:12 AM
cpp hello world not working harmster Programming 3 09-14-2004 02:21 PM

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

All times are GMT -5. The time now is 10:34 PM.

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