How to install CLang using precompiled binaries? - c++

How do I install CLang on Ubuntu, using precompiled binaries of CLang that I downloaded?
Here's how I downloaded CLang: "LLVM Download Page" -> "Download LLVM 3.2" -> "Clang Binaries for Ubuntu-12.04/x86_64" ( http://llvm.org/releases/3.2/clang+llvm-3.2-x86_64-linux-ubuntu-12.04.tar.gz .)
Then, I expanded the archive into a folder on my Ubuntu 12.04 LTS 64-bit machine. The contents of the expanded folder look like this:
$ ls clang+llvm-3.2-x86_64-linux-ubuntu-12.04
bin docs include lib share
Question: What do I do next? Do I have to copy these into some folders myself, and if so, which ones exactly? Most instructions I found online are for building CLang from source, which doesn't apply here.
I am a newbie to most of these tools. I created a basic hello-world C++ program, and was able to compile and run it, using GCC and autotools. Now, I want to compile the same program with CLang.

You can follow the same step as mentioned in https://askubuntu.com/questions/89615/how-do-i-install-llvm-clang-3-0
using GNU tar:
wget <clang-binaries-tarball-url> # or `curl -O <url>`
tar xf clang*
cd clang*
sudo cp -R * /usr/local/
If your tar isn't GNU and
the archive you get is .tar.gz, you can use tar -xzf;
if you have .tar.xz archive, you can use tar -xJf;
for .tar.bz2 archive, you can use tar -xjf.

Assuming you compiled your program with g++ hello.cpp
The equivalents of gcc and g++ are clang and clang++ accordingly. They are found in the bin folder.
It doesn't matter where you place the folders of clang, what matters is you don't move them later. So place them somewhere (I prefer $HOME and I'll assume this for the next)
Then:
Prepend it to $PATH variable
export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:$PATH
Make this permanent by adding it to ~/.bashrc
echo "export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:\$PATH" >> ~/.bashrc
Now you can do clang++ hello.cpp

I would like to install clang in /home/s. i.e.,
/home/s
bin
lib
include
...
I did the following in Ubuntu:
wget <clang-binaries-tarball-url>
sudo tar -xf <clang+llvm-..tar.xz> --strip-components=1 -C /home/s
# Set the path environmental variable
export PATH=/home/s/bin:$PATH
# Tell ldconfig about new shared library in /home/s/lib
cd /home/s
cat > libs.conf << "END"
/home/s/lib
END
sudo mv libs.conf /etc/ld.so.conf.d/libs.conf
sudo ldconfig
To test it:
clang --version
The output is:
clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/s/bin
Let's test C++17 Filesystem withex1.cpp
#include <iostream>
#include <filesystem>
int main() {
for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
std::cout << file.path() << '\n';
}
}
Compile it
clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic ex1.cpp -o ex1 -lc++fs
Run it
./ex1
The output:
"./ex1"
"./ex1.cpp"

Related

I want to type "g++" instead of "i686-pc-cygwin-g++" on Cygwin64 Terminal?

Everytime I have to compile with Cygwin64 Terminal, I have to write
$ i686-pc-cygwin-g++ myFile.cpp
Is there a way I can tell Cygwin that whenever I type g++ I'm actually referring to i686-pc-cygwin-g++ since it says -bash: g++: command not found
when I type g++ .
If there are no at all link to g++ in one of standard directories, like /usr/bin/g++, just create it.
Soft link:
ln -s 'path/to/i686-pc-cygwin-g++' '/usr/bin/g++`
Or even hard link:
ln 'path/to/i686-pc-cygwin-g++' '/usr/bin/g++'
Those are base commands, must be available in any bash shell.
The directory /usr/bin must be in the standard search path.
If link exists or if you use several compilers and want to switch between them, you can install and use update-alternatives, like
sudo update-alternatives --install /usr/bin/g++ g++ /path/to/i686-pc-cygwin-g++ 10
where 10 is priority.
Switch between them with:
sudo update-alternatives --config g++
remove all:
sudo update-alternatives --remove-all g++
As #Amadeus mentioned, it worked :
$ alias g++=i686-pc-cygwin-g++
Made me able to type g++ instead of i686-pc-cygwin-g++

