下载篇
为了节约你的时间,请务必注意两点:
- github的速度提起来 (百度“github加速” 可以找到方法)
- clang如果按YCM官方要求,需要找网速快一点的进行下载(我暂时没有找到好的方法),实在下载不了,就用系统自带的。
参考: YCM Full installation guide 用Vundle进行安装。等大半天后,等来的结果还是失败。最终百度到方法:https://www.jianshu.com/p/d908ce81017a?nomobile=yes 章节1.5。进入~/.vim/vundle/目录,使用命令:
$ git clone --recursive https://github.com/Valloric/YouCompleteMe.git
这种方式比直接用vundle要直观多了,可以看到下载模块进度和错误打印。尝试几次clone后终于下载成功。
这时,打开vim终于不会有报错了。
编译篇
由于PC上有几个clang版本,为了更好的控制,决定自己手工去配置CMakefile进行编译。参考了文章:https://www.cnblogs.com/rickyk/p/3877238.html
- 按步骤建立一个ycm_build文件夹, 进入该文件夹,运行命令:
$ cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
出现如下信息:
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (found suitable version "3.6.9", minimum required is "3.5")
-- NOT using libclang, no semantic completion for C/C++/ObjC will be available
-- NOT using clang-tidy for static analysis.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chw/ycm_build
可以看到是没有clang,C/C++不能自动补全。
- 修改CMakefile.txt文件,加上clang支持
$ vi ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/CMakeLists.txt
修改如下几个配置:
option( USE_DEV_FLAGS "Use compilation flags meant for YCM developers" OFF )
#设置 USE_CLANG_COMPLETER 为 ON
option( USE_CLANG_COMPLETER "Use Clang semantic completer for C/C++/ObjC" ON)
option( USE_SYSTEM_LIBCLANG "Set to ON to use the system libclang library" OFF )
#以下2行设置自己的CLANG的PATH
set( PATH_TO_LLVM_ROOT "/opt/clangllvm-10.0.0" CACHE PATH "Path to the root of a LLVM+Clang binary distribution" )
set( EXTERNAL_LIBCLANG_PATH "/opt/clangllvm-10.0.0/lib/libclang.so" CACHE PATH "Path to the libclang library to use" )
主要是将USE_CLANG_COMPLETER 打开,后面LLVM和LIBCLANG的路径需要设置为自己llvm的路径。将ycm_build文件夹清空,重新执行cmake命令:
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (found suitable version "3.6.9", minimum required is "3.5")
-- Using libclang to provide semantic completion for C/C++/ObjC
-- Using external libclang: /opt/clangllvm-10.0.0/lib/libclang.so.10
-- NOT using clang-tidy for static analysis.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zokoo/ycm_build
这里可以看到clang已经成功添加了。但仍有clang-tidy没有用起来。Clang-tidy需要在cmake命令行定义,但加上我这边在编译到52%后会挂很久,然后失败。从这篇文章说这个没啥用,所以不折腾,有兴趣可以去了解了解。https://stackoverflow.com/questions/55168132/youcompleteme-and-clang-tidy
- 编译,运行如下命令:
$ cmake --build . --target ycm_core --config Release
出现如下报错:
Scanning dependencies of target BoostParts
[ 1%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/codecvt_error_category.cpp.o
[ 3%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/operations.cpp.o
[ 5%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/path.cpp.o
[ 7%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/path_traits.cpp.o
[ 9%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/portability.cpp.o
[ 11%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/unique_path.cpp.o
[ 13%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/utf8_codecvt_facet.cpp.o
[ 15%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/windows_file_codecvt.cpp.o
[ 17%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/c_regex_traits.cpp.o
[ 19%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/cpp_regex_traits.cpp.o
[ 21%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/cregex.cpp.o
[ 23%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/fileiter.cpp.o
[ 25%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/icu.cpp.o
[ 27%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/instances.cpp.o
[ 29%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/posix_api.cpp.o
[ 31%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex.cpp.o
[ 33%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_debug.cpp.o
[ 35%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_raw_buffer.cpp.o
[ 37%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_traits_defaults.cpp.o
[ 39%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/static_mutex.cpp.o
[ 41%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/usinstances.cpp.o
[ 43%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/w32_regex_traits.cpp.o
[ 45%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/wc_regex_traits.cpp.o
[ 47%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/wide_posix_api.cpp.o
[ 49%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/winstances.cpp.o
[ 50%] Linking CXX static library libBoostParts.a
[ 50%] Built target BoostParts
Scanning dependencies of target ycm_core
[ 52%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Candidate.cpp.o
[ 54%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o
In file included from /home/zokoo/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/ClangCompleter/Location.h:21:0,
from /home/zokoo/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/ClangCompleter/FixIt.h:21,
from /home/zokoo/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/ClangCompleter/CompletionData.h:21, from /home/zokoo/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/CandidateRepository.cpp:25:
/home/zokoo/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/ClangCompleter/ClangUtils.h:21:10: fatal error: clang-c/Index.h: No such file or directory
#include <clang-c/Index.h>
^~~~~~~~~~~~~~~~~
compilation terminated.
ycm/CMakeFiles/ycm_core.dir/build.make:86: recipe for target 'ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o' failed
make[3]: *** [ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o] Error 1
CMakeFiles/Makefile2:140: recipe for target 'ycm/CMakeFiles/ycm_core.dir/all' failed
make[2]: *** [ycm/CMakeFiles/ycm_core.dir/all] Error 2
CMakeFiles/Makefile2:152: recipe for target 'ycm/CMakeFiles/ycm_core.dir/rule' failed
make[1]: *** [ycm/CMakeFiles/ycm_core.dir/rule] Error 2
Makefile:131: recipe for target 'ycm_core' failed
make: *** [ycm_core] Error 2
找不到头文件。这是Include路径没把llvm包含进来。我采用的方法是定义新环境变量:
$ vi ~/.bashrc
在文件末尾加上三行:
export PATH=/opt/clangllvm-10.0.0/bin:$PATH
export C_INCLUDE_PATH=/opt/clangllvm-10.0.0/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/opt/clangllvm-10.0.0/include:$CPLUS_INCLUDE_PATH
保存退出,更新环境变量:
$ source ~/.bashrc
重新编译后OK。
使用篇
进入我的测试目录,打开test.c文件,会有类似如下报错:
NoExtraConfDetected: No .ycm_extra_conf.py file detected, so no compile flags are available. Thus no semantic suppor...
是因为缺少配置文件,可在当前文件夹下新建一个.ycm_extra_conf.py文件,写入如下内容:
def Settings( **kwargs ):
return {
'flags': [ '-x', 'c++', '-Wall', '-Wextra', '-Werror' ],
}
最终效果如下:
image.png
网友评论