安装llvm、clang指定使用非默认gcc、g++

简介: 安装LLVM、clang时不使用系统默认的gcc、g++版本:官方手册安装文档:We use here the command-line, non-interactive CMake interface.

安装LLVM、clang时不使用系统默认的gcc、g++版本:

官方手册安装文档:

We use here the command-line, non-interactive CMake interface.

  1. Download and install CMake. Version 3.4.3 is the minimum required.

  2. Open a shell. Your development tools must be reachable from this shell through the PATH environment variable.

  3. Create a build directory. Building LLVM in the source directory is not supported. cd to this directory:

    $ mkdir mybuilddir
    $ cd mybuilddir
  4. Execute this command in the shell replacing path/to/llvm/source/root with the path to the root of your LLVM source tree:

    $ cmake path/to/llvm/source/root

    CMake will detect your development environment, perform a series of tests, and generate the files required for building LLVM. CMake will use default values for all build parameters. See the Options and variables section for a list of build parameters that you can modify.

    This can fail if CMake can’t detect your toolset, or if it thinks that the environment is not sane enough. In this case, make sure that the toolset that you intend to use is the only one reachable from the shell, and that the shell itself is the correct one for your development environment. CMake will refuse to build MinGW makefiles if you have a POSIX shell reachable through the PATH environment variable, for instance. You can force CMake to use a given build tool; for instructions, see the Usage section, below.

  5. After CMake has finished running, proceed to use IDE project files, or start the build from the build directory:

    $ cmake --build .

    The --build option tells cmake to invoke the underlying build tool (makeninjaxcodebuildmsbuild, etc.)

    The underlying build tool can be invoked directly, of course, but the --build option is portable.

  6. After LLVM has finished building, install it from the build directory:

    $ cmake --build . --target install

    The --target option with install parameter in addition to the --build option tells cmake to build the install target.

    It is possible to set a different install prefix at installation time by invoking the cmake_install.cmake script generated in the build directory:

    $ cmake -DCMAKE_INSTALL_PREFIX=/tmp/llvm -P cmake_install.cmake

其中第四步添加些参数:

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/llvm-6.0.0_gcc-7.2.0/ -DCMAKE_C_COMPILER=/usr/local/gcc/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/gcc/bin/g++ -DCMAKE_C_FLAGS=-L/usr/local/gcc/lib64 -DCMAKE_CXX_FLAGS=-L/usr/local/gcc/lib64 -DCMAKE_CXX_LINK_FLAGS="-L/usr/local/gcc/lib64 -Wl,-rpath,/usr/local/gcc/lib64" -DGCC_INSTALL_PREFIX=/usr/local/gcc -DCMAKE_BUILD_TYPE=Release /usr/local/llvm-6.0.0.src

 

CMAKE_INSTALL_PREFIX llvm安装路径
LLVM_LIBDIR_SUFFIX llvm库路径
CMAKE_C_COMPILER 指定gcc路径
CMAKE_CXX_COMPILER 指定g++路径
CMAKE_C_FLAGS c库路径
CMAKE_CXX_FLAGS c++库路径
CMAKE_CXX_LINK_FLAGS c++动态链接库
GCC_INSTALL_PREFIX  

 

clang安装参考:http://clang.llvm.org/get_started.html

Note: as an experimental setup, you can use a single checkout with all the projects, and an easy CMake invocation, see the LLVM Doc "For developers to work with a git monorepo"

If you would like to check out and build Clang, the current procedure is as follows:

Get the required tools.
See Getting Started with the LLVM System - Requirements.
Note also that Python is needed for running the test suite. Get it at: http://www.python.org/download
Standard build process uses CMake. Get it at: http://www.cmake.org/download
Check out LLVM:
Change directory to where you want the llvm directory placed.
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
Check out Clang:
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../..
Check out extra Clang tools: (optional)
cd llvm/tools/clang/tools
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
cd ../../../..
Check out Compiler-RT (optional):
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ../..
Check out libcxx: (only required to build and run Compiler-RT tests on OS X, optional otherwise)
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx(这个可选包被我删了)
cd ../..
Build LLVM and Clang:
mkdir build (in-tree build is not supported)
cd build
cmake -G "Unix Makefiles" ../llvm
make
This builds both LLVM and Clang for debug mode.
Note: For subsequent Clang development, you can just run make clang.
CMake allows you to generate project files for several IDEs: Xcode, Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3. For more details see Building LLVM with CMake page.
If you intend to use Clang's C++ support, you may need to tell it how to find your C++ standard library headers. In general, Clang will detect the best version of libstdc++ headers available and use them - it will look both for system installations of libstdc++ as well as installations adjacent to Clang itself. If your configuration fits neither of these scenarios, you can use the -DGCC_INSTALL_PREFIX cmake option to tell Clang where the gcc containing the desired libstdc++ is installed.
Try it out (assuming you add llvm/build/bin to your path):
clang --help
clang file.c -fsyntax-only (check for correctness)
clang file.c -S -emit-llvm -o - (print out unoptimized llvm code)
clang file.c -S -emit-llvm -o - -O3
clang file.c -S -O3 -o - (output native machine code)
Run the testsuite:
make check-clang

按照官方的这个操作编译clang时会出现如下错误:

error: ‘sort’ is not a member of ‘llvm’

官方的答复是:

解决方法:

http://releases.llvm.org/download.html从这里下载最新的clang代码,替换llvm/tools/clang下的所有文件,重新编译就可以了

 

参考:

官方文件:http://llvm.org/docs/CMake.html#usage

clan安装:http://clang.llvm.org/get_started.html

https://www.cnblogs.com/zengkefu/p/6349534.html

目录
相关文章
|
4月前
|
NoSQL 编译器 开发工具
006.gcc编译器
gcc是什么?
47 0
006.gcc编译器
|
5月前
|
存储 NoSQL 算法
从一个crash问题展开,探索gcc编译优化细节
问题分析的过程也正是技术成长之路,本文以一个gcc编译优化引发的crash为切入点,逐步展开对编译器优化细节的探索之路,在分析过程中打开了新世界的大门……
437 1
|
1天前
|
C语言
gcc的简易用法
【5月更文挑战第10天】gcc的简易用法。
14 8
|
26天前
|
C语言
转载 - gcc/ld 动态连接库和静态连接库使用方法
本文介绍了如何在GCC中实现部分程序静态链接、部分动态链接。使用`-Wl`标志传递链接器参数,`-Bstatic`强制链接静态库,`-Bdynamic`强制链接动态库。
37 0
|
2月前
|
编译器 C语言 C++
列举gcc 常见和有用的编译警告选项
列举gcc 常见和有用的编译警告选项
14 0
|
2月前
|
编译器 C语言
gcc编译警告:warning: suggest parentheses around assignment used as truth value
gcc编译警告:warning: suggest parentheses around assignment used as truth value
27 0
|
2月前
|
编译器 Linux C语言
gcc编译器的使用方法
gcc编译器的使用方法
23 1
|
3月前
|
编译器 C语言
gcc/g++语法
gcc/g++语法
|
5月前
|
C语言
gcc静态编译/usr/bin/ld: cannot find -lc
gcc静态编译/usr/bin/ld: cannot find -lc
|
6月前
|
编译器 程序员 C语言
gcc的编译过程和gcc与g++的区别
gcc的编译过程和gcc与g++的区别
55 0