clang++ can not locate c++ header and library

My OS is OS X 10.10.2 and the default compiler for C is clang.
But this version of clang does not support ubsan (undefined sanitizer) which comes in the 3.4 release of clang. I also want to use KLEE to do some analysis. AFAIK KLEE works well with LLVM-<=3.4. I decided to install
llvm-3.4 and clang-3.4 in my laptop.
After installing clang-3.4 in my system, I encountered a issue that the compiler can not locate the c++ header file. I installed clang-3.4 in /usr/local and I can find the c++ header file in /usr/local/include/c++/4.8.4. How can I add this directory to the search path of clang-3.4 and also the c++ library?
for the following demo code:
#include <iostream>
using namespace std;
int main(){
cout<<"Hellow World\n";
return 0;
}
When I compile it using command clang++ test.cpp, I got the error
test1.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Below is the version of clang I used
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
I used the following shell command to install llvm-3.4 and clang-3.4:
wget http://llvm.org/releases/3.4/llvm-3.4.src.tar.gz \
http://llvm.org/releases/3.4/clang-3.4.src.tar.gz \
http://llvm.org/releases/3.4/clang-tools-extra-3.4.src.tar.gz \
http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz
tar zxf llvm-3.4.src.tar.gz
tar zxf clang-3.4.src.tar.gz -C llvm-3.4/tools
mv llvm-3.4/tools/clang{-3.4,}
tar zxf clang-tools-extra-3.4.src.tar.gz -C llvm-3.4/tools/clang/tools
mv llvm-3.4/tools/clang/tools/{clang-tools-extra-3.4,extra}
tar zxf compiler-rt-3.4.src.tar.gz -C llvm-3.4/projects
mv llvm-3.4/projects/compiler-rt{-3.4,}
cd llvm-3.4
./configure --enable-cxx11 \
--enable-bindings=none --enable-shared \
--enable-debug-symbols --enable-optimized
make
make install
Now I have two versions of clang in my OS, one is the default one shipped with OSX located in /usr/bin and the other is clang-3.4 located in /usr/local/bin. The previous one can find the C++ header file while the latter can not.
Did you read the user documentation of clang notably the section on command line options ? BTW, current (march 2015) version of clang is 3.6!
For C++ code you should use the clang++ command, not the clang command.
You might pass -I and -L options to clang++ but you might perhaps have misinstalled your clang compiler.
You should be aware of the -v and -H options of clang or clang++ ; they could be useful, at least to understand more your issue.
addenda
BTW, a program reported to work with Clang 3.4 is extremely likely to work with a more modern version, like Clang 3.5 or 3.6
You probably have a PATH issue; you should have configure -d your Clang-3.4 & LLvm-3.4 programs with --program-suffix=-my-3.4 (if you do that, repeat your entire compiler build and installation) and you probably should run /usr/local/bin/clang++-my-3.4 ; maybe you also need some --with-gcc-toolchain additional configure option.
I'm pretty sure that you should be able to try to compile or use your mysterious software requiring Clang-3.4 with a more modern version like 3.5 or 3.6 ; your MacOSX 10.10.2 is rumored to have a Clang-3.5 based system compiler, it very probably is able to compile and work with your mysterious software.

Installing and using the ParMetis library

