C/C++教程

安装YCM

本文主要是介绍安装YCM,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

安装Vundle

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Vundle也是vim的插件,所以放到~/.vim/bundle/下。

安装环境

# install CMake
sudo apt-get install build-essential cmake
# install Python
sudo apt-get install python-dev python3-dev

配置.vimrc

"set mouse=a
"set paste  "取消自动注释
set selection=exclusive
set selectmode=mouse,key
set number
set tabstop=4 
set softtabstop=4 
set shiftwidth=4 
set noexpandtab 
set relativenumber number  
set autoindent
set cindent

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()
"
Plugin 'VundleVim/Vundle.vim'
Plugin 'https://gitee.com/mirrors/youcompleteme.git' 
 "这个就是YCM插件
call vundle#end()            " required
filetype plugin indent on    " required
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

let g:ycm_server_python_interpreter='/usr/bin/python3'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
let g:ycm_key_invoke_completion = '<c-z>'
let g:ycm_semantic_triggers =  {
                        \ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
                        \ 'cs,lua,javascript': ['re!\w{2}'],
                        \ }
let g:ycm_confirm_extra_conf = 1 "设置为0关闭加载提示
let g:ycm_autoclose_preview_window_after_completion = 1 " 关闭原型提示
let g:ycm_autoclose_preview_window_after_insertion = 1 
"let g:ycm_key_list_select_completion = ['<C-o>', '<C-l>']
let g:ycm_show_diagnostics_ui = 0   "关闭语法诊断

然后运行:PluginInstall,等待安装完成。

然后编译YCM

cd ~/.vim/bundle/YouCompleteMe
python3 install.py --clang-completer

最后在项目目录下新建.ycm_extra_conf.py

import os
import ycm_core

flags = [ 
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',

'-std=c++11',

'-x',
'c++',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/usr/include/c++/9',  这里要改!系统的c++头文件目录
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
]

compilation_database_folder = ''

if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )


# This is the entry point; this function is called by ycmd to produce flags for
# a file.
def Settings( **kwargs ):
	if not database::
		return {
            'flags': flags,
            'include_paths_relative_to_dir': DirectoryOfThisScript()
        }
    filename = kwargs[ 'filename' ]
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
        return None

  # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  # python list, but a "list-like" StringVec object.
    return {
        'flags': list( compilation_info.compiler_flags_ ),
        'include_paths_relative_to_dir': compilation_info.compiler_working_dir_
    }

中文帮助手册

PS

学到一点新东西

inoremap <expr> <Down>     pumvisible() ? "\<C-n>" : "\<Down>"

i:插入状态下有效

nore:不递归

map:按键映射

:表示被代替按键是个表达式

pumvisible():Returns non-zero when the popup menu is visible, zero otherwise. See ins-completion-menu.This can be used to avoid some things that would remove the popup menu.

大概意思就是vim弹出选择菜单时返回非0值,这时候按就是,ctrl+n:向下选择,否则就是

这篇关于安装YCM的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!