g ++致命错误,无此文件或目录

So I am using the gvim editor to program in C++, however when I open a .cpp file on the editor then type :!g++ -std=C++17 file.cpp -o file I get this error: See attached file below:

C:\Windows\system32\cmd.exe /c(g++ -std=c++17 file.cpp - o file)
g++: error: file.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated

现在这与vim或我的命令有关吗?

提前致谢。

评论
Rose
Rose

It seems that you're unfortunately using gcc-mingw that doesn't come with gnumake properly configured (:let $CXXFLAGS='-std=c++17 -Wall -Werror'+ make %< would have been enough otherwise). Thus you'll have a do a few things manually. And don't misspell the parameters to g++. And forget :!{compilername}, this is want we had to do 25 years ago with vi. Unlike vi, vim permits to integrate compilation.

" to do once
:let &makeprg='g++ -std=c++17 -Wall -Werror % -o %<'

" to execute each time to compile
:make
:copen

" to navigate the errors
:cc      " jump to error under cursor
:cnext   " jump to next error
:cprev   " jump to previous error
:cclose  " close the quickfix window

-> :h quickfix

This shouldn't execute g++ from the wrong directory. If this still does, run :cd %:h.

The execution should work with :./%.

如果您希望为此使用热键...

nnoremap <silent> <F7>   :<c-u>update<cr>:make<cr>
nnoremap <silent> <C-F5> :<c-u>./%<cr>

nnoremap <silent> <F3>   :<c-u>cnext<cr>
nnoremap <silent> <S-F3> :<c-u>cprev<cr>

Automatically opening the quickfix windows only if there are errors or executing the result only if there are no errors requires a little more work.

点赞
评论