I am installing ParMetis 4.0.3 but into a non-default directory, with:
make config prefix=/My-ParMETIS-Directory/
And afterward:
make install
After that, I get a set of directories in that folder. And to write a program that uses it, I'm supposed to add the 'include "parmetis.h"' in the headline, and also add the 'libraries/binaries'. How exactly do I do the latter?
I'm just trying to get my code to compile now, and doing so I run:
g++ test.cpp
This is test.cpp:
#include<iostream>
#include "include/parmetis.h"
using std::cout;
using std::endl;
int main()
{
cout << "Test!" << endl;
return 0;
}
I keep getting "was not declared in this scope" for everything/every-line in parmetis.h.
How can I get test.cpp use the other folders/files that were installed?
You need to provide the location of your ParMetis library to
the compiler, since you have choosen to install the library
not in the default library directories.
mpic++ test.cpp -I /My-ParMETIS-Directory/ -I /My-METIS-Directory/
Edit#2:
What I did to get your code compiling:
Download the ParMetis library from webpage to /tmp
cd /tmp/
wget http://glaros.dtc.umn.edu/gkhome/fetch/sw/parmetis/parmetis-4.0.3.tar.gz
Unpack archive
tar -xf parmetis-4.0.3.tar.gz
Install ParMETIS into directory /tmp/parmetis
mkdir parmetis
cd parmetis-4.0.3/
make config prefix=/tmp/parmetis
make install
Install METIS into directory /tmp/metis
cd /tmp/
mkdir metis
cd parmetis-4.0.3/metis
make config prefix=/tmp/metis
make install
Compile test.cpp which is located in /tmp/
cd /tmp/
mpic++ test.cpp -I /tmp/parmetis -I /tmp/metis
when you coding wtih C++ ,if you meet question like that :
ccJjiCo.o: In function `main':
bsplele.cpp:(.text+0x45e): undefined reference to `METIS_PartMeshNodal'
collect2: ld returned 1 exit status
you can check the compiler sentence and you should paste the -lmetis at the end of the compilation command.It is useful for me.
my right compilation command is that:g++ -L/home/hadoop/metis/lib -I/home/hadoop/metis/include/ LL_metis.cpp -lmetis

How to install clang in custom location from SVN, making it recognize c++ standard library, on OS X

I try to compile and use clang from svn trunk. I basically try to follow the directions here:
svn co -q http://llvm.org/svn/llvm-project/llvm/trunk llvm
svn co -q http://llvm.org/svn/llvm-project/cfe/trunk llvm/tools/clang
svn co -q http://llvm.org/svn/llvm-project/clang-tools-extra/trunk llvm/tools/clang/tools/extra
svn co -q http://llvm.org/svn/llvm-project/compiler-rt/trunk llvm/projects/compiler-rt
mkdir llvm_build_release
cd llvm_build_release
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/usr/local -DLLVM_TARGETS_TO_BUILD=host ../llvm
make -j12
make install
Above, I configure clang to be installed in the custom location ~/usr/local since I want to be able to play with it without changing my default environment.
I then create a simple test.cpp:
#include <iostream>
int main(int argc, const char* argv[]){
std::cout << "Hello world\n";
return 0;
}
and try to compile it:
~/usr/local/bin/clang++ test.cpp -o test
but get the error message:
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
(using the system version of clang, the same compilation works fine).
If I manually enter which standard library to use, it does work
~/usr/local/bin/clang++ -std=c++11 -stdlib=libstdc++ -I/usr/include/c++/4.2.1/ -L/usr/lib test.cpp -o test
The question is: How do I configure, compile and install clang from source so that I do not have to enter these standard library settings, but instead can just enter the ordinary ~/usr/local/bin/clang++ test.cpp -o test? I have macports installed, with its version of the standard libraries and the include files, if that helps.

Linking g++ 4.8 to libstdc++

