g++ fatal error no such file or directory

混江龙づ霸主 提交于 2020-06-09 07:02:10

问题


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

Now is this related to vim, or to my command?

Thanks in advance.


回答1:


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 :./%.

And if you wish to have hot keys for that...

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.



来源:https://stackoverflow.com/questions/61850080/g-fatal-error-no-such-file-or-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!