Error compiling minimal OpenCL example from LLVM documentation using clang - c++

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!

Related

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.

ffmpeg armv7s compile segmentation fault

I use https://github.com/kewlbear/FFmpeg-iOS-build-script to compile ffmpeg,
but when i try to compile, for armv7s architecture i have next error
all other archs compile fine
clang: error: unable to execute command: Segmentation fault: 11 clang:
error: clang frontend command failed due to signal (use -v to see
invocation) Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM
3.6.0svn) Target: arm-apple-darwin14.3.0 Thread model: posix clang: note: diagnostic msg: PLEASE submit a bug report to
http://developer.apple.com/bugreporter/ and include the crash
backtrace, preprocessed source, and associated run script. clang:
note: diagnostic msg:
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed
source(s) and associated run script(s) are located at: clang: note:
diagnostic msg:
/var/folders/zk/wj2cn08s26x64nlzk3_kjnv80000gn/T/vp8-c6ec27.c clang:
note: diagnostic msg:
/var/folders/zk/wj2cn08s26x64nlzk3_kjnv80000gn/T/vp8-c6ec27.sh clang:
note: diagnostic msg:
******************** make: * [libavcodec/vp8.o] Error 254 make: * Waiting for unfinished jobs.... CC libavcodec/wmv2enc.o
Couple months ago all compiled without any errors.
Tried on couple versions of ffmpeg
Here is Clang -v
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0 Thread model: posix
and xcode version is Version 6.3.1 (6D1002)
I think i found a solution, as you can understand from this article http://www.cocoanetics.com/2014/10/xcode-6-drops-armv7s/ xcode doesn't have armv7s support for now, so armv7s will use armv7 version and it must not reduce performance.