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

本文档提供了详细的步骤说明如何避免使用系统默认的gcc、g++版本来安装LLVM和Clang。包括配置CMake参数以指定特定的编译器路径及库路径,以及解决常见编译错误的方法。

安装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_PREFIXllvm安装路径
LLVM_LIBDIR_SUFFIXllvm库路径
CMAKE_C_COMPILER指定gcc路径
CMAKE_CXX_COMPILER指定g++路径
CMAKE_C_FLAGSc库路径
CMAKE_CXX_FLAGSc++库路径
CMAKE_CXX_LINK_FLAGSc++动态链接库
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’

官方的答复是:

c115b8e090369631138a08fc04f9fd733dc.jpg

解决方法:

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

 

 

 

转载于:https://my.oschina.net/beyondwxf/blog/1835138

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值