Skip to content

Instantly share code, notes, and snippets.

@lakshits11
Forked from NigoroJr/cpp_on_windows_vim.md
Created June 27, 2020 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshits11/0aeffe3e06eb72d3da3c1960c4205149 to your computer and use it in GitHub Desktop.
Save lakshits11/0aeffe3e06eb72d3da3c1960c4205149 to your computer and use it in GitHub Desktop.
A guide for setting up C++ development environment on Windows

Set up a basic C++ development environment on Windows

What this guide is

This guide aims to walk you through setting up an environment for doing C++ development on Windows using cygwin, gvim, and the mighty command prompt. After completing this guide, you should have...

  • UNIX commands executable from the command prompt
  • smart completion (using clang) for C++
  • the base for adding more Vim plugins

This guide assumes you have a 64 bit Windows. It has been tested on Windows 7 Home Premium with gvim 7.4-1467 and clang-3.7.

Cygwin

We will start off by installing cygwin. Download the version that's right for your processor and install it following the wizard. When prompted for the packages to install, select clang (in the Devel category). Feel free to select more packages that you like. Remember the path that you installed cygwin in. It defaults to C:\cygwin64\ (for 64 bit version), but if you've changed it, you should note that. This guide will assume you are using the default location.

You can move on to the following steps while cygwin is installing stuff.

UNIX-ifying your command prompt

The next step is to add the directory of your cygwin binaries to the PATH environment variable. That's right, you can now type ls and not get schooled by command prompt! Go to Control Panel and open your system overview. Click on Advanced Settings -> Environment Variables. Edit PATH and append ;C:\cygwin64\bin (i.e. paths separated by ;). Do not add spaces before or after the semicolon. Now open up your command prompt and check that ls outputs the list of files and directories.

Try out the clang command. It has been verified that for clang-3.5, clang.exe is an executable, but for clang-3.7, the clang command is not executable from the command prompt. To work around this issue, make a backup of the original clang file at C:\cygwin64\bin\clang and copy the executable clang-*.exe (where * is the version number) and name it clang.exe. Now you should be able to run clang --version from within the command prompt.

Getting ready to Vim

Download Vim from tuxproject.de. You can either go down the 7z archive or the self-extracting route, but make sure you remember where you installed it. Note the version of lua that your Vim was compiled with. You can optionally add the directory where gvim.exe and vim.exe lives to your PATH so that you can run vim from the command prompt. Let's say we installed vim in C:\Program Files\Vim.

Lua

You need the lua DLL in order to use neocomplete.vim. Download the lua version stated in tuxproject.de so that you have matching versions. Let me save you a few keystrokes: Lua Download page. Get the DLL that's in the archive that you downloaded and place it in the top-level directory of you vim installation. In our case, it's C:\Program Files\Vim.

Option 1: Getting your dotfiles

If you have your own configuration that's Windows-compatible, you can use it easily. Let's say you cloned your dotfiles to ~/dotfiles in cygwin. In the command prompt, link your vim configs to your home directory.

> mklink C:\Users\Foobar\.vimrc C:\cygwin64\home\foobar\.vimrc
> mklink C:\Users\Foobar\.gvimrc C:\cygwin64\home\foobar\.gvimrc
> mklink /D C:\Users\Foobar\.vim C:\cygwin64\home\foobar\.vim

You can use _vimrc instead of .vimrc and vimfiles instead of .vim, but it makes it easier when configuring paths under .vim so I recommend the traditional .vimrc and .vim.

My dotfiles can be found here, FWIW.

Option 2: Using the Dark Power

dein.vim is a Vim plugin manager that's currently being actively developed as a successor for neobundle.vim. Let's try it out!

Download dein.vim on cygwin.

$ git clone https://github.com/Shougo/dein.vim /cygdrive/c/Foobar/.cache/dein/repos/github.com/Shougo/dein.vim

Or you can just download the zip file and extract it to C:\Users\Foobar\.cache\dein\repos\github.com\Shougo\dein.vim. Of course, you can change the location to whatever you like.

Here's a .vimrc that will get you started.

if &compatible
  set nocompatible
endif

set runtimepath+=~/.cache/dein/repos/github.com/Shougo/dein.vim
set runtimepath+=~/.vim

set number
syntax on

call dein#begin(expand('~/.cache/dein'))

call dein#add('Shougo/dein.vim')
call dein#add('Shougo/vimproc.vim', {
      \ 'build': 'make',
      \ })
" Use only if lua is enabled and autoload when going into insert mode
call dein#add('Shougo/neocomplete.vim', {
      \ 'depends': 'vimproc.vim',
      \ 'if': has('lua'),
      \ 'lazy': 1,
      \ 'on_i': 1,
      \ })
call dein#add('osyo-manga/vim-marching', {
      \ 'depends': ['vimproc.vim', 'neocomplete.vim'],
      \ 'lazy': 1,
      \ 'on_ft': 'cpp',
      \ })

call dein#end()

if dein#check_install()
  call dein#install()
endif

filetype plugin indent on

augroup MyDeinAutoCmd
  autocmd!
augroup END

" neocomplete.vim
if dein#tap('neocomplete.vim')
  function! s:neocomplete_source() abort
    let g:neocomplete#enable_at_startup = 1
    let g:neocomplete#force_omni_input_patterns = {
        \ 'cpp': '[^. *\t]\%(\.\|->\)\w*\|[A-Za-z>]\w*::\w*',
        \ }
  endfunction

  function! s:neocomplete_post_source() abort
    call neocomplete#initialize()
  endfunction

  execute 'autocmd MyDeinAutoCmd User' 'dein#source#' . g:dein#name
        \ 'call s:neocomplete_source()'
  execute 'autocmd MyDeinAutoCmd User' 'dein#source#' . g:dein#name
        \ 'call s:neocomplete_post_source()'
endif

" marching.vim
if dein#tap('vim-marching')
  function! s:marching_source() abort
    let g:marching_enable_neocomplete = 1
    let g:marching_clang_command = 'clang'
    let g:marching#clang_command#options = {
          \ 'cpp': '-std=c++11',
          \ }

    set updatetime=100
    imap <C-x><C-o> <Plug>(marching_force_start_omni_complete)
  endfunction

  execute 'autocmd MyDeinAutoCmd User' 'dein#source#' . g:dein#name
        \ 'call s:marching_source()'
endif

Grab the latest binary of vimproc.vim in the releases. Put this in C:\Users\Foobar\.cache\dein\repos\github.com\Shougo\vimproc.vim\lib\. You might want to do this after you run vim once with the above configurations.

Testing it out

Write a "Hello, World" program. If everything went well, you should see smart completion pop up when typing std::.

What now?

There are so many things that you can configure. For example, you can use neoinclude.vim for header files completions, quickrun.vim for quickly running the code without getting out of vim, etc. etc...

Happy Vimming!

Author

Naoki Mizuno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment