安装LLVM、clang时不使用系统默认的gcc、g++版本:
官方手册安装文档:
We use here the command-line, non-interactive CMake interface.
Download and install CMake. Version 3.4.3 is the minimum required.
Open a shell. Your development tools must be reachable from this shell through the PATH environment variable.
Create a build directory. Building LLVM in the source directory is not supported. cd to this directory:
$ mkdir mybuilddir $ cd mybuilddirExecute 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/rootCMake 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.
After CMake has finished running, proceed to use IDE project files, or start the build from the build directory:
$ cmake --build .The
--build
option tellscmake
to invoke the underlying build tool (make
,ninja
,xcodebuild
,msbuild
, etc.)The underlying build tool can be invoked directly, of course, but the
--build
option is portable.After LLVM has finished building, install it from the build directory:
$ cmake --build . --target installThe
--target
option withinstall
parameter in addition to the--build
option tellscmake
to build theinstall
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