To make it a little less magic, what you produced is an ELF executable. you can ask linux which file is of what kind using "file <filename>" command, e.g.
Code:
home/you> g++ main.cpp -o main
home/you> file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.0, dynamically linked (uses shared libs), not stripped
home/you>
Second, there are two kind of things a linux system can natively run: ELF executables (like g++, bash, and now main) and scripts (text files containing a batch of commands). when you invoke "bash <filename>", you start another instance of the shell and you ask it to read commands from <filename> rather than from the keyboard. After a good deal of characters read out of "main" without finding a 'new line' character, bash concludes that it is likely to be a binary file (such as .jpg images, .mp3 files, etc.) and not lines of text. It complains about it.
If you're comfortable with DOS (oops. I mean windows, sorry) shell, you certainly wonder now why just invoking "main" on the command line did not work and why you're forced to type "./main" instead.
The answer is that, unlike in DOS/windows, the unix shells only look for binaries in the directories that are mentionned in the $PATH environment variable, where DOS command interpreter looked first for a matching .EXE/.COM in the current directory and then in directories mentionned in %PATH%.
Note that you can see the content of your $PATH by typing
Code:
home/you> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games
By saying "./main", you override this, as what you're now giving is recognizable as a path, not simply as a name to be resolved by the shell. invoking "/home/you/main" would have worked as well.
If you find this silly and want to be able to test your freshly compiled programs, all you have to do is to include "the current directory" in your $PATH with
Code:
home/you> export $PATH=".:$PATH"
Be warned, however, that by making this, you could be fooled by a program named "ls" or "firefox" or (worse) "sudo" that lies in the current directory rather than in the regular system location for programs. That's why this "shortcut" isn't present by default.
If you like it that way, you can edit your .bashrc file so that the $PATH is fixed everytime you open a new console.
Bookmarks