Using more than one version of clang

I find that in a Linux distro I want to use both a past version of clang, offered as a package, and the latest version I build from the llvm/clang source files. I want to use either version at different times for the purpose of testing some code.

Is this possible ?

What are the things I must do to use both versions at different times ? In particular how does a version of clang find its own header files and libaries when I invoke "clang", and what do I have to do make sure that a particular version of "clang" I invoke correctly finds its own header files and libraries for the purposes of compiling and linking ?

It finds its own headers relative to the executable that the libraries
are compiled into, but the executable must be inside a specific
directory structure. If you just build from source then the
executables produced by the build (e.g. in `Release+Asserts/bin/`) are
already in the right directory structure.

What I usually do is add the build directory's bin/ to my PATH
whenever I want to use the freshly built one, and have my default PATH
use the system one.

-- Sean Silva

What are the things I must do to use both versions at different times ? In
particular how does a version of clang find its own header files and
libaries when I invoke "clang", and what do I have to do make sure that a
particular version of "clang" I invoke correctly finds its own header files
and libraries for the purposes of compiling and linking ?

It finds its own headers relative to the executable that the libraries
are compiled into, but the executable must be inside a specific
directory structure. If you just build from source then the
executables produced by the build (e.g. in `Release+Asserts/bin/`) are
already in the right directory structure.

Are you saying that if I just invoke "clang", and it is found in some directory within the PATH, then it automatically finds its header files and libraries relative to where the executable exists ? That would be excellent.

What I usually do is add the build directory's bin/ to my PATH
whenever I want to use the freshly built one, and have my default PATH
use the system one.

That is what I intend to do also.

Are you saying that if I just invoke "clang", and it is found in some
directory within the PATH, then it automatically finds its header files and
libraries relative to where the executable exists ? That would be excellent.

That is correct.

That would be excellent.

Actually it's not quite as peachy as you may think. For people who use
clang as a library the lookup happens relative to *their* binary,
which need not be in the magical specific directory hierarchy :confused: It's
really annoying.

-- Sean Silva

Where in the doc does it specify the directory hierarchy needed ?

It is hardcoded in CompilerInvocation::GetResourcesPath in
lib/Frontend/CompilerInvocation.cpp. The doxygen [1] describes what
the function does, but the actual path it looks for is hardcoded in
the function itself, so you will have to look at the source. You can
also specify this manually with the `-resource-dir
/path/to/resource/dir/` commandline argument to `clang -cc1`.

[1] <http://clang.llvm.org/doxygen/classclang_1_1CompilerInvocation.html#a21e87a1469b3212140f3befc6c37c4c6>

-- Sean Silva

I guess the question then is where in the clang doc does it explain
where the compiler looks for header files and libraries.

It is mentioned at
<http://clang.llvm.org/docs/LibTooling.html#builtin-includes>, but
that's not a very high-visibility place to have it.

We get at least one person per month that runs into issues due to not
finding the builtin includes, so we should probably bring the issue to
the forefront.

-- Sean Silva

I would agree with that. End-users of clang, or any C++ compiler, have to understand where a compiler looks for its header files and where a linker looks for its libraries.