how to select a particular gcc-toolchain in clang? - c++

Clang automatically selects the gcc-version with the highest version:
$ clang++ -v main.cpp
clang version 3.8.1-12
(tags/RELEASE_381/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.0.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.0.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.0.1
how can i force clang to use a different gcc installation, say 5.4.1 ?
i tried to call clang with --gcc-toolchain="/usr/lib/gcc/x86_64-linux-gnu/5.4.1" but without success.

An valid path for --gcc-toolchain is apparently "/usr" as clang seem to look for gcc in
$PREFIX/{include|lib}/gcc/$PLATFORM/$VERSION/*
so as a workaround you can trick clang to use a particular version by creating a filesystem with overlay-fs or symlinking a folder-structure containing only one folder
mkdir $MYTOOLCHAIN
cd $MYTOOLCHAIN
ln -s /usr/include include #for headerfiles
ln -s /usr/bin bin #for tools like ld
mkdir -p lib/gcc/x86_64-linux-gnu/ #clang will deduce what to select
cd lib/gcc/x86_64-linux-gnu/
#link the toolchain we want here
ln -s /usr/lib/gcc/x86_64-linux-gnu/$VERSION $VERSION
#usage: clang++ --gcc-toolchain=$MYTOOLCHAIN main.cpp
however maybe there is a better way by instructing clang to pick the version via a flag...

Related

Error compiling minimal OpenCL example from LLVM documentation using clang

My goal is to use C++ code in OpenCL kernels, which has been, as far as I can tell, supported by clang for quite a while.
I am using clang 15.0.6 on my Ubuntu machine 22.04 and am trying to compile the examples found in the LLVM documentation here, the compiler is installed via the "official" script found here.
clang -v shows:
Ubuntu clang version 15.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;#m64
Selected multilib: .;#m64
Just like in the LLVM manual, I am trying to compile an empty kernel (kernel void k(){}) using this clang version, but it always throws a linker error:
$ clang test.cl
ld: error: undefined symbol: main
referenced by /lib/x86_64-linux-gnu/Scrt1.o:(\_start)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Surely I am missing something absolutely basic, but what it is?
Thanks in advance for any help!

Clang lexer goes into SEGFAULT

I'm trying to compile and use a Clang tool (github). It compiles w/o any errors, but when I try to execute it, it goes into SEGFAULT. Debugger gives me the following stack trace:
clang::Preprocessor::RemovePragmaHandler(llvm::StringRef, clang::PragmaHandler*) 0x00007f8dd0da9599 <-- SEGFAULT here
clang::Parser::resetPragmaHandlers() 0x00007f8dd0a78c65
clang::Parser::~Parser() 0x00007f8dd0aa8260
clang::Parser::~Parser() 0x00007f8dd0aa86ca
clang::ParseAST(clang::Sema&, bool, bool) 0x00007f8dd09f39c8
clang::FrontendAction::Execute() 0x00007f8dd0ecd459
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) 0x00007f8dd0e814ee
clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) 0x00007f8dd0b31e49
clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) 0x00007f8dd0b2803c
clang::tooling::ToolInvocation::run() 0x00007f8dd0b2dafc
Clang distribution contains no debug symbols, and my machine stucks when I trying to compile LLVM and Clang with debug symbols on my own.
LLVM version is 9.0.0, clang -v gives the following:
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/9.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/9.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0
Candidate multilib: .;#m64
Candidate multilib: 32;#m32
Selected multilib: .;#m64

Clang with CLion: Cannot get compiler information

I tried to switch from gcc to clang in CLion by changing the Toolchains preference:
But now the cmake fails with the following:
Cannot get compiler information:
Compiler exited with error code 1: /usr/bin/clang++ -xc++ -DENABLE_EXTRAS ...
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0
If I manually run CMake in a terminal using -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ then it will work, only in CLion it doesn't.
Using clang 6.0 solves the problem:
-DCMAKE_C_COMPILER=/usr/bin/clang-6.0
-DCMAKE_CXX_COMPILER=/usr/bin/clang++-6.0
in CMake Options and go to File -> Invalidate Caches / Restart.
Everything works normal.

error using cmake with llvm

I am trying to make a very basic skeleton code, this one, compile on Ubuntu 16.04. Here's all I have installed globally on the system:
clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.0.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;#m64
Selected multilib: .;#m64
Found CUDA installation: /usr/local/cuda
& llvm-3.8 and lldb-3.8 packages.
The error I am getting on using cmake is :
CMake Error at /usr/share/llvm-3.8/cmake/LLVMConfig.cmake:178 (include):
include could not find load file:
/usr/share/llvm/cmake/LLVMExports.cmake
Call Stack (most recent call first):
CMakeLists.txt:3 (find_package)
CMake Error at /usr/share/llvm-3.8/cmake/LLVMConfig.cmake:181 (include):
include could not find load file:
/usr/share/llvm/cmake/LLVM-Config.cmake
Call Stack (most recent call first):
CMakeLists.txt:3 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/kapil/Projects/CppProjects/hacking-llvm/build/CMakeFiles/CMakeOutput.log".
I am a beginner at using llvm (dont know about their cmake packaging structure) and don't know how to remove the error. I checked this error on google and several people have reported the bug, but I couldn't parse and get some information on how to deal with it. Please help.

C++11 not Working with Macports gcc47

I recently installed Macports on my Macbook Pro, and installed the gcc47 port. After adding alias g++="g++-mp-4.7" to my .profile and ensuring that I was using g++-4.7, I attempted to build one of my projects that makes use of C++11. However, I receive this error:
cc1plus: error: unrecognized command line option "-std=c++11"
I find this strange, since Macports built gcc with support for C++, so I would expect that C++11 should work without any problems. I have pasted the output from g++ -v below. Do you have any ideas as to why g++-mp-4.7 is not recognizing -std=c++11 as a valid option?
COLLECT_GCC=g++-mp-4.7
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.7.1/lto-wrapper
Target: x86_64-apple-darwin12
Configured with: ../gcc-4.7.1/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc47 --includedir=/opt/local/include/gcc47 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.7 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.7 --with-gxx-include-dir=/opt/local/include/gcc47/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --enable-stage1-checking --disable-multilib --enable-lto --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-ppl-version-check --with-pkgversion='MacPorts gcc47 4.7.1_2'
Thread model: posix
gcc version 4.7.1 (MacPorts gcc47 4.7.1_2)
For future reference, if you install gcc_select you can do:
sudo port select gcc mp-gcc47
hash gcc
This should setup your Macports installed gcc; if you need to reset to Xcode's default, just rerun it with 'none'
EDIT: and Xcode 4.4.x comes with a decent build of Clang; if you install it and then from Preferences->Downloads: Command Line Tools click "Install"
Run clang as:
clang++ -std=c++11 -stdlib=libc++ ...
Note that if mp-gcc47 is not installed, you'll need to do this first:
sudo port install mp-gcc47
To get a full list of available gcc ports:
sudo port list | grep gcc | less
My problem was that since GNU Make executes commands in a different shell, alias g++="g++-mp-4.7" will have no effect on the commands executed by make.