I downloaded and built gcc 4.8.1 on my desktop, running 64-bit Ubuntu 12.04. I built it out of source, like the docs recommend, and with the commands
../../gcc-4.8.1/configure --prefix=$HOME --program-suffix=-4.8
make
make -k check
make install
It seemed to pass all the tests, and I installed everything into my home directory w/ the suffix -4.8 to distinguish from the system gcc, which is version 4.6.3.
Unfortunately when I compile c++ programs using g++-4.8 it links to the system libc and libstdc++ rather than the newer ones compiled from gcc-4.8.1. I downloaded and built gcc 4.8 because I wanted to play around with the new C++11 features in the standard library, so this behaviour is definitely not what I wanted. What can I do to get gcc-4.8 to automatically link to the standard libraries that came with it rather than the system standard libraries?
When you link with your own gcc you need to add an extra run-time linker search path(s) with -Wl,-rpath,$(PREFIX)/lib64 so that at run-time it finds the shared libraries corresponding to your gcc.
I normally create a wrapper named gcc and g++ in the same directory as gcc-4.8 and g++-4.8 which I invoke instead of gcc-4.8 and g++-4.8, as prescribed in Dynamic linker is unable to find GCC libraries:
#!/bin/bash
exec ${0}SUFFIX -Wl,-rpath,PREFIX/lib64 "$#"
When installing SUFFIX and PREFIX should be replaced with what was passed to configure:
cd ${PREFIX}/bin && rm -f gcc g++ c++ gfortran
sed -e 's#PREFIX#${PREFIX}#g' -e 's#SUFFIX#${SUFFIX}#g' gcc-wrapper.sh > ${PREFIX}/bin/gcc
chmod +x ${PREFIX}/bin/gcc
cd ${PREFIX}/bin && ln gcc g++ && ln gcc c++ && ln gcc gfortran
(gcc-wrapper.sh is that bash snippet).
The above solution does not work with some versions of libtool because g++ -Wl,... -v assumes linking mode and fails with an error.
A better solution is to use specs file. Once gcc/g++ is built, invoke the following command to make gcc/g++ add -rpath to the linker command line (replace ${PREFIX}/lib64 as necessary):
g++ -dumpspecs | awk '/^\*link:/ { print; getline; print "-rpath=${PREFIX}/lib64", $0; next } { print }' > $(dirname $(g++ -print-libgcc-file-name))/specs
I just had the same problem when building gcc-4.8.2. I don't have root access on that machine and therefore need to install to my home directory. It took several attempts before I figured out the magic required to get this to work so I will reproduce it here so other people will have an easier time. These are the commands that I used to configure gcc:
prefix=/user/grc/packages
export LDFLAGS=-Wl,-rpath,$prefix/lib
export LD_RUN_PATH=$prefix/lib
export LD_LIBRARY_PATH=$prefix/lib
../../src/gmp-4.3.2/configure --prefix=$prefix
../../src/mpfr-2.4.2/configure --prefix=$prefix
../../src/mpc-0.8.1/configure --prefix=$prefix --with-mpfr=$prefix --with-gmp=$prefix
../../src/gcc-4.8.2/configure --prefix=$prefix --with-mpfr=$prefix --with-gmp=$prefix --with-mpc=$prefix --enable-languages=c,c++
That got me a working binary but any program I built with that version of g++ wouldn't run correctly unless I built it with the -Wl,-rpath,$prefix/lib64 option. It is possible to get g++ to automatically add that option by providing a specs file. If you run
strace g++ 2>&1 | grep specs
you can see which directories it checks for a specs file. In my case it was $prefix/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/specs so I ran g++ -dumpspecs to create a new specs file:
cd $prefix/lib/gcc/x86_64-unknown-linux-gnu/4.8.2
$prefix/bin/g++ -dumpspecs > xx
mv xx specs
and then edited that file to provide the -rpath option. Search for the lines like this:
*link_libgcc:
%D
and edit to add the rpath option:
*link_libgcc:
%D -rpath /user/grc/packages/lib/%M
The %M expands to either ../lib or ../lib64 depending on whether you are building a 32-bit or a 64-bit executable.
Note that when I tried this same trick on an older gcc-4.7 build it didn't work because it didn't expand the %M. For older versions you can remove the %M and just hardcode lib or lib64 but that is only a viable solution if you only ever build 32-bit executables (with lib) or only ever build 64-bit executables (with lib64).
gcc -print-search-dirs will tell you where your compiler is looking for runtime libraries, etc. You can override this with the -B<prefix> option.