2

I'm working on a project using the C++ language (but - this is not a C++-specific question, really). On my system, there are several C++ compilers installed, several versions of the same compiler, and perhaps even some versions built myself under my own home directory. I use different compilers (of C++) for different purposes (although I'm exaggerating here a little to make the question more general).

Now, my project depends, for some reason, on the compiler being gcc rather than clang, and with version between 4.7 and 4.9 with a preference for a newer version.

I want to get CMake to find the relevant compilers I have, and to use the best one available, if there's a valid one. But - I want this to happen not just on my system; that is pretty easy to force. I want it to do this on any (reasonable) system, and not depend on the developer who grabs my code doing any customization of CMakeLists.txt or setting the compiler explicitly with CXX=/some/thing etc.

More concretely:

  • How do I affect the kinds of locations CMake looks for compilers in?
  • How do I affect the order in which these locations are searched?
  • How can I express preference of certain compiler providers and versions?
  • How can I express constraints on the compiler provider and version?

Notes:

  • Answers which work for CMake 3.x only are relevant, but I'm also interested in CMake 2.8.x .
  • You can tell me to RTFM, but please be specific about what FM and where in the FM (and whether it will actually help me).
  • I'm working on Linux in case it matters.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • I suspect if CMake searches, it is according to your path. So the order of the paths in $PATH. – hiandbaii Jan 22 '16 at 19:48
  • @hiandbaii: But that would be just part of the story. Suppose my path has 3 `g++`s, a `g++-5.2`, `g++-4.6`, `g++-4.8` and `g++-4.7` in that order. How can I tell it what to prefer? – einpoklum Jan 22 '16 at 19:56
  • I don't think there's any official way to restrict allowed compilers, but you can certainty write some control statements with CMAKE_CXX_COMPILER_ID and CMAKE_CXX_COMPILER_VERSION – hiandbaii Jan 22 '16 at 20:39
  • @hiandbaii: Can I express vesion constraints? – einpoklum Jan 22 '16 at 21:28
  • thats what you use with CMAKE_CXX_COMPILER_VERSION ? – hiandbaii Jan 22 '16 at 22:20

1 Answers1

0

CMake is using /usr/bin/c++ unless CXX is set or -DCMAKE_CXX_COMPILER is passed to your CMake call.

You can get the found compiler version from CMAKE_CXX_COMPILER and error out if it is not sufficient. Same is true if you check for specific C++ compiler features like variadic templates.
But I don't know of a way to check several compilers and choose one by CMake.

usr1234567
  • 21,601
  • 16
  • 108
  • 128