LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-02-2015, 09:44 AM   #1
/dev/dog
LQ Newbie
 
Registered: Nov 2014
Location: New Jersey, US
Distribution: Debian
Posts: 29

Rep: Reputation: 0
Question ./ needed before C executable


Hi All

So I was teaching my cousin how to code, and I looked stupid in front of her when I couldn't get a simple Hello World program to output to the command line.

The code is right, and gcc compiled no problems. I spent 15 minutes trying to get it to work. I was trying to run
Code:
test
without the "./" before it. The problem is that there were no errors and no warnings. It seemed like my code just had a run time error, which didn't make sense because it was this:
Code:
#include <stdio.h>
int main() {
printf("Hello World");
return 0;}
to actually get it to work I have to run
Code:
./test
I know I can add that to my ~/bin and then I don't need to type in the "./"

My question is: WHY is "./" necessary? And why does running the file without "./" cause no errors yet does not run correctly?

Thank you very much

Last edited by /dev/dog; 01-02-2015 at 09:46 AM. Reason: "#include <stdio.h>"
 
Old 01-02-2015, 09:49 AM   #2
cepheus11
Member
 
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 269

Rep: Reputation: 83
"test" is a program. Type

Code:
which test
to see where it is. Usually /usr/bin/test. Without arguments, it simply does nothing and returns with error code. Path priority is so that this program is found before your program, or your program is not found at all.

Last edited by cepheus11; 01-02-2015 at 10:16 AM. Reason: typo
 
2 members found this post helpful.
Old 01-02-2015, 10:11 AM   #3
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 5,996

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
To put it briefly the path environmental variable tells Linux where to look for executables. This path does not include the current working directory by default (as it does in, say, Windows) because this could be a security risk or simply cause the wrong thing to be executed. Suppose, of you will, you accidentally create an executable called "cd", "rm", "mv" or similar and then try to execute those commands in the diretory in which executable resides. Now suppose that you regularly use directories which other, whom you may not trust completely, are able to write to.
It could, perhaps, be argues that this is less important nowadays but, as with a lot of these security related "quirks" on Linux it is part of Linux's security as a whole.
 
1 members found this post helpful.
Old 01-02-2015, 10:27 AM   #4
/dev/dog
LQ Newbie
 
Registered: Nov 2014
Location: New Jersey, US
Distribution: Debian
Posts: 29

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by cepheus11 View Post
"test" is a program. Type

Code:
which test
to see where it is. Usually /usr/bin/test. Without arguments, it simply does nothing and returns with error code. Path priority is so that this program is found before your program, or your program is not found at all.

Amazing! I did not know that, that is very clarifying! It did not even cross my mind that test is a program. And thank you 273 for explaining that my ~/bin/test would be found last. Yes, test resides /usr/bin/test.

After renaming my executable to testt, and setting my Makefile to place it in ~/bin/testt, it works without having to specify the path.

Thank you very much guys!
 
Old 01-02-2015, 10:32 AM   #5
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 5,996

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
Glad to help, I meant to mention that you can change the order in which Linux searches by changing the order in the path environmental variable but I would't recommend that, for the reasons mentioned in my previous post regarding security, unless you research and know exactly what you're doing.
 
Old 01-02-2015, 01:17 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by /dev/dog View Post
Amazing! I did not know that, that is very clarifying! It did not even cross my mind that test is a program.
It's even worse than that. The test command is used so frequently that it is also built into most shells, as you will see if you run "type test". That means that no matter how you set up the search sequence in your PATH variable, the shell will never run either your program or the one stored in /bin or /usr/bin.

You learn quickly that when you are trying to write a program, do not call it "test". (Personally, I use "try".)
 
  


Reply

Tags
command line, gcc, printf, stdout


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
Executable Ace Blackwell Programming 3 05-01-2008 02:55 AM
XGL needed for Beryl is conflicting with OpenGL needed for Cedega chunkeydelight Linux - Desktop 2 07-18-2007 06:50 AM
Errors installing Q, which is needed for Lex, which is needed for PHP Virtuality Linux - Software 1 05-29-2007 05:47 PM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 05:57 AM
Non executable stack with Linux -- && 64bit CPU needed? lelolas Linux - Security 5 07-25-2004 10:25 PM


All times are GMT -5. The time now is 02:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration