views:

7816

answers:

6

I know it, forgets it and relearn it again. Time to write it down.

A: 

./[name of the program]

For example ./a.out

P-A
Good for you! The dot and slash are there because on many systems, the current directory ("." in Unix terms) is not part of the path searched by the shell. Thus, adding it makes it explicit which program you want to run.
unwind
The reason for "." not being on the path is that you sometimes cd into other people's directories, where users might have left malicious programs in the hope you'll run them. Even if "." is after /usr/bin to prevent someone leaving a version of (for example) "ls", they might still catch typos.
Steve Jessop
Even better point.
P-A
The general principle is that you don't want to have a directory on the path where a less privileged user has write access. "." sometimes has that write access, since sometimes you're in a less privileged user's directory.
Steve Jessop
+6  A: 

gcc main.cpp -o main.out
./main.out

Neverrav
+5  A: 

Add following to get best warnings, you will not regret it. If you can, compile WISE (warning is error)

- Wall -pedantic -Weffc++ -Werror
Nazgob
Should really be the default.
Konrad Rudolph
I agree, but it's not for many projects / ppl.
Nazgob
And -Werror while your at it.
JesperE
+2  A: 

Use a makefile. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.

Read the make manual, it seems quite long at first glance but most sections you can just skim over. All in all it took me a few hours and made me much more productive.

Konrad Rudolph
Thank you, that is a good answer too. I really try to use make as mush as I can and that is also a reason for me forgetting the syntax of the "easy" way.
P-A
+7  A: 

If it is a simple single source program:

make foo

where the source file is foo.c or foo.cpp, etc.

You dont even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus extension.

camh
I didn't realize the builtin rules propagated to targets specified when invoking make. Learned something new today =)
Branan
+1  A: 

All application execution in a Unix (Linux, MacOS X, AIX etc) environment depends on the executable search path.

You can display this path in the terminal with this command:

echo $PATH

On MacOS X (by default) this will display the following colon separated search path:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

So any executable in the listed directories can by run just by typing in their name. Eg:

cat mytextfile.txt

This runs /bin/cat and displays mytextfile.txt to the terminal.

To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on MacOS X I can fully qualify it like so:

/Users/oliver/MyProgram

If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example if MyProgram was in the directory /Users/oliver/MyProject I and I was in my home directory I can qualify the executable name like this, and have it execute:

MyProject/MyProgram

Or say I was in the directory /Users/oliver/MyProject2 and I wanted to execute /Users/oliver/MyProject/MyProgram I can use a relative path like this, to execute it:

../MyProject/MyProgram

Similarly if I am in the same directory as MyProgram I need to use a "current directory" relative path. The current directory you are in is the period character followed by a slash. Eg:

./MyProgram

To determine which directory you are currently in use the pwd command.

If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example if you have a "bin" directory in your home directory for regularly used shell scripts of other programs if may be wise to alter your executable search path.

This can be does easily by either creating or editing the existing .bash_profile file in your home directory and adding the lines:

#!/bin/sh
export PATH=$PATH:~/bin

Here the tilde (~) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn't already exist). Note also that this technique requires that your login shell be bash (the default on MacOS X and most Linux distributions). Also note that if you want your programs installed in ~/bin to be used in preference to system executables your should reorder the export statement as follows:

export PATH=~/bin:$PATH
orj
If you add the comments from 'onebyone.livejournal.com' on my own answer about security this answer is complete. Thank you.
P-A