error using cmake with llvm - c++

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.

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.

how to select a particular gcc-toolchain in clang?

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...

CUDA 7.0 complains about not supporting gcc 4.9

I have installed gcc-4.7, gcc-4.8, gcc-4.9
When I try to do:
luarocks install cutorch
I get an error:
In file included from /usr/include/cuda_runtime.h:59:0,
from <command-line>:0:
/usr/include/host_config.h:82:2: error: #error -- unsupported GNU version! gcc 4.9 and up are not supported!
#error -- unsupported GNU version! gcc 4.9 and up are not supported!
I found a similar problem
But when I try to run the command:
nvcc --compiler-bindir /usr/bin/gcc-4.7
I get an error:
nvcc fatal: No input files specified; use option --help for more information
I'm new and installed linux day ago. Please help me
Run the following commands before the installation:
export CXX=/usr/bin/g++-4.8
export CC=/usr/bin/gcc-4.8
this should set the compiler to gcc-4.8 for the compilation.