博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
clang complete
阅读量:5060 次
发布时间:2019-06-12

本文共 9741 字,大约阅读时间需要 32 分钟。

Clang Installation:

If you want to build clang yourself, see . Otherwise see below for already built binaries of clang for easier installation.

Debian/Ubuntu

sudo apt-get install clang

Gentoo

emerge clang

Arch Linux

pacman -S clang

MacOSX

sudo port install clang

Windows

On windows building clang is usually done using mingw or with msvc. Autocompletion works with both compilers, but overall clang itself works better with mingw than with vc++, although both are currently unable to completely link a full program. This is being investigated, but at the moment vc++ is unable to compile but can autocomplete, and mingw can compile & autocomplete but cannot link.

Building with msvc

Follow the  and add those lines to your .vimrc:

" The quotes at the beggining of clang_exec and at the end of clang_user_options are important, don't remove them" They basically trick vim into thinking clang executed fine, because the msvc build autocompletes correctly but fails" to compile." Don't forget to put paths with spaces in quotes other wise vim won't be able to execute the commandlet g:clang_exec = '"C:\path\to\clang.exe'let g:clang_user_options = '2> NUL || exit 0"'

If you use another compiler, or for some reasons you want to make it point to other system headers you can modify g:clang_user_options like this:

let g:clang_user_options = '-IC:\foo\bar "-IC:\path with spaces\keke" 2> NUL || exit 0"'

Building with mingw

It is possible to build clang using MinGW and get a decent performance (see the benchmarks: ). Following is a description of the method.

The instructions to build clang are based on those found in .

  1. Get MinGW  - the automated installer. Install it to the default location.
  2. Get CMake  - the binary win32 installer. I got version 2.8. Install it. Again, do not change the defaults.
  3. Get LLVM and Clang source code . I got version 2.8.
  4. Extract llvm sources. Let E:\LLVM be our working directory. After this step we are supposed to have llvm sources in E:\LLVM\llvm-2.8.
  5. Extract clang sources to E:\LLVM\llvm-2.8\tools. After this step we are supposed to have clang sources in E:\LLVM\llvm-2.8\tools\clang-2.8. However, this is not how it should be. Rename clang-2.8 to clang.
  6. Go to E:\LLVM\llvm-2.8. Create a build directory, let it be build.
  7. Open a command prompt (Run cmd.exe). Change the current directory to E:\LLVM\llvm-2.8\build.
  8. Run (this is a long line... do not forget to include the two dots ("..") in the end of the line):
    E:\LLVM\llvm-2.8\build>"C:\Program Files\CMake 2.8\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Release \-DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" \-DCMAKE_SHARED_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" -G "MinGW Makefiles" ..
    If you are using the 64-bit version of Windows, use this line instead (since CMake will be installed to C:\Program Files (x86)\CMake 2.8\bin):
    E:\LLVM\llvm-2.8\build>"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Release \-DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" \-DCMAKE_SHARED_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" -G "MinGW Makefiles" ..
  9. After the configuration is done run C:\MinGW\bin\mingw32-make. This will take some time to get LLVM and clang built.

    Note: If you are having g++ errors concerning the --disable-shared and --enable-static flags when attempting to make the project, remove these flags from the -DCMAKE_EXE_LINKER_FLAG and -DCMAKE_SHARED_LINKER_FLAGS options in the command from step 8 and run the modified command. Then attempt this step again.

  10. In order to install clang, use C:\MinGW\bin\mingw32-make install/strip (that is default for making packages with cmake, but not for normal installations). It strips symbols and hence produces smaller executable.

  11. Add the bin folder from the installation directory (which is usually C:\Program Files\LLVM\bin) to your system PATH. At this point, clang_complete should work for C files. But for using STL with C++, an additional step is required. Follow on to step 12.

  12. This step is required only if you are using the standard C++ libraries. At the root directory of your C++ project, create a file named.clang_complete. Add the following lines in the file:

    -IC:/MinGW/include-IC:/MinGW/lib-IC:/MinGW/lib/gcc/mingw32/4.6.2/include/c++-IC:/MinGW/lib/gcc/mingw32/4.6.2/include/c++/mingw32
    This is assuming that you have installed MinGW to C:\MinGW and you are using GCC version 4.6.2. If that is not the case, adjust the paths according to your system.

Instead of steps 3-5 you may get the sources using subversion (see , the section for unix-like systems).

A little explanation of the long cmake command line is due:

  • First, we would like a release build. A release build is built using most available optimizations and is of course much faster than a debug build.
  • Second, we would like to build a statically linked executable. By default MinGW builds an executable which depends on some MinGW dlls. This makes the start up time of the program much longer, since Windows has to look for all these dlls each time we start the program. By building it statically we make it start much faster. Since we are going to run clang every time we try to complete something, start-up time is of highly importance.

Using Clang

As written above, Clang chokes on the standard Windows header files. Luckily the good folks at MinGW wrote compatible header files which Clang is ok with.

