I'm on Archx64 with bspwm as WM. I have both gcc and clang installed and updated to latest versions. I want build systems to ask me everytime to choose either g++ or clang ++ to compile.

I need clang++ as default compiler for my own stuff , but I have cmake that usually chooses compilers on its own. So I exported C and CXX as clang and clang++ in .bashrc . Everything runs fine as expected, but the problem arises when I need to compile something with g++ and the program looks for /usr/bin/c++ and finds clang++ instead of g++. I need , everytime some program tries to compile its stuff , it could ask me beforehand , which compiler to use. Is it possible?

My current method is to export each time and restart my machine :p which is insane amounts of stupidity.

  • I don't think it's bash's job (or the job of any shell) to tell any particular piece of software's build system how to choose among several available compilers. This is more like a CMake question, not a bash question. – Celada Feb 8 '15 at 12:21
  • @Celada Cmake is a cross platform tool and will snatch any compiler that is defined (sym-linked) in /usr/bin/c++ . I'm exhausted at this moment and have no idea whose job is it to do so , but cmake is not the one. – Abhinav Gauniyal Feb 8 '15 at 12:23
  • I guess you want to look into man alternatives – guido Feb 8 '15 at 12:45
  • @guido no manual entry for alternatives. and I miss ubuntu too :p where we could do update-alternatives. – Abhinav Gauniyal Feb 8 '15 at 12:46

There is no real general answer. If some tool insists on some tool it will do so.

If setting environment variables works for you, there is no need to reboot. You can simply call your command like this:

CXX=g++ cmake ...

This will run cmake with the CXX environment variable set to g++.

If you really want to do what you asked for you could replace /usr/bin/c++ (or whatever command is needed by your tool) by a shell script asking for the needed compiler and then calling that compiler. - But be really sure you know what you are doing if you decide to go that way.

| improve this answer | |
  • Sorry for bugging ,I have a small doubt, if I could source ~/.bashrc again with updated values of CC and CXX , would I need to restart my machine? – Abhinav Gauniyal Feb 8 '15 at 12:32
  • 1
    To update your environment variables you can either 1) use export CXX=... 2) write your variables in some file and source it using source filename or 3) simply set your variables for that single command as given in the answer. - No need for reboot in any case. – michas Feb 8 '15 at 12:35

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.