sanctus Posted March 19, 2007 Report Posted March 19, 2007 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. Quote
Buffy Posted March 19, 2007 Report Posted March 19, 2007 Does it have execute *permission* in the file system?? try:chmod 777 hello Allowed,Buffy Quote
Qfwfq Posted March 20, 2007 Report Posted March 20, 2007 but if I double click it nothing happensWell 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>. Quote
sanctus Posted March 20, 2007 Author Report Posted March 20, 2007 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]$ Quote
Qfwfq Posted March 20, 2007 Report Posted March 20, 2007 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. Quote
Buffy Posted March 20, 2007 Report Posted March 20, 2007 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 $PATHset $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 Quote
Qfwfq Posted March 20, 2007 Report Posted March 20, 2007 :D and I thought the current dir was always considered for commands!!!:);) Quote
Buffy Posted March 20, 2007 Report Posted March 20, 2007 :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 Quote
sanctus Posted March 20, 2007 Author Report Posted March 20, 2007 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... Quote
Buffy Posted March 20, 2007 Report Posted March 20, 2007 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 likeset PATH /bin:/usr/bin:and soforthand 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 Quote
sanctus Posted March 20, 2007 Author Report Posted March 20, 2007 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. Quote
Buffy Posted March 20, 2007 Report Posted March 20, 2007 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 Quote
sanctus Posted March 21, 2007 Author Report Posted March 21, 2007 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]$ Quote
Qfwfq Posted March 21, 2007 Report Posted March 21, 2007 Only in...wait for it...Windoze!Always something new to learn.... :confused: Quote
Buffy Posted March 21, 2007 Report Posted March 21, 2007 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 Quote
alexander Posted March 21, 2007 Report Posted March 21, 2007 lol guys its ./hello not just hello :lol: Quote
alexander Posted March 21, 2007 Report Posted March 21, 2007 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.cppthen you run g++ primes_rec.cpp -o primes_recthen you execute it via ./primes_rec Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.