6

I have two g++ programs located at /usr/local/bin/ and /usr/bin/

I would like to have the default g++ to be in /usr/local/bin/. However, I do not want to change my PATH environment variable because for some other program. I would prefer the version in /usr/bin/ than that in /usr/local/bin/. Is this possible?

To make my point clear:

I want my default for my two program to be:

g++ in /usr/local/bin/
python in /usr/bin/

But in /usr/local/bin/ and /usr/bin/, both programs exist, what should I do?

  • What are the programs? Most of the time I run into a not-the-default-g++ requirement, I just specify it in the makefile. Otherwise you could run PATH=/usr/bin:$PATH ./application when you run the second program you mentioned, that way it has a different PATH than what is in your environment – rexroni Sep 10 '15 at 2:40
  • I will edit my problem to make it clearer – user40780 Sep 10 '15 at 2:45
10

Option 1: Make an override folder on your path

If you need these programs to be called in indirect ways (like by some application started by the window manager will call g++ or python, for instance), you should edit your path. You could simply add a new folder to the beginning of your path in your ~/.bashrc:

export PATH=/home/username/.bin:$PATH

and place two symbolic links to point to the appropriate programs:

ln -s /usr/bin/python /home/username/.bin/python
ln -s /usr/local/bin/g++ /home/username/.bin/g++

That way, once your ~/.bashrc is properly sourced (log out, then log back in), everything should find the right python and the right g++.

Option 2: Use an alias for bash to follow

If you are looking for a lighter weight solution, and if you only call python directly from bash, you could setup an alias in your ~/.bashrc:

alias python=/usr/bin/python

Option 3: Just change the name of python in /usr/local/bin/

Or you could always just rename /usr/local/bin/python to be /usr/local/bin/python-alternate or something. I wouldn't suggest renaming things in /usr/bin, since at least in Debian that is controlled by a package manager. Usually /usr/local/bin isn't.

Option 4: Specify the correct compiler in the Makefile

If your workflow uses make, or some broader application that calls make (such as autotools or cmake), there is almost always an option to specify your compiler. For instance, your makefile could look like:

CXX=/usr/local/bin/g++

all:
    $(CXX) inputfile.cpp -o outputfile

or with cmake you might configure with

cmake -D CMAKE_CXX_COMPILER=/usr/local/bin/g++ ..

Different programs will have different syntaxes for specifying the compiler, but you can most always specify it.

  • Option 2 doesn't make sense for a program like g++ which is very commonly invoked indirectly from makefiles and the like. – Gilles Sep 10 '15 at 22:04
  • @Gilles That is why I used python for that example. But you're right that g++ is kind of a special case, so I added a compiler-specific option. – rexroni Sep 12 '15 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.