Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install clang pre-built binaries ubuntu 12.04

Tags:

ubuntu

llvm

I am very new to linux, and don't know where I need to put Clang pre-built binaries http://llvm.org/releases/download.html#3.3 . I download it, unpack with mouse, and add path to /bin to my $PATH, and path to /lib to $LD_LIBRARY_PATH, and add *.conf with path to my /lib to /etc/ld.so.conf.d . I even reboot my notebook.
...but still cant build my simple program with Code::Blocks GCC (error: /home/aadgrand/tmp/LLVM-3.3/final/llvm.src/lib/Support/Signals.cpp - undefined reference to `dladdr'). Text of the test programm:

#include <iostream>
#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
using namespace std;

int main()
{
    llvm::LLVMContext &c = llvm::getGlobalContext(); // error line
    cout << "Never printed" << endl;
    return 0;
}

What else I need to do? Maybe I need to use *.sig-file? Hm?

Same question as How to install CLang using precompiled binaries? . Sorry for repeat, but it is really very interesting and important.

===== UPDATED ======

I've just insalled llvm-3.1, same error like with llvm-3.4 (from pre-built binaries):

-------------- Build: Debug in test2 ---------------

Compiling: main.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
COLLECT_GCC_OPTIONS='-std=c++98' '-pg' '-g' '-v' '-Wall' '-fexceptions' '-I' '/usr/lib/llvm-3.1/include' '-c' '-o' 'obj/Debug/main.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.6/cc1plus -quiet -v -I /usr/lib/llvm-3.1/include -imultilib . -imultiarch x86_64-linux-gnu -D_GNU_SOURCE /home/kpdev/projects/test2/test2/main.cpp -quiet -dumpbase main.cpp -mtune=generic -march=x86-64 -auxbase-strip obj/Debug/main.o -g -Wall -std=c++98 -version -p -fexceptions -fstack-protector -o /tmp/ccbKoBDJ.s
GNU C++ (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
    compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include"
    #include "..." search starts here:
    #include <...> search starts here:
 /usr/lib/llvm-3.1/include
 /usr/include/c++/4.6
 /usr/include/c++/4.6/x86_64-linux-gnu/.
 /usr/include/c++/4.6/backward
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
    compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 65b5171ac1bd7b3f07dbea6bdb24be3d
/home/kpdev/projects/test2/test2/main.cpp: In function ‘int main()’:
/home/kpdev/projects/test2/test2/main.cpp:11:24: warning: unused variable ‘c’ [-Wunused-variable]
COLLECT_GCC_OPTIONS='-std=c++98' '-pg' '-g' '-v' '-Wall' '-fexceptions' '-I' '/usr/lib/llvm-3.1/include' '-c' '-o' 'obj/Debug/main.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as --64 -o obj/Debug/main.o /tmp/ccbKoBDJ.s
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-std=c++98' '-pg' '-g' '-v' '-Wall' '-fexceptions' '-I' '/usr/lib/llvm-3.1/include' '-c' '-o' 'obj/Debug/main.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'



Linking console executable: bin/Debug/test2
/usr/lib/llvm-3.1/lib/libLLVMSupport.a(Signals.o): In function `PrintStackTrace(void*)':
(.text+0x6c): undefined reference to `dladdr'
/usr/lib/llvm-3.1/lib/libLLVMSupport.a(Signals.o): In function `PrintStackTrace(void*)':
(.text+0x187): undefined reference to `dladdr'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
2 errors, 1 warnings


||=== Build finished: 2 errors, 1 warnings ===|
like image 368
kpdev Avatar asked Jul 15 '13 14:07

kpdev


People also ask

How do I install the latest version of clang on Ubuntu?

To open up a terminal window on Ubuntu, press Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Once the terminal window is open and ready to use, enter the apt install command and install the “clang-12” package, along with the –install-suggests command-line switch, which grabs all suggested packages.


2 Answers

The title of your question has misled both myself and Atropo. It looked as if you want to install the clang C++ compiler and build an llvm C++ program with it in Code::Blocks.

What you are actually trying to do is install clang+llvm (they are actually different things) and build an llvm program with GCC in Code::blocks. You did say "Code::Blocks GCC", but I assumed that just meant "I had Code::Blocks set up with GCC and now I want to use clang". The C::B build log that you have added, however, shows that you building with GCC.

Given that you are building your llvm program with GCC it is not clear why you want to install the clang compiler. Perhaps you want to use clang but you do not know that Code::Blocks can support the clang compiler, or do not know how to configure C::B to use clang. But let's take one thing at time. You can build this llvm program with gcc if you just get things set up properly. And - a separate issue - you can install clang properly. And - another separate issue - you can configure C::B to build with clang if you want.

Atropo pasted commands for installing the latest clang and its debugger lldb. But clang doesn't need all the llvm libraries and installing just clang and lldb does not give you all the libraries you need to build your llvm program.

You said in one of your comments that you had been able to build your program with clang++ in a terminal. That suggests you had, at that point, a working manual install of clang and llvm. But I don't know if you still have one after the intervening messing about and anyway I agree with Atropo that manually installing such complex software - customizing your PATH and LD_LIBRARY_PATH etc. etc. - is much less sensible than a managed installation from a package repoository, letting the package manager get things right.

First, let's build your llvm program with gcc in C::B, and for that, begin with some precautionary steps that may or may not be necessary:

  • Undo all that customizing you did and delete or conceal your manually installed clang/llvm binaries.

  • Get all your packages up to date. In a console, run:

    sudo apt-get update

  • Ensure you have all the basic packages for building C/C++ software. In the console, run:

    sudo apt-get install build-essential

That finishes the precautions. Next:

  • Install llvm from the standard ubuntu 12.04 repository. You'll get an old one, v2.9 but that's fine for now:

    sudo apt-get install llvm

Now you've got all the software you need. Keep that console handy and launch C::B in your simple llvm project.

  • As you now have a standard system-install of llvm, change the second two #include directives of your source code to the following:

    #include <llvm/Module.h>

    #include <llvm/LLVMContext.h>

  • Get llvm to tell you what compiler options you need to pass to GCC. In the console run:

    llvm-config --cxxflags

  • Copy the output of that command from the console to the clipboard. In C::B, open your project's Build options and navigate Compiler settings -> Other options for GCC. In the Other options edit-box, paste in the compiler options from the clipboard and OK it.

  • Get llvm to tell you what libraries you need to link. In the console run:

    llvm-config --libs

  • Copy the output of that command from the console to the clipboard (this time there is a lot of it). In C::B, navigate Build options -> Linker settings -> Other linker options. In the edit-box, paste in the library options from the clipboard and OK it.

  • Get llvm to tell you what (other) options you need to pass to the linker. In the console run:

    llvm-config --ldflags

  • Copy the output of that command from the console to the clipboard. In C::B, navigate, once again, Build options -> Linker settings -> Other linker options In the edit-box, after - repeat, after - the library options you have previously pasted there, paste in the additional linker options from the clipboard and OK it.

  • Now rebuild your project and this time it will compile and link with only only a warning for the unused variable c (at least it does for me).

That covers building your program. If you now run it, it will output "Never printed", which I guess you don't want it to; but that's a problem for you :) It builds and runs.

If you also want to install all the latest llvm and clang packages, read on.

  • Install the llvm toolchain repository, for Ubuntu 12.04, in your software sources

    sudo add-apt-repository 'deb http://llvm.org/apt/precise/ llvm-toolchain-precise main'

  • Install the gpg key of that repository to authenticate package downloads from it. Note the final hyphen on this commandline:

    wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key|sudo apt-key add -

  • Refresh your package database again:

    sudo apt-get update

  • Install the whole llvm+clang toolchain:

    sudo apt-get install clang-3.4 clang-3.4-doc libclang-common-3.4-dev libclang-3.4-dev libclang1-3.4 libclang1-3.4-dbg libllvm-3.4-ocaml-dev libllvm3.4 libllvm3.4-dbg lldb-3.4 llvm-3.4 llvm-3.4-dev llvm-3.4-doc llvm-3.4-examples llvm-3.4-runtime cpp11-migrate-3.4 clang-format-3.4

  • Prove that you have got clang 3.4:

    clang -v

This command should output:

Ubuntu clang version 3.4-1~exp1 (trunk) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix

Finally, you might want to configure C::B to use clang instead of GCC. I will just get you started.

  • In C::B, navigate to Compiler settings for GCC and click the Copy button. In the Add new compiler edit-box, enter clang-3.4 and click OK. Then follow the prompt that C::B gives you to update the "Toolchain executables" page.
like image 114
Mike Kinghan Avatar answered Sep 29 '22 16:09

Mike Kinghan


Instead of manually install it you can use the LLVM repo, add it to your apt sources and install it via apt-get or the Ubuntu packet manager. See LLVM apt repo on how to do it.

Do the steps as written in the Install paragraph, in a terminal:

sudo wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -

sudo apt-get install clang-3.4 lldb-3.4 
like image 39
Atropo Avatar answered Sep 29 '22 18:09

Atropo