How to compile a cpp file directly from vim editor in Windows?

前端 未结 1 1773
孤街浪徒
孤街浪徒 2021-01-27 22:51

I\'ve recently installed a vim editor in my Windows operating system. I only know the conventional procedure i.e, creating the source file in the editor and running it from the

1条回答
  •  太阳男子
    2021-01-27 23:11

    I assume your version of Vim is windows version and not cygwin version.

    First you need to install a compiler, and make sure it's in your PATH.

    Also, read the documentation about quickfix window as this is the integrated vim way of compiling. :!make or :!g++ ... are not the way to go.

    MSVC

    I don't suppose this is the compiler you have as I expect you'd have used Visual Studio in that case. Anyway, IIRC, there is a msdev compiler plugin you could load with :compiler msdev, then you should able to run :make.

    Don't hesitate to complete my answer if you see errors.

    g++ through cygwin

    There is a big advantage: gnumake is properly configured: in the console you could run make foo, and if you have foo.cpp or foo.c and no Makefile in the current directory, this would compile the monofile project. In all cases, a Makefile is fine; and it's required with multiple source files.

    The big problem: pathnames are not expressed in the same way. They need to be translated. I provide a way to do that in my Build-Tools-Wrapper plugin. Just execute :BTW add cygwin.

    Then from vim, again type :make %<. That will translate into :make foo (assuming you're editing foo.cpp), which translates into make fooshell wise, which translates into $CXX $CPPFLAGS $CXXFLAGS $LDFLAGS foo.cpp -o foo $LDLIBS (or something like that).

    Note: this means the options can be tweaked with: :let $CXXFLAGS = '-std=c++17 -Wall -Wextra'

    BTW, if you have my build-tools-wrapper plugin, you can execute directly :Make instead of :make %<, or just directly, IIRC.

    g++ through mingw

    The good news: no need to translate pathnames

    The bad news, gnumake isn't correctly configured. This means that in the console make foo won't work. And consequently, this won't work from Vim.

    This time, you'll either need a Makefile, or you'll need to tweak 'makeprg' setting. Like for instance :let &makeprg = 'g++ -Wall -Wextra -std=c++17 -o %< %' and then type simply :make.

    0 讨论(0)
提交回复
热议问题