LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-14-2004, 11:42 PM   #1
brancheb
LQ Newbie
 
Registered: May 2004
Posts: 3

Rep: Reputation: 0
Executable compiled w/gcc won't run


Hello,

I'm a Linux newbie and gcc is stumping me. I am running gcc 3.3.2 under Suse Linux 9.0 personal. I created the canonical hello.c to test the compiler. The code snippet is:

#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}

It appears to compile properly, but it won't run! The following is a copy of the console screen that describes the behavior:

ebranch@dhcppc5:~/Documents> gcc -o hello hello.c
ebranch@dhcppc5:~/Documents> hello
bash: hello: command not found
ebranch@dhcppc5:~/Documents> ls -l
total 15035
-rwxr-xr-x 1 ebranch users 9866 2004-05-15 00:08 factorial
-rw-r--r-- 1 ebranch users 2875 2004-05-15 00:07 factorial.c
-rw------- 1 ebranch users 2082834 2004-04-14 18:55 gcc.ps
-rwxr-xr-x 1 ebranch users 8231 2004-05-15 00:10 hello
-rw-r--r-- 1 ebranch users 73 2004-05-14 22:50 hello.c
-rw------- 1 root root 13260800 2004-04-23 20:47 vide-linux-2.00.tar
ebranch@dhcppc5:~/Documents>

By the way, I did the same thing with factorial.c, which is in the public domain and I get the same behavior. What am I doing wrong?

Thanks in advance,

Elliott Branch
 
Old 05-14-2004, 11:49 PM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
type

./hello

typing

hello

tells bash to look in the PATH variables for which directories the program is at
 
Old 05-15-2004, 07:50 AM   #3
brancheb
LQ Newbie
 
Registered: May 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Red face

Hmmm, I'm not in Windows anymore. Duh!!!

Much thanks,

Elliott
 
Old 05-15-2004, 08:05 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
You should consider changing your path in .bashrc or .profile to something like this
Code:
PATH=.:$PATH
export PATH
This makes the current working directory part of your path.
 
Old 05-15-2004, 08:09 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
To explain that a bit:
Unlike windows/DOS, Linux/UNIX does not look for the program to start in the current working directory. Say, you are in your home-directory: /home/brancheb (type "pwd" to verify) and you program "hello" is in your home directory. Then entering "hello" on the shell prompt, the shell will only look for the program "hello" to execute in the directories listed in the environment vanriable PATH (the "search path).

Type "echo $PATH" to see what directories the shell wil search for programs.
To solve the problem, there are several possibilities:

1) Start the program by specifying the directory where the program is. The dot: "." specifies the current working directory. So, start the program with "./hello" (like SciYro said) if you are in the directory where the program is. Or the entire path: /home/brancheb/hello. Your home-directory is also in an environment variable: HOME. So you can also start your program with $HOME/hello. Or use the shell's shrotcut for the home-directory: ~/hello.

2) Put the directory where the program is, temporarily in the search path (PATH):
export PATH=$PATH:/home/brancheb
Then you can start it with just "hello". But when you exit the shell (close the xterm, or log out), you'll have to enter the export PATH... command again to make this happen.

3) Log in as root and copy your program to one of the directories listed in PATH (check: "echo $PATH"), preferably in /usr/local/bin if this is in your PATH (depends on your flavour of GNU/Linux)

4) Make a "bin" directory in your home-directory: "mkdir /home/brancheb/bin", copy your program (and furure program) there ("cp hello bin"). Then edit your ~/.bashrc file: Put the line below at the end of this file:

export PATH=$PATH:$HOME/bin

Now the directory /home/brancheb/bin will be permanently part of the search path, but only for the user "brancheb" (counting from the first next time you log in!)

5) Make it work like windows/DOS, by putting the current working directory ( . ) in your search path temporarily by entering the command:

export PATH=$PATH:.

or permanently by putting this line at the end of your ~/.bashrc file.
Note that this practice is considered unsafe. This especially true for the root account: NEVER put the current working directory in the search path of root!. However some flavours of Linux configure this for normal user accounts. Slackware even does this for root (not sure if slackware still does this, but in the past they did). Again: generally this is considered bad practice.
 
Old 05-16-2004, 01:56 PM   #6
Squall
Member
 
Registered: Jan 2004
Location: The land of the free and the home of the brave
Distribution: Slack 10
Posts: 239

Rep: Reputation: 31
"PATH=.:$PATH
export PATH"

Correct me if I'm wrong, but that would be the same thing as typing PATH=$PATH:$PWD wouldn't it?

Btw, you don't need to type both those lines in linux. In linux you could just type "export PATH=.:$PATH" instead of "PATH=.:$PATH" and "export PATH"
 
  


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
version GLIBC_2.3 not found error when cross compiled executable is run on targert sateeshgalla Programming 3 05-14-2008 10:42 AM
Run executable file compiled by kylix and mysql in Fedora Core 4 aa_husni Fedora 0 09-15-2005 12:14 AM
gcc can't create executable Aeion Linux - Newbie 1 04-06-2004 12:03 PM
To big executable with gcc thrix Programming 8 01-22-2004 04:43 PM
i want smaller executable with gcc ! thrix Programming 6 01-22-2004 10:50 AM

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

All times are GMT -5. The time now is 10:12 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