Look at the following contrived example code. "Windows.h" is only included for illustrative purposes, it is not really needed here. The file name is 'test.cpp'.

#include 
#include
#include
#include
int main(){
typedef boost::shared_ptr
stringPtr; std::vector
stringVector; stringVector.push_back( stringPtr(new std::string) ); stringVector[0]-> return 0;}

The following code is the contents of "pch.h":

#include 
#include
#include
#include

I am using the rxvt-native (without X support) bash shell from cygwin for the following examples. Note that the paths on your system might be different, depending on the versions installed etc. Note also that I installed the boost library to E:\boost_1_44_0.

A script to create the pre-compiled-headers file:

$ cat build_clang_pch.bash clang -fno-exceptions -fgnu-runtime -x c++-header \    pch.h -o pch.h.pch \    -I"E:\LLVM\llvm-2.8\build\lib\clang\2.8\include" \    -I"C:\MinGW\lib\gcc\mingw32\4.5.0\include" \    -I"E:\boost_1_44_0" \    -fms-extensions -fmsc-version=1300 \    -D__MSVCRT_VERSION__=0x700 -D_WIN32_WINNT=0x0500 \    -include malloc.h

The following script runs clang without using a pre-compiled headers file.

$ cat test_completion_no_pch.bash clang -cc1 -fsyntax-only -fno-caret-diagnostics -fdiagnostics-print-source-range-info \    -code-completion-at=test.cpp:11:22 test.cpp \    -I"E:\LLVM\llvm-2.8\build\lib\clang\2.8\include" \    -I"C:\MinGW\lib\gcc\mingw32\4.5.0\include" \    -I"E:\boost_1_44_0" \    -fms-extensions -fmsc-version=1300 -fgnu-runtime \    -D__MSVCRT_VERSION__=0x700 -D_WIN32_WINNT=0x0500 \    -include malloc.h

The following script runs clang using a pre-compiled headers file.

$ cat test_completion.bash clang -cc1 -fsyntax-only -fno-caret-diagnostics -fdiagnostics-print-source-range-info \    -code-completion-at=test.cpp:11:22 test.cpp \    -I"E:\LLVM\llvm-2.8\build\lib\clang\2.8\include" \    -I"C:\MinGW\lib\gcc\mingw32\4.5.0\include" \    -I"E:\boost_1_44_0" \    -fms-extensions -fmsc-version=1300 -fgnu-runtime \    -D__MSVCRT_VERSION__=0x700 -D_WIN32_WINNT=0x0500 \    -include-pch pch.h.pch \    -include malloc.h

Summary

We used 3 'optimizations' to get clang work as fast as possible:

  1. Clang was built statically
  2. Clang was built using 'release' flags rather than debug
  3. We use a pre-compiled headers file.

Another 'optimization' you might try is normalizing your header files. In this context 'normalizing' means to not include a header file in another header file. Instead include all necessary header files in the cpp file. This is not always possible, but usually using forward declarations can get you a long way.

If you follow these guidelines, Clang will emit no errors and it would not be necessary to filter its output.

As a footnote, add the following to your .vimrc file:

" fix cygwin shell redirectionset shellredir=>\"%s\"\ 2>&1

This is necessary if you run gvim from a cygwin bash shell.

Using clang library

By default clang_complete use clang executable to complete the code. It is possible to use the clang library which improves the completion speed (look in the documentation of clang_complete for more informations). To make it working you need:

  • python installed
  • vim with python support (check it with :version)
  • in your .vimrc add: let g:clang_use_library = 1

You can check that clang_complete use the library with :let g:clang_use_library (it returns 1 when using the library)

Troubleshootings

Q: The completion works with the clang executable but when I use the clang library I have the following error message: 'User defined completion (^U^N^P) Pattern not found'

A: Most of the time this is due to a compilation error. To identify the problem do :call g:ClangUpdateQuickFix() followed by :copen. If you do not see any compilation problem and you still have '... Pattern not found' check that the clang library's is correctly loaded (put some debug messages in libclang.py to understand what happens).

转载于:https://www.cnblogs.com/weinyzhou/archive/2012/11/05/2808486.html

你可能感兴趣的文章
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
关于js sort排序方法
查看>>
JAVA面试常见问题之Redis篇
查看>>
javascript:二叉搜索树 实现
查看>>
网络爬虫Heritrix源码分析(一) 包介绍
查看>>
__int128的实现
查看>>
Problem - 1118B - Codeforces(Tanya and Candies)
查看>>
jdk1.8 api 下载
查看>>
svn 图标不显示
查看>>
getElement的几中属性介绍
查看>>
iOS 使用Quartz 2D画虚线 【转】
查看>>
平面最接近点对
查看>>
HTML列表,表格与媒体元素
查看>>
PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点?
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
linux基础-命令
查看>>
java对象的深浅克隆
查看>>
Hadoop流程---从tpch到hive
查看>>