LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-07-2005, 03:48 AM   #1
rchillal
LQ Newbie
 
Registered: Feb 2005
Posts: 10

Rep: Reputation: 0
Compiling C++ programs


I compiled my first C++ program on Linux
> g++ -c HelloWorld.cpp -o Hello

Now when I try to run the executable "Hello"
>./Hello
I get
> permission denied

where as if
>g++ -c HelloWorld.cpp
>g++ -o HelloWorld.o Hello
>./Hello

It runs quite properly ! Is there something wrong in compiling ? How do I compile & create an executable in a single command ?

Also how do I directly run my executables (C & C++) without having to always prefix it with "./" ?
 
Old 02-07-2005, 04:31 AM   #2
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 30
Hi,

I do not know about the problem with the permissions, but if you want to run programs in the current directory you have to modify your PATH variable and add ":." at the end. If you use bash as shell, then modify your .bashrc. Notice however that you do not want to do this for the root user, as it could be a security threat.

Good luck...
 
Old 02-07-2005, 04:57 AM   #3
png
LQ Newbie
 
Registered: Oct 2004
Posts: 19

Rep: Reputation: 0
You can compile and link a program using the syntax you mention:

g++ -o program program.cpp

You won't need to issue the "g++ -c ..." command first as it is all done in the command above.
 
Old 02-07-2005, 05:03 AM   #4
harken
Member
 
Registered: Jan 2005
Location: Between the chair and the desk
Distribution: Debian Sarge, kernel 2.6.13
Posts: 666

Rep: Reputation: 30
Quote:
g++ -o program program.cpp
I think it's
Code:
g++ -o program.cpp program
As for what enemorales said, if you're in the same directory as the executable you don't need to add it to your path. Just issue './programname'.
Security threat?
 
Old 02-07-2005, 05:09 AM   #5
rchillal
LQ Newbie
 
Registered: Feb 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks, I will try that out.

But still, nobody has answered to my question

"how do I directly run my executables (C & C++) without having to always prefix it with "./" ?"

Because, when I had installed RED HAT 6 (now I have Mandrake 10.1), I never had to use this prefix to run my executables.
 
Old 02-07-2005, 05:48 AM   #6
JunctaJuvant
Member
 
Registered: May 2003
Location: Wageningen, the Netherlands
Distribution: OS X
Posts: 488

Rep: Reputation: 31
The following entry adds the current directory to the $PATH variable of a normal user in Slackware Linux (in my case it is in /etc/profile, maybe it is the same for you).
Code:
# For non-root users, add the current directory to the search path:
if [ ! "`id -u`" = "0" ]; then
 PATH="$PATH:."
fi
Hope that helps.
 
Old 02-07-2005, 05:56 AM   #7
png
LQ Newbie
 
Registered: Oct 2004
Posts: 19

Rep: Reputation: 0
Ok, I know it's annoying but it's a basic security measure. By having you type "./" in front of any program in the current dir, you can be sure no one can trick you into running a fake system command (eg, a modified 'ls' executable in your $HOME). That's why "." is not in the PATH by default.
 
Old 02-07-2005, 05:58 AM   #8
png
LQ Newbie
 
Registered: Oct 2004
Posts: 19

Rep: Reputation: 0
And no, the -o option tells g++ the desired name for the output file. If you omit it, g++ will name your executable "a.out".
 
Old 02-07-2005, 07:10 AM   #9
JunctaJuvant
Member
 
Registered: May 2003
Location: Wageningen, the Netherlands
Distribution: OS X
Posts: 488

Rep: Reputation: 31
Doesn't the order in which the path is searched prevent the malicious "ls" from being executed, since the real "ls" command is found first? Just wondering.
Also, I have tested on my system with two instances of the same c program, called "ls" and "ls.bin" respectively (both only printing "hi" and then quiting). Typing "ls.bin" ran the program fine, but with "ls" it only worked by typing "./ls"
So it appears that on my system I can only run programs in this way that have an extension (like .exe, or .bin, or .abighairylamawithpurpledots), maybe it is the same with other systems.
 
Old 02-07-2005, 09:33 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,814

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Extensions do not mean anything in linux. All that is required is the executable permission be set. So ls.bin is not the same thing as ls.
 
Old 02-07-2005, 09:43 AM   #11
png
LQ Newbie
 
Registered: Oct 2004
Posts: 19

Rep: Reputation: 0
Yes, I have just tested and it happens right like that. The weird thing is, if I run 'which ls', I get the expected answer: my version of 'ls' is the one to be executed.

I have made a quick search on bash builtin commands but 'ls' is not among them.

This is quite a mystery.
 
Old 02-07-2005, 10:03 AM   #12
JunctaJuvant
Member
 
Registered: May 2003
Location: Wageningen, the Netherlands
Distribution: OS X
Posts: 488

Rep: Reputation: 31
That is what I thought too, which is why I literally tried the silly extension and it worked!
If I rename both files to "test.bladiebla" and "test", then "test.bladiebla" executes without "./"
However, "test" still requires the "./" even though the permissions on both programs are the same. And yet I do not get a "no such file or directory" error, simply a newline as if I just pressed enter on a blank line.
As root user it is indeed not possible for me to run "test.bin" without "./", which is what I expected. And it does give the bash error message.
Does anyone know why this is?
 
Old 02-07-2005, 12:45 PM   #13
JunctaJuvant
Member
 
Registered: May 2003
Location: Wageningen, the Netherlands
Distribution: OS X
Posts: 488

Rep: Reputation: 31
ok I'm an idiot. See "man test", lol
 
Old 02-08-2005, 05:27 AM   #14
png
LQ Newbie
 
Registered: Oct 2004
Posts: 19

Rep: Reputation: 0
Well... I have to thank a freebsd-using friend to point me in the right direction.

The thing is, bash keeps a table with all the results it gets from PATH searches. So, when it has found a program somewhere in the PATH, it never searches for it again because its location is unlikely to change.

That is why yesterday, when I made my own 'ls', I kept running /bin/ls instead of /home/png/bin/ls.

There is a bash builtin command, hash , that shows the current contents of the search table. If you do the same test as I did, and then run 'hash', you can see exactly which version of 'ls' will be executed. Furthermore, if you erase that executable, bash will no longer be able to find it and will complain about it, even when there is another executable with the same name in the path!!!

How to fix that?

When an executable changes its location, you need to tell bash "forget what you assumed about this program" so that it will search again next time. In bash, this is done with the following command:

hash -r

This erases the full hash table.

You can see more about this in bash(1), in the "BASH BUILTIN COMMANDS" section.

BTW, in freebsd there is also the 'rehash' command that makes the shell rebuild the table.
 
  


Reply


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
Compiling Programs Faxanadu Linux - Software 3 05-30-2005 01:51 AM
Compiling Programs Thorrn4 Linux - Software 6 03-26-2005 12:25 AM
Compiling Programs mike35 Linux - Newbie 2 10-22-2003 09:39 AM
compiling c,c++ programs ksd Linux - Newbie 9 10-13-2003 08:23 AM
Compiling Programs Jabber63 Linux - Software 3 08-06-2003 02:58 AM


All times are GMT -5. The time now is 12:16 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration