ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.