Jump to content
Science Forums

Recommended Posts

Posted

I have a problem running hello world and I really don't see what I do wrong. I wrote in emacs in c++ mode:

#include<iostream>
using namespace std;
int main()
{cout<<"hello world";}

save it as hello.cpp, then I type "M-x compile" and write "g++ -o hello hello.cpp". This makes me a file called hello and in properties it says that it is of type executable and of mime type application/x-executable, but if I double click it nothing happens (really nothing), what do I have to do to get the hello world message? How do I run it?

 

Thanks very much for any help, because after 2 hours of wasting time I'm getting quite depressed, because it is supposed to be a basic thing.

Posted
but if I double click it nothing happens
Well I haven't used linux much but the analogous thing on Windoze would cause a console window to appear and disappear in a jiffy. If it's the same in linux it would be better to call up the console window, cd to the right directory and type hello<return>.
Posted

Buffy, I tried but still doesn't work...

Qfwfq, when I just type hello (in the right directory) it says comand not found. Here is the "resumee":

[sandro@localhost ~]$ cd prove
[sandro@localhost prove]$ chmod 777 hello
[sandro@localhost prove]$ man chmod
[sandro@localhost prove]$ hello
bash: hello: command not found
[sandro@localhost prove]$ 

Posted

If you do ls does the file show up? If it does, then the command not found error would be the problem Buffy pointed out as the file isn't recognized as being a command of that name. Try ls with the option for displaying status, it should show whether Buffy's trick took effect.

Posted

If after checking that "hello" actually is in the directory, you should check to see if "." is in your path! Don't know what shell you're using but this should work for just about all shells:

echo $PATH

set $PATH $PATH:.

Use the second command if you don't see "." in the path.

 

Note also if you have some configuration settings different in your GNU environment, I think it may be spitting out a "hello.exe" which would be required for windows, but superfluous (and annoying) on *nix. If it does have an extension you can rename it without it because *nix doesn't give a hoot about them: its all about having execute permission for the file.

 

Go do,

Buffy

Posted
:D and I thought the current dir was always considered for commands!!!

:)

;)

Only in...wait for it...Windoze! I've been told that at the time it was considered a security feature in *nix, but that its real use turned out to be enforcing execution order, so you could make the current directory's version the default or the last resort if it wasn't found anywhere else in the path, or somewhere in the middle of the path for that matter...

 

Endless options,

Buffy

Posted

If I understood right I have to write this in the .bashrc file for example? I already put some other paths there to make cmbeasy run, so I guessed you mean that by shell...you know still on the steep path of learning *nix...

Posted
If I understood right I have to write this in the .bashrc file for example? I already put some other paths there to make cmbeasy run, so I guessed you mean that by shell...

Yep, the .bashrc file is the place to go. It should have a line in it like

set PATH /bin:/usr/bin:and soforth

and you just need to make sure that a "." is there all by itself like:

set PATH /bin:/usr/bin:.

That basically says, "when you're looking for this command I just typed on the command line, go look in the following directories in order to find the file with this name, and the first one you find that has execute permissions, try to run it as a script or an executable. If I put a "." in that means look in the current directory too."

 

biff,

Buffy

Posted

AS I'm on windows now doing things so that I can't change right away to linux, I have still a question because I remeber not having the "soforth" unless the one I added for cmbeasy. Is it important or shall I just add the "."?

Thanks for the help so far.

Posted
I have still a question because I remeber not having the "soforth" unless the one I added for cmbeasy. Is it important or shall I just add the "."?

Sorry about that! The "and soforth" is just a placeholder for a bunch of other paths that could be listed there. Ignore it completely! Its just that on most systems that I have played on, I've built up that path to be a full page long! There can be all sorts of garbage in there!

 

You should note that the down side of putting the "." at the end is that if there are any "hello"s in the other folders in the path, they will get executed and not the one you just built. Most of us put the "." directory in the front of the path, but that has the opposite downside....

 

ls -asdF,

Buffy

Posted

Well, it stil doesn't work. Also you wrote 2 different ways of doing it, I tried out all the combinations of (ie commenting some with #)

echo $PATH
set $PATH $PATH:.
set PATH /bin:/usr/bin:
set PATH /bin:/usr/bin:.

But still having all four as above for example and hello being stored in ~/prove I get

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/sandro/cmbeasy//bin:/home/sandro/bin:/home/sandro/cmbeasy//bin
[sandro@localhost ~]$ hello
bash: hello: command not found
[sandro@localhost ~]$ 
[sandro@localhost ~]$ cd prove
[sandro@localhost prove]$ hello
bash: hello: command not found
[sandro@localhost prove]$ 

Posted
Well, it stil doesn't work. Also you wrote 2 different ways of doing it, I tried out all the combinations of (ie commenting some with #)

echo $PATH
set $PATH $PATH:.
set PATH /bin:/usr/bin:
set PATH /bin:/usr/bin:.

The second line "set $PATH $PATH:." is a no-op, because the name of the environment variable is "PATH" and the dollar sign is used to cause it to be evaluated in expressions. This line as written creates a variable that the system does not care about called $PATH... you can take this out.

 

You obviously have a big PATH defined though as shown in this line:

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/sandro/cmbeasy//bin:/home/sandro/bin:/home/sandro/cmbeasy//bin

Unfortunately the code in the first block above does not echo the PATH until *after* you've altered it, so its hard to see if it even took effect. Try rewriting the .bashrc as:

set PATH $PATH:.
echo $PATH

Then when you open the shell window it should show you the path and have a dot at the end of it.

But still having all four as above for example and hello being stored in ~/prove I get

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/sandro/cmbeasy//bin:/home/sandro/bin:/home/sandro/cmbeasy//bin
[sandro@localhost ~]$ hello
bash: hello: command not found
[sandro@localhost ~]$ 
[sandro@localhost ~]$ cd prove
[sandro@localhost prove]$ hello
bash: hello: command not found
[sandro@localhost prove]$ 

Its hard to see if its *actually* there (I believe you of course!), with the file name "hello" (not "hello.exe") and if it has proper permissions. so what we need next is for you to type:

ls -l hello*

So we can see what's there and what the permissions on the file are.

 

rm -f /,

Buffy

Posted

like all you have to do to compile and run a program is:

 

you need a program:

#include <iostream>
#include <math.h>

using std::cin;
using std::cout;

unsigned int factor(unsigned int num)
{
   unsigned int val=0; //we'llneed this later
   for(unsigned int i = int(sqrt(num))+1; i>1; i--) //this will go from sqareroot of the number -1, that is the largest prime factor you can have and go down.
   {
       if(!(num%i)) //test for primity
       {
           val=i; //set the value to i
       }

   }
   if(val <= 1)
   {
       return num;
   }
   cout << val << " * ";
   return factor(num/val);
}

int main()
{
   unsigned int num;
   cout << "Number to prime factor: ";
   cin >> num;
   cout << "Factors:n";
   cout << factor(num) << " = " << num << "n";
   return 0;
}

save it as primes_rec.cpp

then you run g++ primes_rec.cpp -o primes_rec

then you execute it via ./primes_rec

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...